0% found this document useful (0 votes)
1K views

CLRS 4 1 2

This document discusses algorithms for matrix multiplication and summarizes solutions to related questions. It begins by generalizing the recursive matrix multiplication algorithm to work for non-power-of-two matrices, and discusses the running time of Θ(n3). It then covers Strassen's algorithm for matrix multiplication using only 7 multiplications instead of 8, providing pseudo-code. Finally, it discusses how to multiply complex numbers using only 3 real number multiplications by expanding the terms.

Uploaded by

MathKilledYouBro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

CLRS 4 1 2

This document discusses algorithms for matrix multiplication and summarizes solutions to related questions. It begins by generalizing the recursive matrix multiplication algorithm to work for non-power-of-two matrices, and discusses the running time of Θ(n3). It then covers Strassen's algorithm for matrix multiplication using only 7 multiplications instead of 8, providing pseudo-code. Finally, it discusses how to multiply complex numbers using only 3 real number multiplications by expanding the terms.

Uploaded by

MathKilledYouBro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CLRS Chapter 4.1 & 4.

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.

4.1 Matrix Multiply Recursive


4.1.1 Generalize Matrix Multiply Recursive
Question: Generalize Matrix-Multiply-Recursive to multiply n × n for
which n is not an exact power of 2. Give a recurrence describing its running
time. Argue Θ(n3 ) worst case running time.
Solution:

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.2 Multiplying k n × n matrices


Question: How quickly can you multiply a k n × n matrix (k n rows and n
columns) by an n × k n matrix, where k >= 1, using Matrix-Multiply-
Recursive as a subroutine? Answer the same question for multiplying an
n × k n matrix by a k n × n matrix. Which is asymptotically faster, and by how
much?
Solution: The basic intuition here is that the former will result in an n × n
matrix and the latter will result in a k n × k n matrix. This means that the
first calculation will take Θ(kn3 ) time, while the second will take Θ(k 2 n3 ) time,
which is slower by a factor of k.

4.1.3 Copying vs. Index Calculation


Question: Suppose that instead of partitioning matrices by index calculation
in Matrix-Multiply-Recursive, you copy the appropriate elements of A, B,
and C into separate n/2 × n/2 sub-matrices A11 , A12 , A21 , A22 ; B11 , B12 , B21 ,
B22 ; and C11 , C12 , C21 , C22 , respectively. After the recursive calls, you copy
the results from C11 , C12 , C21 , and C22 back into the appropriate places in C.
How does recurrence (4.9) change and what is its solution?
Solution: This requires adding two separate steps to the algorithm. First,
partitioning does not take Θ(1) time because we have to copy each element.
That involves a double-nested loop which would be Θ(n2 ). Combining also takes
Θ(n2 ), which gives a constant factor of 2 and is subsumed by the asymptotic
notation. We can evaluate 4.9 to equal:
8T (n/2) + Θ(n2 )
This is actually confirmed in the third edition of the book. ”If we were to
create 12 new n/2 × n/2 matrices, we would spend Θ(n2 ) time copying entries.
In the third edition, equation 4.16 gives the above adjustment to the equation
of 4.9.

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 )

Recurrence for the worst case running time:

4T (n/2) + Θ(1)
According to the master method:

O(1) = O(nlog2 2−1 ) = O(n0 ) = O(1)

Resulting recurrence is Θ(n2 ).

4.1.6 Impact of Copying vs. Index Calculation


Since copying also takes Θ(n2 )-time, that creates a constant factor of 2 which
can be subsumed by the asymptotic notation, resulting in Θ(n2 ).

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

4.2.2 Pseudo-code for Strassen’s Algorithm


Question: Write pseudo-code for Strassen’s algorithm.
Solution:

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.

4.2.4 V.Pan’s Discovery


Question:
V. Pan discovered a way to multiply 68×68 matrices using 132, 464 multipli-
cations, 70 × 70 using 143, 640 multiplications and a way of multiplying 72 × 72
matrices using 155, 424 multiplications. Which method yields the best asymp-
totic running time when used in a divide-and-conquer matrix-multiplication
algorithm? How does it compare with Strassen’s algorithm?
Solution:
The middle method is the best... and it is better than Strassen’s (all three
are).

Value V. Pan Θ Strassen


68 132, 464 n2.795128 141, 043
70 143, 640 n2.795122 153, 012
72 155, 424 n2.795147 165, 617

The recent development from Google of the AlphaTensor algorithm is able


to reduce the computing time to n2.78 for a 4 × 4 matrix, which would be better
than all of the above.
See: https://github.com/deepmind/alphatensor

4.2.5 Multiply complex numbers


Question: Show how to multiply the complex numbers a + bi and c + di using
only three multiplications of real numbers. The algorithm should take a, b,
c, and d as input and produce the real component ac − bd and the imaginary
component ad + bc separately.
Solution: We want to construct the expanded value:

(a + bi) ∗ (c + di) = (ac − bd) ∗ (ad + bc) ∗ i

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

4.2.6 Θ(na ) Algorithm


Question:
Suppose that you have a Θ(na )-time algorithm for squaring n × n matrices,
where a >= 2. Show how to use that algorithm to multiply two different n × n
matrices in Θ(na ) time.
Solution:
Just substitute the other matrix for the second nxn matrix. I am sure that
this answer is too simple to be correct.

You might also like