Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.

Similar presentations


Presentation on theme: "1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved."— Presentation transcript:

1 1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.

2 2 Divide and Conquer Algorithms - Merge Sort - Counting - Closest Pair of Points - Matrix Multiplication

3 3 Divide-and-Conquer Divide-and-conquer. n Break up problem into several parts. n Solve each part recursively. n Combine solutions to sub-problems into overall solution. Most common usage. n Break up problem of size n into two equal parts of size ½n. n Solve two parts recursively. n Combine two solutions into overall solution in linear time. Consequence. n Brute force: n 2. n Divide-and-conquer: n log n. Divide et impera. Veni, vidi, vici. - Julius Caesar

4 5.1 Mergesort

5 5 Obvious sorting applications. List files in a directory. Organize an MP3 library. List names in a phone book. Display Google PageRank results. Problems become easier once sorted. Find the median. Find the closest pair. Binary search in a database. Identify statistical outliers. Find duplicates in a mailing list. Non-obvious sorting applications. Data compression. Computer graphics. Interval scheduling. Computational biology. Minimum spanning tree. Supply chain management. Simulate a system of particles. Book recommendations on Amazon. Load balancing on a parallel computer.... Sorting Sorting. Given n elements, rearrange in ascending order.

6 6 Mergesort Mergesort. n Divide array into two halves. n Recursively sort each half. n Merge two halves to make sorted whole. merge sort divide ALGORITHMS ALGORITHMS AGLORHIMST AGHILMORST Jon von Neumann (1945) O(n) 2T(n/2) O(1)

7 7 Merging Merging. Combine two pre-sorted lists into a sorted whole. How to merge efficiently? n Linear number of comparisons. n Use temporary array. Challenge for the bored. In-place merge. [Kronrud, 1969] AGLORHIMST AGHI using only a constant amount of extra storage

8 8 A Useful Recurrence Relation Def. T(n) = number of comparisons to mergesort an input of size n. Mergesort recurrence. Solution. T(n) = O(n log 2 n). Assorted proofs. We describe several ways to prove this recurrence. Initially we assume n is a power of 2 and replace  with =.

9 9 Proof by Recursion Tree T(n) T(n/2) T(n/4) T(2) n T(n / 2 k ) 2(n/2) 4(n/4) 2 k (n / 2 k ) n/2 (2)... log 2 n n log 2 n

10 10 Proof by Telescoping Claim. If T(n) satisfies this recurrence, then T(n) = n log 2 n. Pf. For n > 1: assumes n is a power of 2

11 11 Proof by Induction Claim. If T(n) satisfies this recurrence, then T(n) = n log 2 n. Pf. (by induction on n) n Base case: n = 1. n Inductive hypothesis: T(n) = n log 2 n. n Goal: show that T(2n) = 2n log 2 (2n). assumes n is a power of 2

12 12 Analysis of Mergesort Recurrence Claim. If T(n) satisfies the following recurrence, then T(n)  n  lg n . Pf. (by induction on n) n Base case: n = 1. n Define n 1 =  n / 2 , n 2 =  n / 2 . n Induction step: assume true for 1, 2,..., n–1. log 2 n

13 13 auxiliary array smallest AGLORHIMST Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. A

14 14 auxiliary array smallest AGLORHIMST A Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. G

15 15 auxiliary array smallest AGLORHIMST AG Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. H

16 16 auxiliary array smallest AGLORHIMST AGH Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. I

17 17 auxiliary array smallest AGLORHIMST AGHI Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. L

18 18 auxiliary array smallest AGLORHIMST AGHIL Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. M

19 19 auxiliary array smallest AGLORHIMST AGHILM Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. O

20 20 auxiliary array smallest AGLORHIMST AGHILMO Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. R

21 21 auxiliary array first half exhausted smallest AGLORHIMST AGHILMOR Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. S

22 22 auxiliary array first half exhausted smallest AGLORHIMST AGHILMORS Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done. T

23 23 auxiliary array first half exhausted second half exhausted AGLORHIMST AGHILMORST Merging Merge. n Keep track of smallest element in each sorted half. n Insert smallest of two elements into auxiliary array. n Repeat until done.

24 5.3 Counting Inversions

25 25 Music site tries to match your song preferences with others. n You rank n songs. n Music site consults database to find people with similar tastes. Similarity metric: number of inversions between two rankings. n My rank: 1, 2, …, n. n Your rank: a 1, a 2, …, a n. n Songs i and j inverted if i a j. Brute force: check all  (n 2 ) pairs i and j. You Me 14325 13245 ABCDE Songs Counting Inversions Inversions 3-2, 4-2

