Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions P.R
Original file line number Diff line number Diff line change
@@ -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)