Your SlideShare is downloading. ×
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Lec3 Algott
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Lec3 Algott

843

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
843
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
55
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Introduction to Algorithms 6.046J/18.401J LECTURE 3 Divide and Conquer • Binary search • Powering a number • Fibonacci numbers • Matrix multiplication • Strassen’s algorithm • VLSI tree layout Prof. Erik D. Demaine September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.1
  • 2. The divide-and-conquer design paradigm 1. Divide the problem (instance) into subproblems. 2. Conquer the subproblems by solving them recursively. 3. Combine subproblem solutions. September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.2
  • 3. Merge sort 1. Divide: Trivial. 2. Conquer: Recursively sort 2 subarrays. 3. Combine: Linear-time merge. September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.3
  • 4. Merge sort 1. Divide: Trivial. 2. Conquer: Recursively sort 2 subarrays. 3. Combine: Linear-time merge. T(n) = 2 T(n/2) + Θ(n) # subproblems work dividing and combining subproblem size September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.4
  • 5. Master theorem (reprise) T(n) = a T(n/b) + f (n) CASE 1: f (n) = O(nlogba – ε), constant ε > 0 ⇒ T(n) = Θ(nlogba) . CASE 2: f (n) = Θ(nlogba lgkn), constant k ≥ 0 ⇒ T(n) = Θ(nlogba lgk+1n) . CASE 3: f (n) = Ω(nlogba + ε ), constant ε > 0, and regularity condition ⇒ T(n) = Θ( f (n)) . September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.5
  • 6. Master theorem (reprise) T(n) = a T(n/b) + f (n) CASE 1: f (n) = O(nlogba – ε), constant ε > 0 ⇒ T(n) = Θ(nlogba) . CASE 2: f (n) = Θ(nlogba lgkn), constant k ≥ 0 ⇒ T(n) = Θ(nlogba lgk+1n) . CASE 3: f (n) = Ω(nlogba + ε ), constant ε > 0, and regularity condition ⇒ T(n) = Θ( f (n)) . Merge sort: a = 2, b = 2 ⇒ nlogba = nlog22 = n ⇒ CASE 2 (k = 0) ⇒ T(n) = Θ(n lg n) . September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.6
  • 7. Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.7
  • 8. Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.8
  • 9. Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.9
  • 10. Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.10
  • 11. Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.11
  • 12. Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.12
  • 13. Binary search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find 9 3 5 7 8 9 12 15 September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.13
  • 14. Recurrence for binary search T(n) = 1 T(n/2) + Θ(1) # subproblems work dividing and combining subproblem size September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.14
  • 15. Recurrence for binary search T(n) = 1 T(n/2) + Θ(1) # subproblems work dividing and combining subproblem size nlogba = nlog21 = n0 = 1 ⇒ CASE 2 (k = 0) ⇒ T(n) = Θ(lg n) . September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.15
  • 16. Powering a number Problem: Compute a n, where n ∈ N. Naive algorithm: Θ(n). September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.16
  • 17. Powering a number Problem: Compute a n, where n ∈ N. Naive algorithm: Θ(n). Divide-and-conquer algorithm: a n/2 ⋅ a n/2 if n is even; an = a (n–1)/2 ⋅ a (n–1)/2 ⋅ a if n is odd. September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.17
  • 18. Powering a number Problem: Compute a n, where n ∈ N. Naive algorithm: Θ(n). Divide-and-conquer algorithm: a n/2 ⋅ a n/2 if n is even; an = a (n–1)/2 ⋅ a (n–1)/2 ⋅ a if n is odd. T(n) = T(n/2) + Θ(1) ⇒ T(n) = Θ(lg n) . September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.18
  • 19. Fibonacci numbers Recursive definition: 0 if n = 0; Fn = 1 if n = 1; Fn–1 + Fn–2 if n ≥ 2. 0 1 1 2 3 5 8 13 21 34 L September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.19
  • 20. Fibonacci numbers Recursive definition: 0 if n = 0; Fn = 1 if n = 1; Fn–1 + Fn–2 if n ≥ 2. 0 1 1 2 3 5 8 13 21 34 L Naive recursive algorithm: Ω(φ n) (exponential time), where φ = (1 + 5) / 2 is the golden ratio. September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.20
  • 21. Computing Fibonacci numbers Bottom-up: • Compute F0, F1, F2, …, Fn in order, forming each number by summing the two previous. • Running time: Θ(n). September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.21
  • 22. Computing Fibonacci numbers Bottom-up: • Compute F0, F1, F2, …, Fn in order, forming each number by summing the two previous. • Running time: Θ(n). Naive recursive squaring: Fn = φ n/ 5 rounded to the nearest integer. • Recursive squaring: Θ(lg n) time. • This method is unreliable, since floating-point arithmetic is prone to round-off errors. September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.22
  • 23. Recursive squaring n ⎡ Fn +1 Fn ⎤ ⎡1 1⎤ Theorem: ⎢ ⎥ = ⎢1 0⎥ . ⎣ Fn Fn −1 ⎦ ⎣ ⎦ September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.23
  • 24. Recursive squaring n ⎡ Fn +1 Fn ⎤ ⎡1 1⎤ Theorem: ⎢ ⎥ = ⎢1 0⎥ . ⎣ Fn Fn −1 ⎦ ⎣ ⎦ Algorithm: Recursive squaring. Time = Θ(lg n) . September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.24
  • 25. Recursive squaring n ⎡ Fn +1 Fn ⎤ ⎡1 1⎤ Theorem: ⎢ ⎥ = ⎢1 0⎥ . ⎣ Fn Fn −1 ⎦ ⎣ ⎦ Algorithm: Recursive squaring. Time = Θ(lg n) . Proof of theorem. (Induction on n.) ⎡ F2 F1 ⎤ ⎡1 1⎤ 1 Base (n = 1): ⎢ =⎢ . ⎣ F1 F0 ⎥ ⎣1 0⎥ ⎦ ⎦ September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.25
  • 26. Recursive squaring Inductive step (n ≥ 2): . ⎡ Fn +1 Fn ⎤ ⎡ Fn Fn −1 ⎤ ⎡1 1⎤ ⎢ F ⎥ = ⎢F ⎥ ⋅ ⎢1 0⎥ Fn −1 ⎦ ⎣ n −1 Fn − 2 ⎦ ⎣ ⎣ n ⎦ n−1 ⎡1 1⎤ ⎡1 1⎤ =⎢ ⎥ ⋅⎢ ⎥. ⎣1 0⎦ ⎣1 0⎦ n ⎡1 1⎤ =⎢ ⎣1 0⎥ ⎦ September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.26
  • 27. Matrix multiplication Input: A = [aij], B = [bij]. i, j = 1, 2,… , n. Output: C = [cij] = A⋅ B. ⎡ c11 c12 L c1n ⎤ ⎡ a11 a12 L a1n ⎤ ⎡ b11 b12 L b1n ⎤ ⎢c c L c2 n ⎥ ⎢a21 a22 L a2 n ⎥ ⎢b21 b22 L b2 n ⎥ ⎢ 21 22 ⎥=⎢ ⎥⋅⎢ ⎥ ⎢ M M O M ⎥ ⎢ M M O M ⎥ ⎢ M M O M ⎥ ⎢c c L cnn ⎥ ⎣ an1 an 2 ⎢ L ann ⎥ ⎢bn1 bn 2 L bnn ⎥ ⎣ n1 n 2 ⎦ ⎦ ⎣ ⎦ n cij = ∑ aik ⋅ bkj k =1 September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.27
  • 28. Standard algorithm for i ← 1 to n do for j ← 1 to n do cij ← 0 for k ← 1 to n do cij ← cij + aik⋅ bkj September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.28
  • 29. Standard algorithm for i ← 1 to n do for j ← 1 to n do cij ← 0 for k ← 1 to n do cij ← cij + aik⋅ bkj Running time = Θ(n3) September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.29
  • 30. Divide-and-conquer algorithm IDEA: n×n matrix = 2×2 matrix of (n/2)×(n/2) submatrices: ⎡r s ⎤ ⎡a b ⎤ ⎡ e f ⎤ ⎢ t u ⎥ = ⎢c d ⎥ ⋅ ⎢ g h ⎥ ⎣ ⎦ ⎣ ⎦ ⎣ ⎦ C = A ⋅ B r = ae + bg s = af + bh 8 mults of (n/2)×(n/2) submatrices t = ce + dg 4 adds of (n/2)×(n/2) submatrices u = cf + dh September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.30
  • 31. Divide-and-conquer algorithm IDEA: n×n matrix = 2×2 matrix of (n/2)×(n/2) submatrices: ⎡r s ⎤ ⎡a b ⎤ ⎡ e f ⎤ ⎢ t u ⎥ = ⎢c d ⎥ ⋅ ⎢ g h ⎥ ⎣ ⎦ ⎣ ⎦ ⎣ ⎦ C = A ⋅ B r = ae + bg recursive s = af + bh 8 mults of (n/2)×(n/2) submatrices ^ t = ce + dh 4 adds of (n/2)×(n/2) submatrices u = cf + dg September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.31
  • 32. Analysis of D&C algorithm T(n) = 8 T(n/2) + Θ(n2) # submatrices work adding submatrices submatrix size September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.32
  • 33. Analysis of D&C algorithm T(n) = 8 T(n/2) + Θ(n2) # submatrices work adding submatrices submatrix size nlogba = nlog28 = n3 ⇒ CASE 1 ⇒ T(n) = Θ(n3). September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.33
  • 34. Analysis of D&C algorithm T(n) = 8 T(n/2) + Θ(n2) # submatrices work adding submatrices submatrix size nlogba = nlog28 = n3 ⇒ CASE 1 ⇒ T(n) = Θ(n3). No better than the ordinary algorithm. September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.34
  • 35. Strassen’s idea • Multiply 2×2 matrices with only 7 recursive mults. September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.35
  • 36. Strassen’s idea • Multiply 2×2 matrices with only 7 recursive mults. P1 = a ⋅ ( f – h) P2 = (a + b) ⋅ h P3 = (c + d) ⋅ e P4 = d ⋅ (g – e) P5 = (a + d) ⋅ (e + h) P6 = (b – d) ⋅ (g + h) P7 = (a – c) ⋅ (e + f ) September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.36
  • 37. Strassen’s idea • Multiply 2×2 matrices with only 7 recursive mults. P1 = a ⋅ ( f – h) r = P5 + P4 – P2 + P6 P2 = (a + b) ⋅ h s = P1 + P2 P3 = (c + d) ⋅ e t = P3 + P4 P4 = d ⋅ (g – e) u = P5 + P1 – P3 – P7 P5 = (a + d) ⋅ (e + h) P6 = (b – d) ⋅ (g + h) P7 = (a – c) ⋅ (e + f ) September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.37
  • 38. Strassen’s idea • Multiply 2×2 matrices with only 7 recursive mults. P1 = a ⋅ ( f – h) r = P5 + P4 – P2 + P6 P2 = (a + b) ⋅ h s = P1 + P2 P3 = (c + d) ⋅ e t = P3 + P4 P4 = d ⋅ (g – e) u = P5 + P1 – P3 – P7 P5 = (a + d) ⋅ (e + h) P6 = (b – d) ⋅ (g + h) 7 mults, 18 adds/subs. 7 mults, 18 adds/subs. P7 = (a – c) ⋅ (e + f ) Note: No reliance on Note: No reliance on commutativity of mult! commutativity of mult! September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.38
  • 39. Strassen’s idea • Multiply 2×2 matrices with only 7 recursive mults. P1 = a ⋅ ( f – h) r = P5 + P4 – P2 + P6 P2 = (a + b) ⋅ h = (a + d) (e + h) P3 = (c + d) ⋅ e + d (g – e) – (a + b) h P4 = d ⋅ (g – e) + (b – d) (g + h) P5 = (a + d) ⋅ (e + h) = ae + ah + de + dh P6 = (b – d) ⋅ (g + h) + dg –de – ah – bh P7 = (a – c) ⋅ (e + f ) + bg + bh – dg – dh = ae + bg September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.39
  • 40. Strassen’s algorithm 1. Divide: Partition A and B into (n/2)×(n/2) submatrices. Form terms to be multiplied using + and – . 2. Conquer: Perform 7 multiplications of (n/2)×(n/2) submatrices recursively. 3. Combine: Form C using + and – on (n/2)×(n/2) submatrices. September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.40
  • 41. Strassen’s algorithm 1. Divide: Partition A and B into (n/2)×(n/2) submatrices. Form terms to be multiplied using + and – . 2. Conquer: Perform 7 multiplications of (n/2)×(n/2) submatrices recursively. 3. Combine: Form C using + and – on (n/2)×(n/2) submatrices. T(n) = 7 T(n/2) + Θ(n2) September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.41
  • 42. Analysis of Strassen T(n) = 7 T(n/2) + Θ(n2) September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.42
  • 43. Analysis of Strassen T(n) = 7 T(n/2) + Θ(n2) nlogba = nlog27 ≈ n2.81 ⇒ CASE 1 ⇒ T(n) = Θ(nlg 7). September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.43
  • 44. Analysis of Strassen T(n) = 7 T(n/2) + Θ(n2) nlogba = nlog27 ≈ n2.81 ⇒ CASE 1 ⇒ T(n) = Θ(nlg 7). The number 2.81 may not seem much smaller than 3, but because the difference is in the exponent, the impact on running time is significant. In fact, Strassen’s algorithm beats the ordinary algorithm on today’s machines for n ≥ 32 or so. September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.44
  • 45. Analysis of Strassen T(n) = 7 T(n/2) + Θ(n2) nlogba = nlog27 ≈ n2.81 ⇒ CASE 1 ⇒ T(n) = Θ(nlg 7). The number 2.81 may not seem much smaller than 3, but because the difference is in the exponent, the impact on running time is significant. In fact, Strassen’s algorithm beats the ordinary algorithm on today’s machines for n ≥ 32 or so. Best to date (of theoretical interest only): Θ(n2.376L). September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.45
  • 46. VLSI layout Problem: Embed a complete binary tree with n leaves in a grid using minimal area. September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.46
  • 47. VLSI layout Problem: Embed a complete binary tree with n leaves in a grid using minimal area. W(n) H(n) September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.47
  • 48. VLSI layout Problem: Embed a complete binary tree with n leaves in a grid using minimal area. W(n) H(n) H(n) = H(n/2) + Θ(1) = Θ(lg n) September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.48
  • 49. VLSI layout Problem: Embed a complete binary tree with n leaves in a grid using minimal area. W(n) H(n) H(n) = H(n/2) + Θ(1) W(n) = 2 W(n/2) + Θ(1) = Θ(lg n) = Θ(n) September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.49
  • 50. VLSI layout Problem: Embed a complete binary tree with n leaves in a grid using minimal area. W(n) H(n) H(n) = H(n/2) + Θ(1) W(n) = 2 W(n/2) + Θ(1) = Θ(lg n) = Θ(n) Area = Θ(n lg n) September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.50
  • 51. H-tree embedding L(n) L(n) September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.51
  • 52. H-tree embedding L(n) L(n) L(n/4) Θ(1) L(n/4) September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.52
  • 53. H-tree embedding L(n) L(n) = 2 L(n/4) + Θ(1) = Θ( n ) L(n) Area = Θ(n) L(n/4) Θ(1) L(n/4) September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.53
  • 54. Conclusion • Divide and conquer is just one of several powerful techniques for algorithm design. • Divide-and-conquer algorithms can be analyzed using recurrences and the master method (so practice this math). • The divide-and-conquer strategy often leads to efficient algorithms. September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.54

×