Design and Analysis of Algorithms Assignment 3
Design and Analysis of Algorithms Assignment 3
Design and Analysis of Algorithms Assignment 3
Assignment 3
Understanding:
Goal: Find the most efficient way to multiply a sequence of matrices, minimizing scalar
multiplications.
Overlapping Subproblems: Optimal solutions for larger sub chains often reuse those for smaller ones.
Optimal Substructure: The optimal solution for a larger problem can be built from optimal solutions
of smaller ones.
Solution:
Recursive Structure:
a. m[i][j] represents the minimum cost to multiply matrices from i to j.
b. m[i][j] = min(m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]), for k from i to j-1
Memorization or Tabulation:
a. Store results of subproblems to avoid redundant calculations.
b. Fill a table m[i][j] in a bottom-up manner.
Steps:
Initialize: Create tables m (n x n) for minimum costs and p (n+1) for dimensions.
Base Cases: m[i][i] = 0 (single matrix costs 0).
Fill Table: Iterate through m, filling entries using the recursive formula.
Find Minimum Cost: The minimum cost is in m[1][n].
Explanation:
The recursive formula explores different splits of the matrix chain to find the most efficient one.
Memorization/tabulation avoids recalculations.
The bottom-up approach builds solutions for larger subproblems from smaller ones.
We have:
p0 = 5
p1 = 10
p2 = 3
p3 = 12
p4 = 5
p5 = 50
p6 = 6
From the algorithm, we have, for all x, m[x,x] = 0. m[1,2] = m[1,1] + m[2,2] + p0 * p1 * p2
m[1,2] = 0 + 150
m[1,2] = 150
M 1 2 3 4 5 6
6 2010 1950 1770 1840 1500 0
5 1655 2430 930 3000 0
4 405 330 180 0
3 330 360
2 150
1 0 The minimum cost is therefore
2010 and the optimal
And using this, we construct the S table:
parenthesizing is:
((A1 * A2) * (A3 * A4) * (A5 *
S 1 2 3 4 5 A6)).
6 2 2 4 4 5
5 4 2 4 4
4 2 2 3
3 2 2
2 1