MCS-211 · Block 1 Unit 3

Complexity Analysis of Simple Algorithms

Section 4 of 8

Block 1 · Unit 3Complexity Analysis of Simple Algorithms8 min · Section 4 of 8

3.4Matrix Multiplication

Covers IGNOU MCS-211 §3.4.3

Matrices are tables of numbers used for graphics transformations, graph adjacency, systems of equations and machine learning. Multiplying two of them is one of the most common heavy computations in computing. This page analyses the standard method.

The problem

The algorithm

Matrix multiplication pseudocode
MatrixMultiply(A, B, n)
for i ← 0 to n − 1 do
    for j ← 0 to n − 1 do
        C[i][j] ← 0
        for k ← 0 to n − 1 do
            C[i][j] ← C[i][j] + A[i][k] * B[k][j]
return C

Analysis

  • The outer loop runs nn times; for each ii the middle loop runs nn times, so the assignment C[i][j] ← 0 runs n2n^2 times.
  • For each (i,j)(i, j) the inner loop runs nn times, so the multiply-add line runs n⋅n⋅n=n3n \cdot n \cdot n = n^3 times.
  • Total: n3n^3 multiplications, n3n^3 additions and n2n^2 initialisations, i.e. Θ(n3)\Theta(n^3).

The input itself has 2n22n^2 numbers, so the algorithm does about nn operations per input number.

C · matrix_mult.c
#include <stdio.h>

#define N 3

int main(void)
{
    int A[N][N] = {{1, 2, 3}, {2, 3, 4}, {4, 5, 6}};
    int B[N][N] = {{1, 1, 1}, {2, 3, 2}, {3, 2, 1}};
    int C[N][N];
    long mults = 0;

    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++) {
            C[i][j] = 0;
            for (int k = 0; k < N; k++) {
                C[i][j] += A[i][k] * B[k][j];
                mults++;
            }
        }

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++)
            printf("%4d", C[i][j]);
        printf("\n");
    }
    printf("multiplications: %ld = n^3 = %d\n", mults, N * N * N);
    return 0;
}
Output
  14  13   8
  20  19  12
  32  31  20
multiplications: 27 = n^3 = 27

This program answers Check Your Progress 2 with fixed matrices. The study material’s version reads the matrices with scanf; that works too, but a program that reads input should check that scanf returned 1 for each value.

Key takeaways

  • cij=∑kaikbkjc_{ij} = \sum_k a_{ik} b_{kj}: row ii of AA times column jj of BB.
  • Three nested loops of nn: n3n^3 multiplications and additions.
  • The standard algorithm is Θ(n3)\Theta(n^3).
  • Initialise each cijc_{ij} to 0 before summing.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

How many multiplications does the standard algorithm use for two 4×44 \times 4 matrices?

Show answer

Answer: C. 64

n³ = 4³ = 64. 16 = n² is the number of entries, each needing 4 multiplications.

Question 2 · multiple choice

Using the matrices AA and BB of the example, what is c23c_{23}?

Show answer

Answer: B. 12

Row 2 of A is (2, 3, 4) and column 3 of B is (1, 2, 1): 2 + 6 + 4 = 12. 19 is c₂₂.

Question 3 · multiple choice

Which line of the algorithm runs exactly n2n^2 times?

Show answer

Answer: A. C[i][j] ← 0

It is inside two loops (i and j) but not the k loop. The multiply-add line is inside all three and runs n³ times.

Question 4 · multiple choice

If nn doubles, the running time of the standard algorithm becomes about:

Show answer

Answer: C. 8 times larger

(2n)³ = 8n³. Four times would be the growth of a Θ(n²) algorithm.

Question 5 · multiple choice

To compute cijc_{ij} we combine:

Show answer

Answer: D. row ii of AA with column jj of BB

c_ij = Σ a_ik b_kj, where k runs along row i of A and down column j of B.

Question 6 · short answer

Multiply A=(1234)A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} and B=(5678)B = \begin{pmatrix} 5 & 6 \\ 7 & 8 \end{pmatrix} and state how many multiplications you used.

Show worked answer

c₁₁ = 1·5 + 2·7 = 19; c₁₂ = 1·6 + 2·8 = 22; c₂₁ = 3·5 + 4·7 = 43; c₂₂ = 3·6 + 4·8 = 50. So C = [[19, 22], [43, 50]]. Each entry used 2 multiplications, 8 = 2³ in total.

↑ ↓ move↵ openTab switch type