Professional Documents
Culture Documents
Divide and Conquer For Nearest Neighbor Problem
Divide and Conquer For Nearest Neighbor Problem
Divide and Conquer For Nearest Neighbor Problem
Neighbor Problem
Read page 125-128 of Algorithm Design
book by Klienberg and Tardos (uploaded)
Divide and Conquer
• Divide the problem into a number of
sub-problems (similar to the original
problem but smaller);
• Conquer the sub-problems by solving them
recursively (if a sub-problem is small
enough, just solve it in a straightforward
manner.
• Combine the solutions to the sub-problems
into the solution for the original problem
Algorithm Design Paradigm I
• Solve smaller problems, and use solutions to the
smaller problems to solve larger ones
– Divide and Conquer
– Time Needed:
𝑛
𝑇 𝑛 = 𝑎𝑇 +𝐶 𝑛 +𝐷 𝑛
𝑏
Two Dimensional Divide and
Conquer
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
If median is a point
then by “convention”
consider it to lie in
either L or R
Divide by x-median
Divide
Q R
Divide by x-median
Conquer
Q R
1
2
2
Q 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
19
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
20
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 size2 × 2 then there is only one
point per each square 31 39
30
28
½
𝛿
26 2
25
21
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
22
Now that we know
points in the strip is
sparse.
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
24
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
25
Why Check just 11 Points for each Point???
i 27
28 ½
26
25
26
Closest Path Pseudo Code
Base Case
Recursive
Calls
28
Closest Pair Pseudo code
29
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
30
Running Time
𝑛
𝑇 𝑛 = 2𝑇 + 𝐶𝑜𝑚𝑏𝑖𝑛𝑒 𝑇𝑖𝑚𝑒 + 𝐷𝑖𝑣𝑖𝑑𝑒 𝑇𝑖𝑚𝑒
2
Divide time
Once we have sorted the points in main finding the median is constant
time
Combine Time
𝜃 𝑛
We need to check max 𝜃 𝑛 and for each point we need constant time
𝑛
Hence we get 𝑇 𝑛 = 2𝑇 +𝜃 𝑛
2
Which we have seen in merge sort comes out to be
𝑇 𝑛 = 𝜃 𝑛𝑙𝑜𝑔 𝑛
31
Total Time
𝑇 𝑛 = 𝜃 𝑛𝑙𝑜𝑔𝑛 + 𝜃 𝑛𝑙𝑜𝑔𝑛
i.e
𝑇 𝑛 = 𝜃(𝑛𝑙𝑜𝑔𝑛)
32