Professional Documents
Culture Documents
Lec3 PDF
Lec3 PDF
6.046J/18.401J LECTURE 3
Divide and Conquer Binary search Powering a number Fibonacci numbers Matrix multiplication Strassens algorithm VLSI tree layout Prof. Erik D. Demaine
September 14, 2005 Copyright 2001-5 Erik D. Demaine and Charles E. Leiserson L2.1
L2.2
Merge sort
1. Divide: Trivial. 2. Conquer: Recursively sort 2 subarrays. 3. Combine: Linear-time merge.
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
L2.5
Binary search
Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial.
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
12
15
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
12
15
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
12
15
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
12
15
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
12
15
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
12
15
L2.13
L2.14
Powering a number
Problem: Compute a n, where n N. Naive algorithm: (n).
L2.16
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 (n1)/2 a (n1)/2 a if n is even; if n is odd.
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 (n1)/2 a (n1)/2 a if n is even; if n is odd.
Fibonacci numbers
Recursive definition: 0 if n = 0; if n = 1; Fn = 1 Fn1 + Fn2 if n 2. 0 1 1 2 3 5 8 13 21 34 L
L2.19
Fibonacci numbers
Recursive definition: 0 if n = 0; if n = 1; Fn = 1 Fn1 + Fn2 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
L2.21
Recursive squaring
Fn +1 Theorem: Fn Fn 1 1 . = Fn 1 1 0 n
L2.23
Recursive squaring
Fn +1 Theorem: Fn Fn 1 1 . = Fn 1 1 0 n
L2.24
Recursive squaring
Fn +1 Theorem: Fn Fn 1 1 . = Fn 1 1 0 n
Recursive squaring
Inductive step (n 2):
Fn +1 F n
. Fn Fn Fn 1 1 1 = Fn 1 Fn 1 Fn 2 1 0 n1 1 1 1 1 = . 1 0 1 0 n 1 1 = 1 0
L2.26
Matrix multiplication
Input: A = [aij], B = [bij]. Output: C = [cij] = A B.
c11 c12 c c 21 22 M M c c n1 n 2 L c1n a11 a12 L c2 n a21 a22 = M O M M a a L cnn n1 n 2 n
i, j = 1, 2, , n.
L b1n L b2 n O M L bnn
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
L2.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
L2.29
Divide-and-conquer algorithm
IDEA: nn matrix = 22 matrix of (n/2)(n/2) submatrices: r s a b e f t u = c d g h
C r s t u = ae + bg = af + bh = ce + dg = cf + dh
September 14, 2005
Divide-and-conquer algorithm
IDEA: nn matrix = 22 matrix of (n/2)(n/2) submatrices: r s a b e f t u = c d g h
r s t u
= ae + bg = af + bh = ce + dh = cf + dg
September 14, 2005
L2.32
L2.33
Strassens idea
Multiply 22 matrices with only 7 recursive mults.
L2.35
Strassens idea
Multiply 22 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
Strassens idea
Multiply 22 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
L2.37
Strassens idea
Multiply 22 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
7 7 mults, mults, 18 18 adds/subs. adds/subs. Note: Note: No No reliance reliance on on commutativity commutativity of of mult! mult!
L2.38
Strassens idea
Multiply 22 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
L2.39
Strassens 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.
L2.40
Strassens 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.
Analysis of Strassen
T(n) = 7 T(n/2) + (n2)
L2.42
Analysis of Strassen
T(n) = 7 T(n/2) + (n2) nlogba = nlog27 n2.81 CASE 1 T(n) = (nlg 7).
L2.43
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, Strassens algorithm beats the ordinary algorithm on todays machines for n 32 or so.
L2.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, Strassens algorithm beats the ordinary algorithm on todays 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
VLSI layout
Problem: Embed a complete binary tree with n leaves in a grid using minimal area.
L2.46
VLSI layout
Problem: Embed a complete binary tree with n leaves in a grid using minimal area.
W(n) H(n)
L2.47
VLSI layout
Problem: Embed a complete binary tree with n leaves in a grid using minimal area.
VLSI layout
Problem: Embed a complete binary tree with n leaves in a grid using minimal area.
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)
L2.51
H-tree embedding
L(n)
L(n)
H-tree embedding
L(n) L(n) = 2 L(n/4) + (1) = ( 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
L(n)
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.
L2.54