26 26 Applications Applications. n Voting theory. n Collaborative filtering. n Measuring the "sortedness" of an array. n Sensitivity analysis of Google's ranking function. n Rank aggregation for meta-searching on the Web. n Nonparametric statistics (e.g., Kendall's Tau distance).

27 27 Counting Inversions: Divide-and-Conquer Divide-and-conquer. 481021512113769

28 28 Counting Inversions: Divide-and-Conquer Divide-and-conquer. n Divide: separate list into two pieces. 481021512113769 481021512113769 Divide: O(1).

29 29 Counting Inversions: Divide-and-Conquer Divide-and-conquer. n Divide: separate list into two pieces. n Conquer: recursively count inversions in each half. 481021512113769 481021512113769 5 blue-blue inversions8 green-green inversions Divide: O(1). Conquer: 2T(n / 2) 5-4, 5-2, 4-2, 8-2, 10-2 6-3, 9-3, 9-7, 12-3, 12-7, 12-11, 11-3, 11-7

30 30 Counting Inversions: Divide-and-Conquer Divide-and-conquer. n Divide: separate list into two pieces. n Conquer: recursively count inversions in each half. n Combine: count inversions where a i and a j are in different halves, and return sum of three quantities. 481021512113769 481021512113769 5 blue-blue inversions8 green-green inversions Divide: O(1). Conquer: 2T(n / 2) Combine: ??? 9 blue-green inversions 5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7 Total = 5 + 8 + 9 = 22.

31 31 13 blue-green inversions: 6 + 3 + 2 + 2 + 0 + 0 Counting Inversions: Combine Combine: count blue-green inversions n Assume each half is sorted. n Count inversions where a i and a j are in different halves. n Merge two sorted halves into sorted whole. Count: O(n) Merge: O(n) 101418193716172325211 710111423181923251617 6322 0 0 to maintain sorted invariant

32 32 Counting Inversions: Implementation Pre-condition. [Merge-and-Count] A and B are sorted. Post-condition. [Sort-and-Count] L is sorted. Sort-and-Count(L) { if list L has one element return 0 and the list L Divide the list into two halves A and B (r A, A)  Sort-and-Count(A) (r B, B)  Sort-and-Count(B) (r B, L)  Merge-and-Count(A, B) return r = r A + r B + r and the sorted list L }

33 33 Algorithm 5.2, page 225

34 34 Algorithm 5.1, page 224

35 35 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves auxiliary array Total: i = 6

36 36 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. i = 6 two sorted halves 2 auxiliary array Total: 6 6

37 37 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 2 auxiliary array i = 6 Total: 6 6

38 38 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 23 auxiliary array i = 6 Total: 6 6

39 39 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 23 auxiliary array i = 5 Total: 6 6

40 40 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 723 auxiliary array i = 5 Total: 6 6

41 41 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 723 auxiliary array i = 4 Total: 6 6

42 42 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 71023 auxiliary array i = 4 Total: 6 6

43 43 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 71023 auxiliary array i = 3 Total: 6 6

44 44 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 7101123 auxiliary array i = 3 Total: 6 + 3 63

45 45 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 7101123 auxiliary array i = 3 Total: 6 + 3 63

46 46 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 710111423 auxiliary array i = 3 Total: 6 + 3 63

47 47 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 710111423 auxiliary array i = 2 Total: 6 + 3 63

48 48 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 71011142316 auxiliary array i = 2 Total: 6 + 3 + 2 632

49 49 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 71011142316 auxiliary array i = 2 Total: 6 + 3 + 2 632

50 50 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 7101114231617 auxiliary array i = 2 Total: 6 + 3 + 2 + 2 6322

51 51 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 7101114231617 auxiliary array i = 2 Total: 6 + 3 + 2 + 2 6322

52 52 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 710111423181617 auxiliary array i = 2 Total: 6 + 3 + 2 + 2 6322

53 53 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 710111423181617 auxiliary array i = 1 Total: 6 + 3 + 2 + 2 6322

54 54 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 71011142318191617 auxiliary array i = 1 Total: 6 + 3 + 2 + 2 6322

55 55 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 71011142318191617 auxiliary array i = 0 Total: 6 + 3 + 2 + 2 first half exhausted 6322

56 56 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 7101114231819231617 auxiliary array i = 0 Total: 6 + 3 + 2 + 2 + 0 6322 0

57 57 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 7101114231819231617 auxiliary array i = 0 Total: 6 + 3 + 2 + 2 + 0 6322 0

