

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!


9/20/07 CS 3343 Analysis of Algorithms 1
Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk 9/20/07 CS 3343 Analysis of Algorithms 2
Problem: Compute a n , where n ∈ N.
a n^ =
a n/^2 ⋅ a n/^2 if n is even; a ( n– 1) /^2 ⋅ a ( n– 1) /^2 ⋅ a if n is odd.
Divide-and-conquer algorithm: (recursive squaring)
T ( n ) = T ( n /2) + Θ(1) ⇒ T ( n ) = Θ(log n ).
Naive algorithm: Θ( n ).
9/20/07 CS 3343 Analysis of Algorithms 3
⋅
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 bkj 1
Input: A = [ aij ], B = [ b (^) ij ]. Output: C = [ c (^) ij ] = A ⋅ B. i , j = 1, 2,… , n.
9/20/07 CS 3343 Analysis of Algorithms 4
for i ← 1 to n do for j ← 1 to n do c (^) ij ← 0 for k ← 1 to n do c (^) ij ← c (^) ij + a (^) ik ⋅ bkj
Running time = Θ( n^3 )
9/20/07 CS 3343 Analysis of Algorithms 5
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 = a·e+b·g s = a·f+ b·h t = c·e+d·h u = c·f +d·g
8 recursive mults of ( n /2)×( n /2) submatrices 4 adds of ( n /2)×( n /2) submatrices
9/20/07 CS 3343 Analysis of Algorithms 6
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 )
9/20/07 CS 3343 Analysis of Algorithms 7
7 mults, 18 adds/subs. Note: No reliance on commutativity of mult!
7 mults, 18 adds/subs. Note: No reliance on commutativity of mult!
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
9/20/07 CS 3343 Analysis of Algorithms 8
s = P 1 + P 2 = a ⋅ ( f – h ) + ( a + b ) ⋅ h = af – ah + ah + bh = af + bh
9/20/07 CS 3343 Analysis of Algorithms 9
1. Divide: Partition A and B into ( n /2)×( n /2) submatrices. Form P -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 )
9/20/07 CS 3343 Analysis of Algorithms 10
T ( n ) = 7 T ( n /2) + Θ( n^2 ) n log ba^ = n log^27 ≈ n 2.81^ ⇒ CASE 1 ⇒ T ( n ) = Θ( n log 7).
Best to date (of theoretical interest only): Θ( n 2.376Λ).
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 ≥ 30 or so.