1 Matrix-Chain Multiplication
Let we have n matrices A1 , A2 , . . . , An . The size of matrix Ai is pi−1 × pi . The size of the matrices is stored in an
array p[0, 1, . . . , n] of size n + 1. The size of matrix Ai is pi−1 × pi which is basically pi−1 × pi when accessing the
size from the array.
1.1 Approach-1
Algorithm 1 is used to find the minimum cost to multiply matrices Ai . . . Aj . Our aim is to find the minimum cost
to multiply matrices A1 . . . An . So the initial call to Algorithm 1 is Matrix-Chain(p, 1, n).
Algorithm 1 Matrix-Chain(p[ ], i, j)
1: if i = j then . Single matrix
2: return 0
3: end if
4: minCost ← ∞ . Initialize the minimum cost to multiply matrices Ai . . . Aj
5: for k ← i to j − 1 do . Check for all possible parenthesis between Ai . . . Aj
6: cost ← Matrix-Chain(p, i, k) + Matrix-Chain(p, k + 1, j) +
| {z } | {z }
Minimum cost to multiply Ai ...Ak Minimum cost to multiply Ak+1 ...Aj
pi−1 pk pj
| {z }
Cost to multiply two matrices [Ai . . . Ak ] and [Ak+1 . . . Aj ]
| {z } | {z }
size=pi−1 ×pk size=pk ×pj
7: if cost < minCost then . Cost using current parenthesis is less than the minimum cost
8: minCost ← cost . Update minimum cost
9: end if
10: end for
11: return minCost . Return the minimum cost to multiply matrices Ai . . . Aj
n−1
X
T (n) = [T (k) + T (n − k) + O(1)]
k=1
"n−1 #
X
= (T (k) + T (n − k)) + (n − 1)
k=1
" n−1
#
X
= 2 T (k) + (n − 1) (1)
k=1
" n−2
#
X
T (n − 1) = 2 T (k) + (n − 2) (2)
k=1
T (n) − T (n − 1) = 2T (n − 1) + 1
T (n) = 3T (n − 1) + 1 (3)
1
T (n) = 3T (n − 1) + 1
= 3 [3T (n − 2) + 1] + 1
= 32 T (n − 2) + (1 + 3)
= 32 [3T (n − 3) + 1] + (1 + 3)
= 33 T (n − 3) + (1 + 3 + 32 )
.. ..
.=.
= 3n−1 T (1) + (1 + 3 + 32 + · · · + 3n−2 )
3n−1 − 1
=0+ T (1) = 0
3−1
1 n−1
= 3 −1 (4)
2
2
1.2 Approach-2
Algorithm 2 is used to find the minimum cost to multiply matrices Ai . . . Aj . Our aim is to find the minimum
cost to multiply matrices A1 . . . An . So the initial call to Algorithm 2 is Matrix-Chain-Store(p, 1, n). In this
Algorithm, we are storing the cost in a 2D-array (say T) of size n × n and initialize it with −1 (any negative value)
because the cost to multiply matrices can be 0 or more.
Algorithm 2 Matrix-Chain-Store(p[ ], i, j)
1: if i = j then . Single matrix
2: Tcost [i][j] ← 0
3: return 0
4: end if
5: if Tcost [i][j] 6= −1 then . Minimum cost to multiply matrices Ai . . . Aj is already computed
6: return Tcost [i][j] . Retrieve minimum cost to multiply matrices Ai . . . Aj from the table and return
7: end if
8: minCost ← ∞ . Initialize the minimum cost to multiply matrices Ai . . . Aj
9: for k ← i to j − 1 do . Check for all possible parenthesis between Ai . . . Aj
10: if Tcost [i][k] 6= −1 then . Minimum cost to multiply matrices Ai . . . Ak is already computed
11: cost1 ← Tcost [i][k] . Retrieve minimum cost to multiply matrices Ai . . . Ak from the table
12: else . Minimum cost to multiply matrices Ai . . . Ak is not computed
13: cost1 ← Matrix-Chain-Store(p, i, k) . Obtain the minimum cost to multiply matrices Ai . . . Ak
| {z }
Minimum cost to multiply Ai ...Ak
14: Tcost [i][k] ← cost1 . Store the minimum cost to multiply matrices Ai . . . Aj in the table.
This step can be ignored because while computing the cost, the algorithm stores it in the table before
returning the cost (See line no. 27 of this algorithm)
15: end if
16: if Tcost [k + 1][j] 6= −1 then . Minimum cost to multiply matrices Ak+1 . . . Aj is already computed
17: cost2 ← Tcost [k + 1][j] . Retrieve minimum cost to multiply matrices Ak+1 . . . Aj from the table
18: else . Minimum cost to multiply matrices Ak+1 . . . Aj is not computed
19: cost2 ← Matrix-Chain-Store(p, k + 1, j) . Obtain the minimum cost to multiply matrices Ak+1 . . . Aj
| {z }
Minimum cost to multiply Ak+1 ...Aj
20: Tcost [k + 1][j] ← cost2 . Store the minimum cost to multiply matrices Ak+1 . . . Aj in the table.
This step can be ignored because while computing the cost, the algorithm stores it in the table before
returning the cost (See line no. 27 of this algorithm)
21: end if
22: cost ← cost1 + cost2 + pi−1 pk pj . Cost to multiply Ai . . . Aj
| {z }
Cost to multiply two matrices [Ai . . . Ak ] and [Ak+1 . . . Aj ]
| {z } | {z }
size=pi−1 ×pk size=pk ×pj
23: if cost < minCost then . Cost using current parenthesis is less than the minimum cost
24: minCost ← cost . Update minimum cost
25: end if
26: end for
27: Tcost [i][j] ← minCost . Store the minimum cost to multiply matrices Ai . . . Aj in the table
28: return minCost . Return the minimum cost to multiply matrices Ai . . . Aj