58 58 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 710111423181923251617 auxiliary array i = 0 Total: 6 + 3 + 2 + 2 + 0 + 0 6322 0 0

59 59 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 710111423181923251617 auxiliary array i = 0 Total: 6 + 3 + 2 + 2 + 0 + 0 = 13 6322 0 0

60 60 Closest Pair of Points Closest pair. Given n points in the plane, find a pair with smallest Euclidean distance between them. Fundamental geometric primitive. n Graphics, computer vision, geographic information systems, molecular modeling, air traffic control. n 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. to make presentation cleaner fast closest pair inspired fast algorithms for these problems

61 61 Closest Pair of Points: First Attempt Divide. Sub-divide region into 4 quadrants. L

62 62 Closest Pair of Points: First Attempt Divide. Sub-divide region into 4 quadrants. Obstacle. Impossible to ensure n/4 points in each piece. L

63 63 Closest Pair of Points Algorithm. n Divide: draw vertical line L so that roughly ½n points on each side. L

64 64 Closest Pair of Points Algorithm. n Divide: draw vertical line L so that roughly ½n points on each side. n Conquer: find closest pair in each side recursively. 12 21 L

65 65 Closest Pair of Points Algorithm. n Divide: draw vertical line L so that roughly ½n points on each side. n Conquer: find closest pair in each side recursively. n Combine: find closest pair with one point in each side. n Return best of 3 solutions. 12 21 8 L seems like  (n 2 )

66 66 Closest Pair of Points Find closest pair with one point in each side, assuming that distance < . 12 21  = min(12, 21) L

67 67 Closest Pair of Points Find closest pair with one point in each side, assuming that distance < . n Observation: only need to consider points within  of line L. 12 21  L  = min(12, 21)

68 68 12 21 1 2 3 4 5 6 7  Closest Pair of Points Find closest pair with one point in each side, assuming that distance < . n Observation: only need to consider points within  of line L. n Sort points in 2  -strip by their y coordinate. L  = min(12, 21)

69 69 12 21 1 2 3 4 5 6 7  Closest Pair of Points Find closest pair with one point in each side, assuming that distance < . n Observation: only need to consider points within  of line L. n Sort points in 2  -strip by their y coordinate. n Only check distances of those within 11 positions in sorted list! L  = min(12, 21)

70 70 Closest Pair of Points Def. Let s i be the point in the 2  -strip, with the i th smallest y-coordinate. Claim. If |i – j|  12, then the distance between s i and s j is at least . Pf. n No two points lie in same ½  -by-½  box. n Two points at least 2 rows apart have distance  2(½  ). ▪ Fact. Still true if we replace 12 with 7.  27 29 30 31 28 26 25  ½½ 2 rows ½½ ½½ 39 i j

71 71 Closest Pair Algorithm Closest-Pair(p 1, …, p n ) { Compute separation line L such that half the points are on one side and half on the other side.  1 = Closest-Pair(left half)  2 = Closest-Pair(right half)  = min(  1,  2 ) Delete all points further than  from separation line L Sort remaining points by y-coordinate. Scan points in y-order and compare distance between each point and next 11 neighbors. If any of these distances is less than , update . return . } O(n log n) 2T(n / 2) O(n) O(n log n) O(n)

72 72 Algorithm 5.3, page 230

73 73 Closest Pair of Points: Analysis Running time. Q. Can we achieve O(n log n)? A. Yes. Don't sort points in strip from scratch each time. n Each recursive returns two lists: all points sorted by y coordinate, and all points sorted by x coordinate. n Sort by merging two pre-sorted lists.

74 74 Summary and Analysis of the 2-D Algorithm Closest Pair of a set of points: 1. Divide the set into two equal sized parts by the line l, and recursively compute the minimal distance in each part. 2. Let d be the minimal of the two minimal distances. 3. Eliminate points that lie farther than d apart from l 4. Sort the remaining points according to their y-coordinates 5. Scan the remaining points in the y order and compute the distances of each point to its five neighbors. 6. If any of these distances is less than d then update d.

75 75 Analysis - Step 2 takes O(1) time - Step 3 takes O(n) time - Step 4 is a sort that takes O(nlogn) time - Step 5 takes O(n) time - Step 6 takes O(1) time The merging of the sub-solutions is dominated by the sorting at step 4, and hence takes O(nlogn) time. This must be repeated once for each level of recursion in the divide-and-conquer algorithm, hence the whole of algorithm ClosestPair takes O(logn*nlogn) = O(nlog2n) time.

