Closest-pair problem: Difference between revisions
Content added Content deleted
(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: | Line 1,859: | ||
} |
} |
||
xp <- x[order(x[,"x"]),] |
xp <- x[order(x[,"x"]),] |
||
⚫ | |||
.cpdandc.rec <- function(xp,yp) |
.cpdandc.rec <- function(xp,yp) |
||
{ |
{ |
||
Line 1,871: | Line 1,870: | ||
xl <- xp[1:floor(n/2),] |
xl <- xp[1:floor(n/2),] |
||
xr <- xp[(floor(n/2)+1):n,] |
xr <- xp[(floor(n/2)+1):n,] |
||
cpl <- .cpdandc.rec(xl) |
|||
cpr <- .cpdandc.rec(xr) |
|||
⚫ | |||
⚫ | |||
cpr <- .cpdandc.rec(xr,yr) |
|||
if (cpl$d<cpr$d) cp <- cpl else cp <- cpr |
if (cpl$d<cpr$d) cp <- cpl else cp <- cpr |
||
cp |
|||
⚫ | |||
ys <- yp[which(abs(xm - yp[,"x"]) < dmin),] |
|||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
xm <- xp[floor(n/2),"x"] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
{ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
{ |
{ |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
k <- k + 1 |
|||
⚫ | |||
{ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
⚫ | |||
} |
} |
||
cp |
|||
} |
} |
||
} |
} |
||
cp |
|||
.cpdandc.rec(xp,yp) |
|||
} |
} |
||