CLRS 4 1 2
CLRS 4 1 2
2
Sam Miller
October 26, 2022
Abstract
The author makes no claims that any of the below are correct in any
form or fashion. I would not copy these answers for any homework.
Generalize-Matrix-Multiply-Recursive(A, n)
1 if n == 1
2 // Base case.
3 c11 = c11 + a11 ∗ b11
4 return
5 if lg(n) is an integer
6 // Recursive case 1: power of 2
7 Matrix-Multiply-Recursive(A, n)
8 if lg(n) is not an integer
9 // Recursive case 2: Not a power of 2
10 Generalize-Matrix-Multiply-Recursive(A, n-1)
11 for i = 1 to n
12 for j = 1 to n
13 // Calculate values for column n
14 cin = cin + aij ∗ bjn − 1
15 // Calculate values for row n
16 cni = cni + anj ∗ bji
17 if i < n and j < n
18 cij = cij + ain ∗ bnj
19 if i == n and j == n
20 // Avoid duplicate calculation
21 cnn = cnn /2
1
We can use the original worst case running time of the Matrix-Multiply-
Recursive which is Θ(n3 ). If it’s not a power of 2, we add a double-loop of n
which is Θ(n2 ), so we are within a constant factor of n3 since n3 + n2 < 2 ∗ n3
and the asymptotic notation subsumes the smaller factor.
4.1.4 Matrix-Add-Recursive
Question: Write pseudo-code for a divide-and-conquer algorithm Matrix-Add-
Recursive that sums two n x n matrices A and B by partitioning each of them
into four n/2 × n/2 sub-matrices and then recursively summing corresponding
pairs of sub-matrices. Assume that matrix partitioning uses Θ(1)-time index
calculations. Write a recurrence for the worst-case running time of Matrix-
Add-Recursive and solve your recurrence. What happens if you use Θ(n2 -time
copying to implement the partitioning instead of index calculations?
2
Solution:
4.1.5 Pseudo-code
Matrix-Add-Recursive(A, B, C, n)
1 if n == 1
2 // Base case.
3 c11 = c11 + a11 + b11
4 // Divide
5 Partition by index into n/ × n/2 sub-matrices
6 A11 , A12 , A21 , A22 ; B11 , B12 , B21 , B22 ;
7 and C11 , C12 , C21 , C22 ,
8 // Conquer
9 Matrix-Add-Recursive(A11 , B11 , C11 )
10 Matrix-Add-Recursive(A12 , B12 , C12 )
11 Matrix-Add-Recursive(A21 , B21 , C21 )
12 Matrix-Add-Recursive(A22 , B22 , C22 )
4T (n/2) + Θ(1)
According to the master method:
3
4.2 Strassen’s Algorithm
4.2.1 Use Strassen’s Algorithm
Question:
Use Strassen’s algorithm to compute the matrix product
1 3 6 8
7 5 4 2
Show your work.
Solution:
Step 1: Partition into sub-matrices.
A11 = (1)
A12 = (3)
A21 = (7)
A22 = (5)
B11 = (6)
B12 = (8)
B21 = (4)
B22 = (2)
Step 2: Calculate S Values
S1 = B12 − B22 = (6)
S2 = A11 + A12 = (4)
S3 = A21 + A22 = (12)
S4 = B21 − B11 = (−2)
S5 = A11 + A22 = (6)
S6 = B11 + B22 = (8)
S7 = A12 − A22 = (−2)
S8 = B21 + B22 = (6)
S9 = A11 − A21 = (−6)
S10 = B11 + B12 = (14)
Step 3: Calculate P values
P1 = A11 ∗ S1 = (1) ∗ (6) = 6
P2 = S2 ∗ B22 = (4) ∗ (2) = 8
P3 = S3 ∗ B11 = (12) ∗ (6) = 72
P4 = A22 ∗ S4 = (5) ∗ (−2) = −10
P5 = S5 ∗ S6 = (6) ∗ (8) = 48
P6 = S7 ∗ S8 = (−2) ∗ (6) = −12
P7 = S9 ∗ S10 = (−6) ∗ (14) = −84
Step 4: Combine into C
C11 = C11 + P5 + P4 − P2 + P6 = 48 + −10 + −8 + −12 = 18
C12 = C12 + P1 + P2 = 6 + 8 = 14
C21 = C21 + P3 + P4 = 72 + −10 = 62
C22 = C22 + P5 + P1 − P3 − P7 = 48 + 6 + −72 + 84 = 54 + 12 = 66
4
18 14
C=
62 66
Strassen-Algorithm(A, B, C, n)
1 if n == 1
2 // Base case.
3 c11 = c11 + a11 ∗ b11
4 return
5 // Divide
6 Partition by index into n/2 × n/2 sub-matrices
7 A11 , A12 , A21 , A22 ; B11 , B12 , B21 , B22 ;
8 and C11 , C12 , C21 , C22
9 // Make S matrices
10 Matrix-Add-Recursive(B12 , −1 ∗ B22 , S1 , n/2)
11 Matrix-Add-Recursive(A11 , A12 , S2 , n/2)
12 Matrix-Add-Recursive(A21 , A22 , S3 , n/2)
13 Matrix-Add-Recursive(B21 , −1 ∗ B11 , S4 , n/2)
14 Matrix-Add-Recursive(A11 , A22 , S5 , n/2)
15 Matrix-Add-Recursive(B11 , B22 , S6 , n/2)
16 Matrix-Add-Recursive(A12 , −1 ∗ A22 , S7 , n/2)
17 Matrix-Add-Recursive(B21 , B22 , S8 , n/2)
18 Matrix-Add-Recursive(A11 , −1 ∗ A21 , S9 , n/2)
19 Matrix-Add-Recursive(B11 , B12 , S10 , n/2)
20 // Make P matrices
21 Strassen-Algorithm(A11 , S1 , P1 , n/2)
22 Strassen-Algorithm(S2 , B22 , P2 , n/2)
23 Strassen-Algorithm(S3 , B11 , P3 , n/2)
24 Strassen-Algorithm(A22 , S4 , P4 , n/2)
25 Strassen-Algorithm(S5 , S6 , P5 , n/2)
26 Strassen-Algorithm(S7 , S8 , P6 , n/2)
27 Strassen-Algorithm(S9 , S10 , P7 , n/2)
28 // Combine into C
29 Matrix-Add-Recursive(P5 , P4 , C11 , n/2)
30 Matrix-Add-Recursive(−1 ∗ P2 , P6 , C11 , n/2)
31 Matrix-Add-Recursive(P1 , P2 , C12 , n/2)
32 Matrix-Add-Recursive(P3 , P4 , C21 , n/2)
33 Matrix-Add-Recursive(P5 , P1 , C22 , n/2)
34 Matrix-Add-Recursive(−1 ∗ P3 , −1 ∗ P7 , C22 , n/2)
5
4.2.3 Largest k to multiply 3 × 3 matrices
Question:
What is the largest k such that if you can multiply 3 × 3 matrices using k
multiplications (not assuming commutativity of multiplication), then you can
multiply n × n matrices in o(nlg7 ) time? What is the running time of this
algorithm?
Solution:
3 × 3 matrices could be computed naively with n3 multiplications.
An n × n matrix could be computed with Strassen’s Algorithm in Θ(nlg7 )
time.
We can see from this that we are looking from the maximum n/k for which
we can get alg7 . a is the value of n/k which according to the problem statement
is 3. 3lg7 = 21.85 so the highest value k is 21.
6
We can do that with the following three multiplications:
P1 = a(c + b) = ac + bc
P2 = b(c + d) = bc + bd
P3 = d(a − b) = ad − bd
This would result in:
(ac − bd) = P1 − P2
(ad + bc) = P2 + P3