Divide and Conquer For Nearest Neighbor Problem

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

Divide and Conquer for Nearest

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

Can we extend the divide and conquer idea to


2 dimensions?
Closest Pair Problems
• 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.
Closest Pair Problem (Input)
Closest Pair Problem(Output)


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

• Calculate the distance between all the pairs and


return the pair of points with the minimum
distance.
1 D Version
• How do we solve this problem in 1
dimensions?
– Sort the number and walk from left to right to
find minimum gap
Divide and Conquer
• Divide and conquer has a chance to do better than
O(n2).

• Divide the problems in two subproblems, conquer


and then combine

• Divide into two groups with almost equal points ..


How?
Closest Pair Problem

Sort P on basis of x-cord to get list to get 𝑃𝑥


𝑛
Median is the approximately the x cord of
2
Time needed nlog n
Divide and Conquer for the
Closest Pair Problem

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

Conquer: Recursively solve Q and R


Combination I
Q R

2

Takes the smaller one of 1 , 2 :  = min(1 , 2 )


Combination II
Is there a point in L and a point in R whose distance
is smaller than  ?

Q R

Takes the smaller one of 1 , 2 :  = min(1 , 2 )


Combination II
• If the answer is “no” then we are done!!!
• If the answer is “yes” then the closest such
pair forms the closest pair for the entire set
• Why????

• How do we determine this?


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.
– 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

• Why cannt we have a situation like below??


• i.e one box has more than one point

• 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

But then this means that 𝜹 is not the min distance 28


½
Returned from left and right halfs
𝛿
2
Which is not true hence our original assumption was wrong 25

And two points cannot lie in the same box

22
Now that we know
points in the strip is
sparse.

How do we use this


information to find “cross
border” pairs in time less
than 𝜽 𝒏 𝟐
23
𝜃 𝑛 𝑀𝑒𝑡ℎ𝑜𝑑 𝑡𝑜 𝑓𝑖𝑛𝑑 𝐶𝑟𝑜𝑠𝑠 𝐵𝑜𝑟𝑑𝑒𝑟 𝑃𝑎𝑖𝑟𝑠

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???

Def. Let si be the point in the 2-strip, with


the ith smallest y-coordinate.

Claim. If |i – j|  12, then the distance between 39 j


si and sj is at least .
31
Pf.
 No two points lie in same ½-by-½ box.
 Two points at least 2 rows apart ½
have distance  2(½). ▪ 2 rows
30
29 ½

i 27
28 ½

26
25

  26
Closest Path Pseudo Code

Code in Main Function/calling function

1. P is the set of points


2. 𝑃𝑥 is P sorted by x coordinates
3. 𝑃𝑦 is P sorted by y coordinates
4. 𝑝0∗ and 𝑝1∗ and are the two points with the min
distance
27
Closest Path Pseudo Code

Base Case

Recursive
Calls

28
Closest Pair Pseudo code

Search for Cross Border Pair

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

Total time = Algo time + sorting time

𝑇 𝑛 = 𝜃 𝑛𝑙𝑜𝑔𝑛 + 𝜃 𝑛𝑙𝑜𝑔𝑛
i.e
𝑇 𝑛 = 𝜃(𝑛𝑙𝑜𝑔𝑛)

32

You might also like