Multiplying two matrices is a classic example of nested loops and shows how the loop structure directly gives the complexity.
The Classical Algorithm
To multiply two n × n matrices A and B into C we compute each of the n² entries as C[i][j] = Σ A[i][k] · B[k][j] for k from 1 to n. That needs three nested loops: i over rows, j over columns, k over the sum. The innermost statement C[i][j] += A[i][k] * B[k][j] executes n × n × n = n³ times, so the number of multiplications is n³ and the number of additions is n²(n − 1). The time complexity is Θ(n³) in the best, worst and average cases, because the loops do not depend on the data values.
Space Needed
The result matrix needs n² elements, and the inputs also take 2n² . Beyond these, only the loop counters are needed. The space complexity is therefore Θ(n²), where the extra space apart from the output is O(1).
Non-square Matrices
For an A of size m × p and B of size p × q, the result is m × q, and the innermost statement runs m·p·q times. Multiplication is possible only when the number of columns of A equals the number of rows of B. If one matrix is much smaller than the other, the count is correspondingly lower, and it is worth choosing the order of multiplication when chaining many matrices, which leads to the matrix-chain problem in dynamic programming.
Can We Do Better
The cubic algorithm looks natural, but is it the best? Every algorithm must at least read the 2n² input numbers and write the n² output numbers, so the lower bound is Ω(n²). The gap between n² and n³ has led to cleverer methods: Strassen's algorithm (Unit II) reduces the exponent to about 2.81 by replacing 8 sub-multiplications by 7; later research has pushed it down to about 2.37, though those methods are not practical for ordinary sizes.
Definition: Matrix multiplication cost. The classical method of multiplying two n × n matrices performs n³ scalar multiplications and so has time complexity Θ(n³).
Table 1.4: Operation counts by order
| n | Multiplications n³ | Result entries n² |
|---|---|---|
| 2 | 8 | 4 |
| 10 | 1,000 | 100 |
| 100 | 1,000,000 | 10,000 |
Example 1.4: Multiplying and counting
Problem: Multiply A = [[1, 2], [3, 4]] by B = [[5, 6], [7, 8]] and count the scalar multiplications.
Solution:
- C[0][0] = 1·5 + 2·7 = 19; C[0][1] = 1·6 + 2·8 = 22.
- C[1][0] = 3·5 + 4·7 = 43; C[1][1] = 3·6 + 4·8 = 50.
- Each of the 4 entries needs 2 multiplications, so the total is 8 = 2³.
Result: C = [[19, 22], [43, 50]] with 8 multiplications.
def matmul(A, B):
n, ops = len(A), 0
C = [[0] * n for _ in range(n)]
for i in range(n):
for j in range(n):
for k in range(n):
C[i][j] += A[i][k] * B[k][j]
ops += 1
return C, ops
C, ops = matmul([[1, 2], [3, 4]], [[5, 6], [7, 8]])
print(C, "multiplications =", ops)
for n in (4, 8, 16):
I = [[1] * n for _ in range(n)]
print("n =", n, "ops =", matmul(I, I)[1])
Output
[[19, 22], [43, 50]] multiplications = 8
n = 4 ops = 64
n = 8 ops = 512
n = 16 ops = 4096
Common mistakes
- Saying matrix multiplication is O(n²) because the result has n² entries.
- Multiplying matrices whose inner dimensions do not match.
- Forgetting that the n³ count does not depend on the values stored.
Key takeaways
- Classical multiplication has three nested loops and performs n³ multiplications: Θ(n³).
- For m×p and p×q matrices the count is m·p·q.
- Lower bound is Ω(n²) because all entries must be read and written.
- Strassen's method lowers the exponent below 3.
Practice questions
- How many multiplications multiply two 10 × 10 matrices classically?
- What is the lower bound for matrix multiplication?
- When can A (m × p) be multiplied by B (r × q)?
Answers to the practice questions
- 10³ = 1000.
- Ω(n²), since all input and output entries must be handled.
- Only if p = r.
Sources
- Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit I
