



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
this document will help to understand the concepts of algorithms.
Typology: Assignments
1 / 5
This page cannot be seen from the preview
Don't miss anything!
CS 601: Design and Analysis of Algorithms Autumn 2020
Arindam Karmakar Scribe:
Given a list ( x 1 , y 1 ) , ( x 2 , y 2 ) ,... , ( xn, yn ) of points in the plane, find the pair of points ( p, q ) that are closest among all pair of points.
Figure 1: Closest pair in red
Here closest is measured in terms of Euclidean distance. The Euclidean distance between a pair of points pi = ( xi, yi ) and pj = ( xj , yj ) is defined as
dist ( pi, pj ) =
√ ( xi − xj )^2 + ( yi − yj )^2
.
The function dist ( , ) can be computed in O (1) time.
Obvious solution Check distance between every pair of points to find the closest one.
Time complexity We have to compute dist ( , ) between
( n 2
) = O ( n^2 ) pair of points. Every distance computation takes constant time. Thus requires O ( n^2 ) time.
Better solution To get a better solution we have to avoid checking distance for each and every pair. For example, in the above figure there is no need to check the distance between pi and pj. Can divide-and-conquer approach help us here????
Our objective find the closest pair for a 2-dimensional point set. Before solving that, we look into 1- dimensional case, where the points has only one coordinate; i.e, the points lie on a line (say x -axis). Then the problem becomes
The function dist ( , ) can be computed in O (1) time.
1-D version: Given n unsorted points { x 1 , x 2 ,... , xn } on the real line, find the closest pair.
Divide-and conquer technique: We divide the problem in smaller subproblems and try to find the solution of the original from the smaller ones. ClosestPair ( P ){
Figure 2: partition of P
Useful Idea: Consider another important observation for the 1-dimensional problem. Let ( p 1 , q 1 ) and ( p 2 , q 2 ) be the closest pairs in PL and PR respectively (computed recursively). See Figure 3 for better understanding. Let δ be the smallest separation among the left and right half, i.e, δ = min( dL, dR ). If the across pair, i.e. ( p 3 , q 3 ) is the overall closest, then p 3 , q 3 must be within δ of xmid. WHY?
Figure 3: Useful Idea to be needed for 2D version
Because if any of p 3 or q 3 is outside the slab (the shaded region in Figure 3), the distance between p 3 and q 3 will be greater than δ , i.e, dLR > min( dL, dR ). So across pair cannot be the closest.
Recall that the input set of points are on a line. Two points ( p, q ) are adjacent/neighbors of each other if there are no points in between them. In Figure 3, ( p 1 , q 1 ) and ( p 2 , q 2 ) are neighbors, but ( q 1 , p 3 ) are
We are now in a position to modify the 1-D algorithm to design the 2-D closest pair algorithm.
Figure 4: Closest pair in red
We divide the points in two halves PL and PR with repect to xmid. What you observe that there are three possible cases as in 1-dimension. Closest pair can be:
Case 1: from the left half i.e, PL
Case 2: fromthe right half i.e, PR
Case 3: both the halves, i.e, one point from PL and the other from PR.
Case 1 and Case 2 are solved recursively. Next we find δ = min( dL, dR ). The next task is to find the closest across pair of points which are at distance less than δ. In 1-dimension, finding the across pair is easy. Because there can be at most one across pair of points within the slab of length 2 δ where δ = min( dL, dR ). But in 2-dimensional case it is not so easy; we may have to test multiple pair of points across the cut. See figure 4, where we have at least two pairs to test. The following figure 5 will make the things clear.
The points inside the slab (shaded region are the ones which may be considered for selection of across pair category. Points 4 and 5 don’t fall in this criteria. Because they cannot pair up with any point from the other side within distance δ. So we should cocentrate only on the points within the slab. Now the question is which pair is the one?
Unlike the 1-D case, there is no special criteria like leftmost or rightmost. In figure 5), points 8 and 2 are the right most and leftmost points in PL and PR but they are not the closest. So we have to check every pair of points inside the slab. But how expensive will be that?
Figure 5: Closest pair in red
Assignment
i) How many pairs of pints inside the slab we have to check to find the closest one? Derive the number of pairs (in the worst case) as a function f ( n ) where n is the total number of points.
ii) Give an example input for the worst case scenario.
iii) What will be the time complexity of the algorithm if f ( n ) is the number of across pairs we need to check? Derive the recurrence and solve the equation using master’s theorem.