The complexity of matrix multiplication of two matrices A and B whose orders are m × n and n × p respectively is
- (A)
O(m × p)
- (B)
O(m × n² × p)
- (C)
O(m × n × p²)
- (D)
O(m × n × p)
The complexity of matrix multiplication of two matrices A and B whose orders are m × n and n × p respectively is
O(m × p)
O(m × n² × p)
O(m × n × p²)
O(m × n × p)
ANSWER
The complexity is , option (d).
Official answer: DSource: ISRO's revised and final answer key of 16 February 2024
Let be and be . The product is an matrix.
Each entry is , which takes multiplications and additions, so work per entry.
There are entries, so the total work is multiplications, that is with the standard triple loop (over , , , running to , , respectively).
Option (a), , only counts the entries and ignores the inner sum of length .
Options (b) and (c) square or , which no loop in the algorithm does: each of the three loops runs once over its own range.
So the complexity is , option (d).
KEY IDEA
Count the entries of the result () and multiply by the length of the inner sum for each entry (): three nested loops, each running once over its own dimension.
WATCH OUT FOR
Choosing because the result has entries, forgetting that every entry itself needs a sum of products; or squaring a dimension, which no loop does.
From the ISRO CS 2023 paper. Every question of this paper · The full paper (PDF, ISRO) · All 2 ISRO Complexity questions, with notes