Uploaded by Manmeet Singh

COLLATE ADA UNIT 1 NOTES

advertisement
COLLATE
(Click me)
Algorithm Design
and Analysis
(Click me)
Notes
UNIT - 1
(Click me)
SOURCE : NOTESHUB
Collate
Unit -1
Strassen Matrix Multiplication
Divide-and-Conquer algorithsm for matrix multiplication
A11 A12
B11 B12
A=
A21 A22 B =
12 21 22
Formulas for C , C , C , C :
11
C 11 C12
B21 B22
C=A×B=
C 21 C22
C 11 = A11B11 + A12B21
C 12 = A11B12 + A12B22
C 21 = A21B11 + A22B21
C 22 = A21B12 + A22B22
The First Attempt Straightforward from the formulas above (assuming that N is a power of 2):
MMult(A, B, N)
If N = 1 Output A × B
* Else
11
11
22
22
Compute A , B , . . . , A , B
% by computing M = N/2
11 11
X1 ← M M ULT(A , B , N/2)
12 21
X2 ← M M ULT(A , B , N/2)
11 12
X3 ← M M ULT(A , B , N/2)
12 22
X4 ← M M ULT(A , B , N/2)
21 11
X5 ← M M ULT(A , B , N/2)
22 21
X6 ← M M ULT(A , B , N/2)
21 12
•
X7 ← M M ULT(A , B , N/2)
22 22
•
X8 ← M M ULT(A , B , N/2)
11
•
C ← X1 + X2
12
•
C ← X3 + X4
21
•
C ← X5 + X6
22
•
C ← X7 + X8
•
Output C
• End If
2
Analysis: The operations on line 3 take constant time. The combining cost (lines 12–15) is Θ(N ) (adding
2
N
N
N
two 2 × 2 matrices takes time 42 = Θ(N )). There are 8 recursive calls (lines 4–11). So let T (N) be the
total number of mathematical operations performed by MMult(A, B, N), then
3.
•
•
•
•
•
•
T (N) = 8T (
N
2
2 ) + Θ(N
)
The Master Theorem gives us
log (8)
T (N) = Θ(N
2
3
) = Θ(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:
C
C
11
21
=P5 + P4 − P2 + P6
C
=P3 + P4
C
12
22
=P1 + P2
=P1 + P5 − P3 − P7
where
11
12
P1 = A (B
11
12
22
22
11
P2 = (A
+ A )B
P3 = (A
+ A )B
21
22
21
P4 = A (B
11
11
22
−B )
11
−B )
22
11
22
21
21
11
P5 = (A
+ A )(B
P6 = (A
12
− A )(B
P7 = (A
11
− A )(B
22
+B )
22
+B )
12
+B )
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
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
11
11
22
22
Compute A , B , . . . , A , B
% by computing M = N/2
11 12
22
P1 ← STRASSEN(A , B − B )
11
12 22
P2 ← STRASSEN(A + A , B )
21
22 11
P3 ← STRASSEN(A + A , B )
22 21
11
P4 ← STRASSEN(A , B − B )
11
22 11
22
P5 ← STRASSEN(A + A , B + B )
12
22 21
22
P6 ← STRASSEN(A − A , B + B )
11
21 11
12
P7 ← STRASSEN(A − A , B + B )
11
C ← P5 + P4 − P2 + P6
12
C ← P1 + P2
21
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
T (N) = 7T (
2
2
2 ) + Θ(N
)
The Master Theorem gives us
log (7)
T (N) = Θ(N
2
2.8
) = Θ(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).
Download