Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

closest pair problem, Assignments of Algorithms and Programming

this document will help to understand the concepts of algorithms.

Typology: Assignments

2019/2020

Uploaded on 04/14/2020

barbie-duarah
barbie-duarah 🇮🇳

1 document

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 601: Design and Analysis of Algorithms Autumn 2020
Lecture WEEK 6 March 18, 2020
Arindam Karmakar Scribe:
1 Closest Pair problem
Given a list (x1, y1),(x2, y2),...,(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) = q(xixj)2+ (yiyj)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(n2) pair of points. Every distance
computation takes constant time. Thus requires O(n2) 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 piand pj.Can
divide-and-conquer approach help us here????
2 Divide-and-conquer approach for a SIMPLER case
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
pf3
pf4
pf5

Partial preview of the text

Download closest pair problem and more Assignments Algorithms and Programming in PDF only on Docsity!

CS 601: Design and Analysis of Algorithms Autumn 2020

Lecture WEEK 6 March 18, 2020

Arindam Karmakar Scribe:

1 Closest Pair problem

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 ) =

√ ( xixj )^2 + ( yiyj )^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????

2 Divide-and-conquer approach for a SIMPLER case

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 ){

  • if (| P | ≤ 3) try all pairs and return the distance of the closest one;
  • else
    • xmid = median( P ); - Partition the points P into two equal sized sets PL, PR by such that x < xmid for all xPL and x ′^ > xmid for all x ′^ ∈ PR. dL = ClosestP air ( PL ) dR = ClosestP air ( PR ) dLR = AcrossP air ( PL, PR )
  • return min( dL, dR, dLR ) }

PL xmid PR

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?

PL PR

dL

xmid

p 1 q 1

dLR

p 3 q 3

dR

p 2 q 2

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.

3 Some more QUESTIONS on 1-D closest pair

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

4 2-dimensional closest pair

We are now in a position to modify the 1-D algorithm to design the 2-D closest pair algorithm.

Divide by xmid

xmid xmid

xmid xmid

Recursively find the closest in left half

Recursively find the closest in right half Find the closest across pair

dL

dL dL

dR dR

dLR??

dLR??

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?

xmid

dL

dR

dLR??

dLR??

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.