Unit - 1
Unit - 1
N 2
T (N) = 8T ( 2 ) + Θ(N )
The Master Theorem gives us
log (8) 3
T (N) = Θ(N 2
) = Θ(N )
3
So this is not an improvement on the “obvious” algorithm given earlier (that uses N operations).
Strassen’s algorithm is based on the following observation:
11 12
C =P5 + P4 − P2 + P6 C =P1 + P2
21 22
C =P3 + P4 C =P1 + P5 − P3 − P7
where
11 12 22
P1 = A (B −B )
11 12 22
P2 = (A + A )B
21 22 11
P3 = (A + A )B
22 21 11
P4 = A (B −B )
11 22 11 22
P5 = (A + A )(B +B )
12 22 21 22
P6 = (A − A )(B +B )
11 21 11 12
P7 = (A − A )(B +B )
11 22
Exercise Verify that C , . . . , C can be computed as above.
The above formulas can be used to compute A × B recursively as follows:
Strassen(A, B)
1. If N = 1 Output A × B
2. Else
11 11 22 22
3. Compute A , B , . . . , A , B % by computing M = N/2
11 12 22
4. P1 ← STRASSEN(A , B − B )
11 12 22
5. P2 ← STRASSEN(A + A , B )
21 22 11
6. P3 ← STRASSEN(A + A , B )
22 21 11
7. P4 ← STRASSEN(A , B − B )
11 22 11 22
8. P5 ← STRASSEN(A + A , B + B )
12 22 21 22
9. P6 ← STRASSEN(A − A , B + B )
11 21 11 12
10. P7 ← STRASSEN(A − A , B + B )
11
11. C ← P5 + P4 − P2 + P6
12
12. C ← P1 + P2
21
13. C ← P3 + P4
22
14. C ← P1 + P5 − P3 − P7
15. Output C
16. End If
2
Analysis: The operations on line 3 take constant time. The combining cost (lines 11–14) is Θ(N ).
There are 7 recursive calls (lines 4–10). So let T (N) be the total number of mathematical operations
performed by Strassen(A, B), then
N 2
T (N) = 7T ( 2 ) + Θ(N )
2
The Master Theorem gives us
log (7) 2.8
T (N) = Θ(N 2
) = Θ(N )
2.32
The best current upper bound for multiplying two matrices of size N × N is O(N ) (by using similar
idea, but instead of dividing a matrix into 4 quarters, people divide them into a bigger number of
submatrices).