Docsity

Comparison of Matrix Multiplication Algorithms: Naive, Standard, and Strassen's, Study notes of Algorithms and Programming

Toggle sidebar
bg1
1
9/20/07 CS 3343 Analysis of Algorithms 1
CS 3343 -- Fall 2007
Matrix Multiplication
Carola Wenk
Slides courtesy of Charles Leiserson with small
changes by Carola Wenk
9/20/07 CS 3343 Analysis of Algorithms 2
Powering a number
Problem: Compute an, where n
∈
N.
an=an/2 ⋅an/2 if nis even;
a(n–1)/2 ⋅a(n–1)/2 ⋅aif nis 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
Matrix multiplication
⋅
=
nnnn
n
n
nnnn
n
n
nnnn
n
n
bbb
bbb
bbb
aaa
aaa
aaa
ccc
ccc
ccc
Λ
ΜΟΜΜ
Λ
Λ
Λ
ΜΟΜΜ
Λ
Λ
Λ
ΜΟΜΜ
Λ
Λ
21
22221
11211
21
22221
11211
21
22221
11211
∑
=
⋅= n
k
kjikij bac
1
Input: A= [aij], B= [bij].
Output: C= [cij] = A
⋅
B.i,j= 1, 2,… , n.
9/20/07 CS 3343 Analysis of Algorithms 4
Standard algorithm
for i ←1to n
do for j←1to n
do cij ←0
for k←1to n
do cij ←cij + aik
⋅
bkj
Running time = Θ(n3)
9/20/07 CS 3343 Analysis of Algorithms 5
Divide-and-conquer algorithm
n×nmatrix = 2×2matrix of (n/2)×(n/2) submatrices:
IDEA:
⋅
=
hg
f
e
dc
ba
ut
s
r
C=A
⋅
B
r = a·e+b·g
s = a·f+ b·h
t = c·e+d·h
u=c·f+d·g
8recursive mults of (n/2)×(n/2) submatrices
4adds of (n/2)×(n/2) submatrices
9/20/07 CS 3343 Analysis of Algorithms 6
Analysis of D&C algorithm
nlogba= nlog28= n3⇒CASE 1⇒T(n) = Θ(n3).
No better than the ordinary algorithm.
# submatrices
submatrix size
work adding
submatrices
T(n) = 8 T(n/2) + Θ(n2)
pf2

Partial preview of the text

Download Comparison of Matrix Multiplication Algorithms: Naive, Standard, and Strassen's and more Study notes Algorithms and Programming in PDF only on Docsity!

9/20/07 CS 3343 Analysis of Algorithms 1

CS 3343 -- Fall 2007

Matrix Multiplication

Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk 9/20/07 CS 3343 Analysis of Algorithms 2

Powering a number

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

Matrix multiplication

 ⋅ 

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

Standard algorithm

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

Divide-and-conquer algorithm

n × n matrix = 2×2 matrix of ( n /2)×( n /2) submatrices:

I DEA :

g h

e f c d

a b t u

r s

C = A^ ⋅^ B

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

Analysis of D&C algorithm

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!

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

9/20/07 CS 3343 Analysis of Algorithms 8

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 )

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

Strassen’s algorithm

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

Analysis of Strassen

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.