

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
Sofya Raskhodnikova
Divide and Conquer
10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
Exercise: Why not divide into groups of 3? 10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
an-1 an-2 … a 0 bn-1 bn-2 … b 0 n bits n bits
n bits 2n bit output
0
0 0 0
10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
MULTIPLY ( n , a, b ) a and b are n -bit integers Assume n is a power of 2 for simplicity
1. If n 2 then use grade-school algorithm else 2. A 1 a div 2 n /2^ ; B 1 b div 2 n /2^ ; 3. A 0 a mod 2 n /2^ ; B 0 b mod 2 n /^. 4. x MULTIPLY( n /2 , A 1 , B 1 ) 5. y MULTIPLY( n /2 , A 0 , B 0 ) 6. z MULTIPLY( n /2 , A 1 + A 0 , B 1 + B 0 )
10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
⋅
n n nn
n
n
n n nn
n
n
n n nn
n
n
b b b
b b b
b b b
a a a
a a a
a a a
c c c
c c c
c c c
1 2
21 22 2
11 12 1
1 2
21 22 2
11 12 1
1 2
21 22 2
11 12 1
=
n
k
cij aik b kj 1
Input: A = [ aij ], B = [ bij ]. Output: C = [ cij ] = A× B. i , j = 1, 2,… , n.
10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
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 = Θ( n^3 )
10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
n n matrix = 2 2 matrix of ( n /2)( n /2) submatrices:
g h
e f c d
a b t u
r s
r = ae + bg s = af + bh t = ce + dh u = cf + dg
8 mults of ( n /2) ( n /2) submatrices 4 adds of ( n /2) ( n /2) submatrices
recursive
10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
n log ba^ = n log^28 = n^3 CASE 1 T ( n ) = Θ( n^3 ).
No better than the ordinary algorithm.
# submatrices submatrix size
work adding submatrices
T ( n ) = 8 T ( n /2) + Θ( n^2 )