Docsity

Algorithms: Divide and Conquer, Lecture notes of Algorithms and Programming

Partial preview of the text

Download Algorithms: Divide and Conquer and more Lecture notes Algorithms and Programming in PDF only on Docsity!

CSE 521

Algorithms:

Divide and Conquer

Larry Ruzzo

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.

Moral 3: unbalanced division less good:

(.1n) 2

  • (.9n) 2
  • n = .82n 2
  • n The 18% savings compounds significantly if you carry recursion to more levels, actually giving O(nlogn), but with a bigger constant. So worth doing if you can’t get 50-50 split, but balanced is better if you can. This is intuitively why Quicksort with random splitter is good – badly unbalanced splits are rare, and not instantly fatal. (1) 2
  • (n-1) 2
  • n = n 2
  • 2n + 2 + n Little improvement here.

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

  • definitely not Euclidean distance!) Must look at all n choose 2 pairwise distances, else  any one you didn’t check might be the shortest. Also true for Euclidean distance in 1-2 dimensions? (… and all the rest of the (n 2 ) edges…) 13

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

Divide. Sub-divide region into 4 quadrants.

closest pair of points

Algorithm.

Divide: draw vertical line L with ≈ n/2 points on each side. L

closest pair of points

Algorithm.

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

Find closest pair with one point in each side,

assuming distance < δ.

12 21 δ = min(12, 21) L

closest pair of points

Find closest pair with one point in each side,

assuming distance < δ.

Observation: suffices to consider points within δ of line L. 22 12 21 δ L δ = min(12, 21)