function bruteForceClosestPair(sequence s) atom {x1,y1} = s[1], {x2,y2} = s[2], dx = x1-x2, dy = y1-y2, mind = dx*dx+dy*dy sequence minp = s[1..2] for i=1 to length(s)-1 do {x1,y1} = s[i] for j=i+1 to length(s) do {x2,y2} = s[j] dx = x1-x2 dx = dx*dx if dx1 equidistant pairs exist, brute and dc may well return different pairs; -- it is only a problem if they decide to return different minimum distances.) atom {{x1,y1},{x2,y2}} = sort(points) printf(1,"Closest pair: {%f,%f} {%f,%f}, distance=%f (%3.2fs)\n",{x1,y2,x2,y2,d,time()-t0}) t0 = time() constant X = 1, Y = 2 sequence xP = sort(testset) function byY(sequence p1, p2) return compare(p1[Y],p2[Y]) end function sequence yP = custom_sort(routine_id("byY"),testset) function distsq(sequence p1,p2) atom {x1,y1} = p1, {x2,y2} = p2 x1 -= x2 y1 -= y2 return x1*x1 + y1*y1 end function function closestPair(sequence xP, yP) -- where xP is P(1) .. P(N) sorted by x coordinate, and -- yP is P(1) .. P(N) sorted by y coordinate (ascending order) integer N, midN, k, nS sequence xL, xR, yL, yR, pairL, pairR, pairMin, yS, cPair atom xm, dL, dR, dmin, closest N = length(xP) if length(yP)!=N then ?9/0 end if -- (sanity check) if N<=3 then return bruteForceClosestPair(xP) end if midN = floor(N/2) xL = xP[1..midN] xR = xP[midN+1..N] xm = xP[midN][X] yL = {} yR = {} for i=1 to N do if yP[i][X]<=xm then yL = append(yL,yP[i]) else yR = append(yR,yP[i]) end if end for {dL, pairL} = closestPair(xL, yL) {dR, pairR} = closestPair(xR, yR) {dmin, pairMin} = {dR, pairR} if dL