
































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































































Thanks to Paul Beame, Kevin Wayne for some slides
algorithm design paradigms: divide and conquer Outline: General Idea Review of Merge Sort Why does it work? Importance of balance Importance of super-linear growth Some interesting applications Closest points Integer Multiplication Finding & Solving Recurrences
merge sort MS(A: array[1..n]) returns array[1..n] { If(n=1) return A; New U:array[1:n/2] = MS(A[1..n/2]); New L:array[1:n/2] = MS(A[n/2+1..n]); Return(Merge(U,L)); } Merge(U,L: array[1..n]) { New C: array[1..2n]; a=1; b=1; For i = 1 to 2n C[i] = “smaller of U[a], L[b] and correspondingly a++ or b++”; Return C; } A U C L split sort merge
why balanced subdivision? Alternative "divide & conquer" algorithm: Sort n- Sort last 1 Merge them T(n) = T(n-1)+T(1)+3n for n ≥ 2 T(1) = 0 Solution: 3n + 3(n-1) + 3(n-2) … = Θ(n 2 )
d&c approach, cont. Moral 1: “two halves are better than a whole” Two problems of half size are better than one full-size problem, even given O(n) overhead of recombining, since the base algorithm has super-linear complexity. Moral 2: “If a little's good, then more's better” Two levels of D&C would be almost 4 times faster, 3 levels almost 8, etc., even though overhead is growing. Best is usually full recursion down to some small constant size (balancing "work" vs "overhead"). In the limit: you’ve just rediscovered mergesort!
d&c approach, cont.
(.1n) 2
A Divide & Conquer Example: Closest Pair of Points
closest pair of points: non-geometric version Given n points and arbitrary distances between them, find the closest pair. (E.g., think of distance as airfare
closest pair of points: 2 dimensional version Closest pair. Given n points in the plane, find a pair with smallest Euclidean distance between them. Fundamental geometric primitive. Graphics, computer vision, geographic information systems, molecular modeling, air traffic control. Special case of nearest neighbor, Euclidean MST, Voronoi. Brute force. Check all pairs of points p and q with Θ(n^2 ) comparisons. 1-D version. O(n log n) easy if points are on a line. Assumption. No two points have same x coordinate. Just to simplify presentation fast closest pair inspired fast algorithms for these problems
closest pair of points. 2d, Euclidean distance: 1 st try
closest pair of points
Divide: draw vertical line L with ≈ n/2 points on each side. L
closest pair of points
Divide: draw vertical line L with ≈ n/2 points on each side. Conquer: find closest pair on each side, recursively. 12 21 L
closest pair of points
12 21 δ = min(12, 21) L
closest pair of points
Observation: suffices to consider points within δ of line L. 22 12 21 δ L δ = min(12, 21)