76 76 Improvement Improving the Algorithm We can improve on this algorithm slightly by reducing the time it takes to achieve the y-coordinate sorting in Step 4. This is done by asking that the recursive solution computed in Step 1 returns the points in sorted order by their y coordinates. This will yield two sorted lists of points which need only be merged (a linear time operation) in Step 4 in order to yield a complete sorted list. Hence the revised algorithm involves making the following changes: Step 1: Divide the set into..., and recursively compute the distance in each part, returning the points in each set in sorted order by y- coordinate. Step 4: Merge the two sorted lists into one sorted list in O(n) time. Hence the merging process is now dominated by the linear time steps thereby yielding an O(nlogn) algorithm for finding the closest pair of a set of points in the plane.

77 5.5 Integer Multiplication

78 78 Integer Arithmetic Add. Given two n-digit integers a and b, compute a + b. n O(n) bit operations. Multiply. Given two n-digit integers a and b, compute a × b. n Brute force solution:  (n 2 ) bit operations. 1 0111 1101+ 0101 111 0101 0111 1000 10111 Add Multiply

79 79 To multiply two n-digit integers: n Multiply four ½n-digit integers. n Add two ½n-digit integers, and shift to obtain result. Divide-and-Conquer Multiplication: Warmup assumes n is a power of 2

80 80 To multiply two n-digit integers: n Add two ½n digit integers. n Multiply three ½n-digit integers. n Add, subtract, and shift ½n-digit integers to obtain result. Theorem. [Karatsuba-Ofman, 1962] Can multiply two n-digit integers in O(n 1.585 ) bit operations. Karatsuba Multiplication AB C A C

81 81 Karatsuba: Recursion Tree n 3(n/2) 9(n/4) 3 k (n / 2 k ) 3 lg n (2)... T(n) T(n/2) T(n/4) T(2) T(n / 2 k ) T(n/4) T(n/2) T(n/4) T(n/2) T(n/4)...

82 Matrix Multiplication

83 83 Matrix multiplication. Given two n-by-n matrices A and B, compute C = AB. Brute force.  (n 3 ) arithmetic operations. Fundamental question. Can we improve upon brute force? Matrix Multiplication

84 84 Matrix Multiplication: Warmup Divide-and-conquer. n Divide: partition A and B into ½n-by-½n blocks. n Conquer: multiply 8 ½n-by-½n recursively. n Combine: add appropriate products using 4 matrix additions.

85 85 Matrix Multiplication: Key Idea Key idea. multiply 2-by-2 block matrices with only 7 multiplications. n 7 multiplications. n 18 = 10 + 8 additions (or subtractions).

86 86 Fast Matrix Multiplication Fast matrix multiplication. (Strassen, 1969) n Divide: partition A and B into ½n-by-½n blocks. n Compute: 14 ½n-by-½n matrices via 10 matrix additions. n Conquer: multiply 7 ½n-by-½n matrices recursively. n Combine: 7 products into 4 terms using 8 matrix additions. Analysis. n Assume n is a power of 2. n T(n) = # arithmetic operations.

87 87 Fast Matrix Multiplication in Practice Implementation issues. n Sparsity. n Caching effects. n Numerical stability. n Odd matrix dimensions. n Crossover to classical algorithm around n = 128. Common misperception: "Strassen is only a theoretical curiosity." n Advanced Computation Group at Apple Computer reports 8x speedup on G4 Velocity Engine when n ~ 2,500. n Range of instances where it's useful is a subject of controversy. Remark. Can "Strassenize" Ax=b, determinant, eigenvalues, and other matrix ops.

88 88 Fast Matrix Multiplication in Theory Q. Multiply two 2-by-2 matrices with only 7 scalar multiplications? A. Yes! [Strassen, 1969] Q. Multiply two 2-by-2 matrices with only 6 scalar multiplications? A. Impossible. [Hopcroft and Kerr, 1971] Q. Two 3-by-3 matrices with only 21 scalar multiplications? A. Also impossible. Q. Two 70-by-70 matrices with only 143,640 scalar multiplications? A. Yes! [Pan, 1980] Decimal wars. n December, 1979: O(n 2.521813 ). n January, 1980: O(n 2.521801 ).

89 89 Fast Matrix Multiplication in Theory Best known. O(n 2.376 ) [Coppersmith-Winograd, 1987.] Conjecture. O(n 2+  ) for any  > 0. Caveat. Theoretical improvements to Strassen are progressively less practical.

90 90 Algorithm 5.4, page 233

91 91

92 92 Algorithm 5.5, Exercise 4, page 247

93 93 Exercise 5, page 248


Download ppt "1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved."

Similar presentations


Ads by Google