0% found this document useful (0 votes)
8 views40 pages

Closest Pair

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views40 pages

Closest Pair

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Closest Pair Problem

Read page 125-128 of Algorithm Design


book by Klienberg and Tardos (uploaded)
Computational Geometry
• Computational geometry is a branch of
computer science devoted to the study of
algorithms which can be stated in terms of
geometry.
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:
𝑛
𝑇 𝑛 = 𝑎𝑇 +𝐶 𝑛 +𝐷 𝑛
𝑏
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

• 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?
• Please tell me
• Sort the number and walk from left to right
to find minimum gap
In 1-D version we will have points in single dimension

Calculating distance between all pairs will take


Let's now move to deeper algorithm 2D
Algo
Simple Divide and Conquer
Let me try
Divide and
Conquer
Simple Closest Pair Algorithm
• Divide and conquer has a chance to do better than
O(n2).

• Divide the problems in two subproblems, conquer


and then combine
2D Divide and Conquer
First Problem to Solve
• Divide into two groups with almost equal points
.. How?
• Help me here

?
Closest Pair Problem (Input)
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 Don't use Insertion Sort ,Merge Sort as they have n^2 time complexity
As simplest algorithm already have n^2 time complexity.
All points have distinct
x coordinate and distinct Line will be on to of
y coordinate. Divide some point that
point will be at index n/2
That has the median x
coordinate
L R

Divide by x-median
Divide and Conquer for the
Closest Pair Problem

The median point is


by “convention”
considered it to lie in
either L or R

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

Conquer: Recursively solve Q and R


Combination I

L R

2

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


Are we done? Do we have a min
distance pair? T/F
Combination II
Is there a point in L and a point in R whose distance
is smaller than  ?

L R

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


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

 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

• 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

30
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 𝜽 𝒏𝟐

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

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
Proof.
 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

  34
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
35
Closest Path Pseudo Code

Base Case

Recursive
Calls

36
Closest Pair Pseudo code

Search for Cross Border Pair

Only check the next


11 points

We will need an extra


check to ensure that s
and s` are on opposite
sides

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

Total time = Algo time + sorting time

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

40

You might also like