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 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 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 Merge sort 1. Divide: Trivial. 2. Conquer: Recursively sort 2 subarrays. 3. Combine: Linear-time merge. T(n) = 2 T(n/2) + !(n) # subproblems subproblem size September 14, 2005 work dividing and combining Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.4 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 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 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 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 September 14, 2005 5 7 8 9 12 15 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.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 September 14, 2005 5 7 8 9 12 15 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.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 September 14, 2005 5 7 8 9 12 15 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.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 September 14, 2005 5 7 8 9 12 15 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.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 September 14, 2005 5 7 8 9 12 15 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.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 September 14, 2005 5 7 8 9 12 15 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.13 Recurrence for binary search T(n) = 1 T(n/2) + !(1) # subproblems subproblem size September 14, 2005 work dividing and combining Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.14 Recurrence for binary search T(n) = 1 T(n/2) + !(1) # subproblems subproblem size work dividing and combining 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 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 Powering a number Problem: Compute a n, where n ' N. Naive algorithm: !(n). Divide-and-conquer algorithm: an = September 14, 2005 a n/2 ( a n/2 a (n–1)/2 ( a (n–1)/2 ( a if n is even; if n is odd. Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.17 Powering a number Problem: Compute a n, where n ' N. Naive algorithm: !(n). Divide-and-conquer algorithm: an = a n/2 ( a n/2 a (n–1)/2 ( a (n–1)/2 ( a if n is even; 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 Fibonacci numbers Recursive definition: 0 if n = 0; if n = 1; Fn = 1 Fn–1 + Fn–2 if n $ 2. 0 1 September 14, 2005 1 2 3 5 8 13 21 34 ! Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.19 Fibonacci numbers Recursive definition: 0 if n = 0; if n = 1; Fn = 1 Fn–1 + Fn–2 if n $ 2. 0 1 1 2 3 5 8 13 21 34 ! 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 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 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 Recursive squaring 0 Fn *1 Theorem: . / Fn September 14, 2005 n Fn - 01 1. 1. + + Fn 21 , /1 0, Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.23 Recursive squaring 0 Fn *1 Theorem: . / Fn n Fn - 01 1. 1. + + Fn 21 , /1 0, Algorithm: Recursive squaring. Time = !(lg n) . September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.24 Recursive squaring 0 Fn *1 Theorem: . / Fn n Fn - 01 1. 1. + + Fn 21 , /1 0, Algorithm: Recursive squaring. Time = !(lg n) . Proof of theorem. (Induction on n.) 1 F F 0 2 1 - 01 1 Base (n = 1): . . 1. + + / F1 F0 , /1 0, September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.25 Recursive squaring Inductive step (n $ 2): 0 Fn *1 . F / n September 14, 2005 . Fn - 0 Fn Fn 21 - 01 1(. 1. + + Fn 21 , / Fn 21 Fn 2 2 , /1 0+, n21 01 101 11. (. . + + /1 0, /1 0, n 01 11. + 1 0 / , Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.26 Matrix multiplication Input: A = [aij], B = [bij]. Output: C = [cij] = A( B. 0 c11 c12 .c c . 21 22 . " " .c c / n1 n 2 ! c1n - 0 a11 a12 ! c2 n + .a21 a22 +1. " # " + . " ! cnn +, /. an1 an 2 i, j = 1, 2,… , n. ! a1n - 0 b11 b12 ! a2 n + .b21 b22 +(. # " + . " " ! ann +, ./bn1 bn 2 ! b1n ! b2 n + + # " + ! bnn +, n cij 1 3 aik ( bkj k 11 September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.27 Standard algorithm for i 4 1 to n do for j 4 1 to n do cij 4 0 for k 4 1 to n do cij 4 cij + aik( bkj September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.28 Standard algorithm for i 4 1 to n do for j 4 1 to n do cij 4 0 for k 4 1 to n do cij 4 cij + aik( bkj Running time = !(n3) September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.29 Divide-and-conquer algorithm IDEA: n5n matrix = 252 matrix of (n/2)56n/2) submatrices: 0r s - 0a b - 0 e f . t u + 1 .c d + ( . g h + , / , / , / C r s t u = ae + bg = af + bh = ce + dg = cf + dh September 14, 2005 = A ( B 8 mults of (n/2)56n/2) submatrices 4 adds of (n/2)56n/2) submatrices Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.30 Divide-and-conquer algorithm IDEA: n5n matrix = 252 matrix of (n/2)56n/2) submatrices: 0r s - 0a b - 0 e f . t u + 1 .c d + ( . g h + , / , / , / r s t u = ae + bg = af + bh = ce + dh = cf + dg September 14, 2005 g C = A ( B recursive 8 mults of (n/2)56n/2) submatrices ^ 4 adds of (n/2)56n/2) submatrices h Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.31 Analysis of D&C algorithm T(n) = 8 T(n/2) + !(n2) # submatrices submatrix size September 14, 2005 work adding submatrices Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.32 Analysis of D&C algorithm T(n) = 8 T(n/2) + !(n2) # submatrices submatrix size work adding submatrices nlogba = nlog28 = n3 # CASE 1 # T(n) = !(n3). September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.33 Analysis of D&C algorithm T(n) = 8 T(n/2) + !(n2) # submatrices submatrix size work adding submatrices 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 Strassen’s idea • Multiply 252 matrices with only 7 recursive mults. September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.35 Strassen’s idea • Multiply 252 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 Strassen’s idea • Multiply 252 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 r s t u = P5 + P4 – P2 + P6 = P1 + P2 = P3 + P4 = P5 + P1 – P3 – P7 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.37 Strassen’s idea • Multiply 252 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 ) Matrix multiplication is not commutative! September 14, 2005 r s t u = P5 + P4 – P2 + P6 = P1 + P2 = P3 + P4 = P5 + P1 – P3 – P7 77 mults, mults, 18 18 adds/subs. adds/subs. Note: Note: No No reliance reliance on on commutativity commutativity of of mult! mult! Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.38 Strassen’s idea • Multiply 252 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 r = P5 + P4 – P2 + P6 = (a + d) (e + h) + d (g – e) – (a + b) h + (b – d) (g + h) = ae + ah + de + dh + dg –de – ah – bh + bg + bh – dg – dh = ae + bg Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.39 Strassen’s algorithm 1. Divide: Partition A and B into (n/2)5(n/2) submatrices. Form terms to be multiplied using + and – . 2. Conquer: Perform 7 multiplications of (n/2)5(n/2) submatrices recursively. 3. Combine: Form C using + and – on (n/2)5(n/2) submatrices. September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.40 Strassen’s algorithm 1. Divide: Partition A and B into (n/2)5(n/2) submatrices. Form terms to be multiplied using + and – . 2. Conquer: Perform 7 multiplications of (n/2)5(n/2) submatrices recursively. 3. Combine: Form C using + and – on (n/2)5(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 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 Analysis of Strassen T(n) = 7 T(n/2) + !(n2) nlogba = nlog27 7 n2.81 # CASE 1 # T(n) = !(nlg 7). September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.43 Analysis of Strassen T(n) = 7 T(n/2) + !(n2) nlogba = nlog27 7 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 Analysis of Strassen T(n) = 7 T(n/2) + !(n2) nlogba = nlog27 7 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.376!). September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.45 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 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 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 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 W(n) = 2 W(n/2) + !(1) = !(n) Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.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) Area = !(n lg n) September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.50 H-tree embedding L(n) L(n) September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.51 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 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 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