Closest Pair
Closest Pair
– Time Needed:
𝑛
𝑇 𝑛 = 𝑎𝑇 +𝐶 𝑛 +𝐷 𝑛
𝑏
Main Motivations
Can we extend the divide and conquer idea to
2 dimensions?
Closest Pair Problem
• Input:
– A set of points P = {p1,…, pn} in two dimensions
• Output:
– The pair of points pi, pj that minimize the
Euclidean distance between them.
2 2
𝑑= 𝑥𝑖 − 𝑥𝑗 + 𝑦𝑖 − 𝑦𝑗
Example of calculating
Eucledean
distance
Closest Pair Problem (Input)
n-random points
Closest Pair Problem(Output)
Our goal is to find the two
points that are closest to
each other.
Simpleton
Algorithm
Simple Divide and Conquer
• O(n2) time algorithm is easy
• Assumptions:
– No two points have the same x-coordinates
– No two points have the same y-coordinates
?
Closest Pair Problem (Input)
Closest Pair Problem
Divide by x-median
Divide and Conquer for the
Closest Pair Problem
Divide by x-median
Conquer
Look at left side find the closest pair
Look at right side find the closest pair
L R
1
2
L R
2
L R
Find closest pair with one point in each side, assuming that distance < .
Observation: only need to consider points within of line L.
– Important to understand this
– ?
21
= min(12, 21)
12
27
Number of Points in the Strip
At a surface level it seems that there can be 𝜃(𝑛) points in the strip
Meaning we will again need 𝜃 𝑛2 comparisons in worst case ( see below)
21
= min(12, 21)
12
28
Sparse Points in the Strip
However when we look closely we see that the points in the strip are
sparse
HOW
𝛿 𝛿
if we divide the strip intro squares of size × then there is only one
2 2
point per each square 31 39
30
28
½
𝛿
26 2
25
29
Sparse Points in the Strip
• Lets assume the opposite that two points lie in the same box
• Distance is max if points are on opposite corners
31
• This means the two points has a distance 39
𝑑 = 0.707𝛿 ½
Which is less than 𝛿
30
26
30
Now that we know points
in the strip is sparse.
31
𝜃 𝑛 𝑀𝑒𝑡ℎ𝑜𝑑 𝑡𝑜 𝑓𝑖𝑛𝑑 𝐶𝑟𝑜𝑠𝑠 𝐵𝑜𝑟𝑑𝑒𝑟 𝑃𝑎𝑖𝑟𝑠
Find closest pair with one point in each side, assuming that distance < .
Observation: only need to consider points within of line L.
Sort points in 2-strip by their y coordinate.
L
7
5
4 21
= min(12, 21)
12 3
32
Closest Pair of Points
Find closest pair with one point in each side, assuming that distance < .
Observation: only need to consider points within of line L.
Sort points in 2-strip by their y coordinate.
Only check distances of those within 11 positions in sorted list!
L
7
5
4 21
= min(12, 21)
12 3
33
Why Check just 11 Points for each Point???
i 27
28 ½
26
25
34
Closest Path Pseudo Code
Base Case
Recursive
Calls
36
Closest Pair Pseudo code
37
Closest Pair Pseudo code
Next, we just return the pair of points that has the min distance (
compare distance of 𝛿 with the min cross boundary distance
38
Running Time
𝑛
𝑇 𝑛 = 2𝑇 + 𝐶𝑜𝑚𝑏𝑖𝑛𝑒 𝑇𝑖𝑚𝑒 + 𝐷𝑖𝑣𝑖𝑑𝑒 𝑇𝑖𝑚𝑒
2
Divide time
Once we have sorted the points in main finding the median is constant
time
Combine Time
𝜃 𝑛
We need need constant time for each point in strip S
𝑛
Hence we get 𝑇 𝑛 = 2𝑇 +𝜃 𝑛
2
Which we have seen in merge sort comes out to be
𝑇 𝑛 = 𝜃 𝑛𝑙𝑜𝑔 𝑛
39
Total Time
𝑇 𝑛 = 𝜃 𝑛𝑙𝑜𝑔𝑛 + 𝜃 𝑛𝑙𝑜𝑔𝑛
i.e
𝑇 𝑛 = 𝜃(𝑛𝑙𝑜𝑔𝑛)
40