From 00d8d550b5037d44385b2ccd35d543da5aa087ed Mon Sep 17 00:00:00 2001 From: Xuanyu Chen Date: Tue, 17 Oct 2017 10:47:40 -0700 Subject: [PATCH] Add files via upload drop rows, not yet --- P.R | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 P.R diff --git a/P.R b/P.R new file mode 100644 index 0000000..54666d6 --- /dev/null +++ b/P.R @@ -0,0 +1,55 @@ +library(data.table) +library(parallel) +nthread = 4 +count = 0 +cluster = makePSOCKcluster(rep("localhost", nthread)) +data = fread("test1.txt", sep = " ", header = FALSE) + +smaller_than <- function(lhs, rhs) { + #both are pair + if (lhs[1] == rhs[1]) + return (lhs[2] < rhs[2]) + else + return (lhs[1] < rhs[1]) +} + +binary_search <- function (edges, value) { + up = nrow(edges) + down = 1 + + while(down <= up) { + midpoint = ceiling((up+down)/2) + pair = as.numeric(edges[midpoint]) + if (smaller_than(value, pair)) + up = midpoint - 1 + else if (smaller_than(pair, value)) + down = midpoint + 1 + else + return (midpoint) + } + + return (-1) +} + +recippar <- function (edges) { + data = edges[order(V1, V2),] + for(x in 1:nrow(data)) + found <- binary_search(data, as.numeric(data[x])) + if(found != -1) { + if(found > x) { + data = data[-found,] + data = data[-x,] + } + else { + data = data[-x,] + data = data[-found,] + } + count = count + 1 + } + else + data = data[-x,] + + count / 2 +} + +recippar(data)