Presentation is loading. Please wait.

Presentation is loading. Please wait.

Closest Pair Design an algorithm to find the closest pair of points.

Similar presentations


Presentation on theme: "Closest Pair Design an algorithm to find the closest pair of points."— Presentation transcript:

1 Closest Pair Design an algorithm to find the closest pair of points

2 Closest Pair Designing algorithms is more of an art than a science.

3 Closest Pair Visually this problem is very simple to solve, but what if I gave you 1 million points?

4 Closest Pair What if I asked you to solve this problem for 1 million different cases?

5 Closest Pair What is the input? What is the output? What are the precise step-by-step instructions? Will the algorithm terminate? Will it produce correct answers all the time? How much time will it take to solve? How much memory is required to solve the problem?

6 Closest Pair The input: 2D point, a pair values (x, y) –Point p; –p.x; –p.y; An array of n Points –Point P[n] –P[0], P[1], P[2], P[3], …, P[n-1]

7 Closest Pair The output: Two points P[a] and P[b] where the distance between P[a] and P[b] is minimum among all possible pairs of points. Sometimes describing the output with precision gives you clues about how to solve the problem.

8 Closest Pair 1. How do you compute the distance between to points? 2. How do you find a distance is minimum among all possible pairs of points?

9 Closest Pair dist(a,b){ d = sqrt[ pow((a.x – b.x),2) + pow((a.y – b.y),2) ]; return d; }

10 Closest Pair d = dist(P[0], P[1]); Compare all possible pairs of points, i.e., compare P[0] with P[1], P[2], P[3], …, P[n-1] compare P[1] with P[2], P[3], P[4], …, P[n-1] compare P[2] with P[3], P[4], P[5], …, P[n-1] compare P[3] with P[4], P[5], P[6], …, P[n-1] … compare P[n-3] with P[n-2], P[n-1] compare P[n-2] with P[n-1]

11 Closest Pair ClosestPair(P[ ], n) { min_dist = dist(P[0],P[1]); for (x = 1 to n-1) { d = dist(P[0], P[x]); if (d < min_dist) { min_dist = d; } return min_dist; } This is not correct!

12 Closest Pair ClosestPair(P[ ], n) { min_dist = dist(P[0],P[1]); for (y = 0 to n-1) { for (x = 1 to n-1) { d = dist(P[y], P[x]); if (d < min_dist) { min_dist = d; }} return min_dist; }

13 Closest Pair Compare P[a] and P[b] a b 0123...n-1 0 1 2 3... n-1

14 Closest Pair Compare P[a] and P[b] a b 0123...n-1 0 X 1 X 2 X 3 X... X n-1 X

15 Closest Pair Compare P[a] and P[b] a b 0123...n-1 0 X 1 X 2 X 3 X... X n-1 X

16 Closest Pair ClosestPair(P[ ], n) { min_dist = dist(P[0],P[1]); for (y = 0 to n-1) { for (x = y+1 to n-1) { d = dist(P[y], P[x]); if (d < min_dist) { min_dist = d; }} return min_dist; }

17 Closest Pair This matrix is n x n = n 2 entries. How many of these n 2 entries must we compute? a b 0123...n-1 0 1 2 3... n-1

18 Summations n-1 + n-2 + n-3 +...+ 3 + 2 + 1


Download ppt "Closest Pair Design an algorithm to find the closest pair of points."

Similar presentations


Ads by Google