Jump to content

Closest-pair problem: Difference between revisions

Optimised divide and conquer method by checking yp only after running through all of xp
(New method based on divide and conquer pseudocode.)
(Optimised divide and conquer method by checking yp only after running through all of xp)
Line 1,859:
}
xp <- x[order(x[,"x"]),]
yp <- x[order(x[,"y"]),]
.cpdandc.rec <- function(xp,yp)
{
Line 1,871 ⟶ 1,870:
xl <- xp[1:floor(n/2),]
xr <- xp[(floor(n/2)+1):n,]
xmcpl <- xp[floor.cpdandc.rec(n/2xl),"x"]
ylcpr <- yp[which.cpdandc.rec(yp[,"x"] <= xmxr),]
yr <- yp[which(yp[,"x"] > xm),]
cpl <- .cpdandc.rec(xl,yl)
cpr <- .cpdandc.rec(xr,yr)
if (cpl$d<cpr$d) cp <- cpl else cp <- cpr
dmin <- cp$d
}
ys <- yp[which(abs(xm - yp[,"x"]) < dmin),]
}
nys <- dim(ys)[1]
cpl <- .cpdandc.rec(xl,ylxp)
if (!is.null(nys) && nys > 1)
yp <- x[order(x[,"y"]),]
xm <- xp[floor(n/2),"x"]
yr ys <- yp[which(abs(xm - yp[,"x"]) >< xmcp$d),]
nys <- dim(ys)[1]
if (!is.null(nys) && nys > 1)
{
for (i in 1:(nys-1))
{
k <- ki + 1
while (k <= nys && ys[i,"y"] - ys[k,"y"] < dmincp$d)
{
d <- sqrt((ys[k,"x"]-ys[i,"x"])^2 + (ys[k,"y"]-ys[i,"y"])^2)
for (i in 1:(nys-1))
if (d < cp$d) cp <- list(p1=ys[i,],p2=ys[k,],d=d)
{
k <- ik + 1
while (k <= nys && ys[i,"y"] - ys[k,"y"] < dmin)
{
d <- sqrt((ys[k,"x"]-ys[i,"x"])^2 + (ys[k,"y"]-ys[i,"y"])^2)
if (d < cp$d) cp <- list(p1=ys[i,],p2=ys[k,],d=d)
k <- k + 1
}
}
}
cp
}
}
cp
.cpdandc.rec(xp,yp)
}
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.