Divide-and-conquer algorithm IDEA: n n matrix = 2 2 matrix of (n/2) (n/2) submatrices: r=ae+bg s=af+bh t =ce+dh u=cf+dg r t s u = a c e g September 15, 2004 Introduction to Algorithms L3.31 b d C = A B recursive 8 mults of (n/2) (n/2) submatrices ^ 4 adds of (n/2) (n/2) submatrices f h
Analysis of D&C algorithm T(n) = 8 T(n/2) + Θ(n 2 ) # submatrices submatrix size work adding submatrices September 15, 2004 Introduction to Algorithms L3.32
Analysis of D&C algorithm T(n) = 8 T(n/2) + Θ(n 2 ) # submatrices submatrix size work adding submatrices n log ba = n log 2 8 = n 3 CASE 1 T(n) = Θ(n 3 ). September 15, 2004 Introduction to Algorithms L3.33
Analysis of D&C algorithm T(n) = 8 T(n/2) + Θ(n 2 ) # submatrices submatrix size work adding submatrices n log ba = n log 2 8 = n 3 CASE 1 T(n) = Θ(n 3 ). No better than the ordinary algorithm. September 15, 2004 Introduction to Algorithms L3.34
Strassen s idea Multiply 2 2 matrices with only 7 recursive mults. September 15, 2004 Introduction to Algorithms L3.35
Strassen s idea Multiply 2 2 matrices with only 7 recursive mults. P 1 = a ( f h) P 2 = (a + b) h P 3 = (c + d) e P 4 = d (g e) P 5 = (a + d) (e + h) P 6 = (b d) (g + h) P 7 = (a c) (e + f ) September 15, 2004 Introduction to Algorithms L3.36
Strassen s idea Multiply 2 2 matrices with only 7 recursive mults. P 1 = a ( f h) P 2 = (a + b) h P 3 = (c + d) e P 4 = d (g e) P 5 = (a + d) (e + h) P 6 = (b d) (g + h) P 7 = (a c) (e + f ) r = P 5 + P 4 P 2 + P 6 s = P 1 + P 2 t = P 3 + P 4 u = P 5 + P 1 P 3 P 7 September 15, 2004 Introduction to Algorithms L3.37
Strassen s idea Multiply 2 2 matrices with only 7 recursive mults. P 1 = a ( f h) P 2 = (a + b) h P 3 = (c + d) e P 4 = d (g e) P 5 = (a + d) (e + h) P 6 = (b d) (g + h) P 7 = (a c) (e + f ) r = P 5 + P 4 P 2 + P 6 s = P 1 + P 2 t = P 3 + P 4 u = P 5 + P 1 P 3 P 7 7 mults, 18 adds/subs. Note: No reliance on commutativity of mult! September 15, 2004 Introduction to Algorithms L3.38
Strassen s idea Multiply 2 2 matrices with only 7 recursive mults. P 1 = a ( f h) P 2 = (a + b) h P 3 = (c + d) e P 4 = d (g e) P 5 = (a + d) (e + h) P 6 = (b d) (g + h) P 7 = (a c) (e + f ) r = P 5 + P 4 P 2 + P 6 =(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 September 15, 2004 Introduction to Algorithms L3.39
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 15, 2004 Introduction to Algorithms L3.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. T(n) = 7 T(n/2) + Θ(n 2 ) September 15, 2004 Introduction to Algorithms L3.41
Analysis of Strassen T(n) = 7 T(n/2) + Θ(n 2 ) September 15, 2004 Introduction to Algorithms L3.42
Analysis of Strassen T(n) = 7 T(n/2) + Θ(n 2 ) n log ba = n log 2 7 n 2.81 CASE 1 T(n) = Θ(n lg 7 ). September 15, 2004 Introduction to Algorithms L3.43
Analysis of Strassen T(n) = 7 T(n/2) + Θ(n 2 ) n log ba = n log 2 7 n 2.81 CASE 1 T(n) = Θ(n lg 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 15, 2004 Introduction to Algorithms L3.44
Analysis of Strassen T(n) = 7 T(n/2) + Θ(n 2 ) n log ba = n log 2 7 n 2.81 CASE 1 T(n) = Θ(n lg 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): Θ(n 2.376L ). September 15, 2004 Introduction to Algorithms L3.45
VLSI layout Problem: Embed a complete binary tree with n leaves in a grid using minimal area. September 15, 2004 Introduction to Algorithms L3.46
VLSI layout Problem: Embed a complete binary tree with n leaves in a grid using minimal area. W(n) H(n) September 15, 2004 Introduction to Algorithms L3.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 15, 2004 Introduction to Algorithms L3.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) W(n) = 2W(n/2) + Θ(1) = Θ(n) September 15, 2004 Introduction to Algorithms L3.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) = 2W(n/2) + Θ(1) = Θ(lg n) = Θ(n) Area = Θ(n lg n) September 15, 2004 Introduction to Algorithms L3.50
H-tree embedding L(n) L(n) September 15, 2004 Introduction to Algorithms L3.51
H-tree embedding L(n) L(n) L(n/4) Θ(1) L(n/4) September 15, 2004 Introduction to Algorithms L3.52
H-tree embedding L(n) L(n) L(n) = 2L(n/4) + Θ(1) = Θ( n) Area = Θ(n) L(n/4) Θ(1) L(n/4) September 15, 2004 Introduction to Algorithms L3.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 15, 2004 Introduction to Algorithms L3.54