Daaunit Iv2
Daaunit Iv2
blog: anilkumarprathipati.wordpress.com 1
UNIT-IV DYNAMIC PROGRAMMING
Example 1:- To illustrate the different costs incurred by different parenthesize of a matrix product, consider
the problem to find the product of three matrices A1, A2, A3 i.e. A1 * A2 * A3 of three matrices. Suppose that
the dimensions of the matrices are 10 × 100, 100 × 5, and 5 × 50, respectively. If we multiply according to
the parenthesization.
((A1 A2) A3) = 10 * 100 * 5 + 10 * 5 * 50 = 7500
(A1 (A2 A3)) = 10 * 100 * 50 +100 * 5 * 50 = 75,000
Thus, computing the product according to the first parenthesization is 10 times faster.
Definition:- The matrix-chain multiplication problem can be stated as follows: given a chain A 1, A2, ...,An
of n matrices, where for i = 1, 2, ..., n, matrix A i has dimension Pi-1 ×Pi, fully parenthesize the product A1 A2
An in a way that minimizes the number of scalar multiplications.
Note:- In the matrix-chain multiplication problem, we are not actually multiplying matrices. Our goal is only
to determine an order for multiplying matrices that has the lowest cost.
Solving the matrix-chain multiplication problem by dynamic programming
Step 1: The structure of an optimal parenthesization. Our first step in the dynamic-programming paradigm is
to find the optimal substructure and then use it to construct an optimal solution to the problem from optimal
solutions to sub problems. For the matrix-chain multiplication problem, we can perform this step as follows.
For example any parenthesization of the product Ai Aj must split the product between Ak and Ak+1 for
Ai+1
some integer k in the range i ≤ k < j. That is, for some value of k, we first compute the matrices Ai,k and Ak+1,j and
then multiply them together to produce the final product Ai,j. The cost of this parenthesization is thus the cost
of computing the matrix Ai,k, plus the cost of computing Ak+1,j, plus the cost of multiplying them together.
Step 2: A recursive solution. Next, we define the cost of an optimal solution recursively in terms of the
optimal solutions to sub problems. For the matrix-chain multiplication problem, We can define m[i, j]
recursively as follows. If i = j, the matrix Ai,j = Ai, so that no scalar multiplications are necessary to compute
the product. Thus, Mij = 0 for i = j
= min { Mi,k + Mk + 1, j + Pi-1 Pk Pj } for i < j
i<=k<j
Step 3: Computing the optimal costs. We perform the third step of the dynamic-programming paradigm and
compute the optimal cost by using a tabular, bottom-up approach.
Step 4: Constructing an optimal solution. In the first level we compare M12 and M23. When M12 < M23, we parenthesize
the A1A2 in the product A1A2A3 i.e (A1A2) A3 and parenthesize the A2A3 in the product A1A2A3 i.e., A1 (A2 A3)
when M12 >M23. This process is repeated until the whole product is parenthesized. The top entry in the table
i.e M13 gives the optimum cost of matrix chain multiplication.
Example:- Find an optimal parenthesization of a matrix-chain product whose dimensions are given in the
table below.
blog: anilkumarprathipati.wordpress.com 2
UNIT-IV DYNAMIC PROGRAMMING
Solution:- Given
P0=5-, P1=4, P2=6, P3=2, P4=7
The Bottom level of the table is to be initialized.
Mi,j=0 where i = j
In the first level when we compare M12 , M23 and M34. As M23=48 is minimum among three we parenthesize the QR
in the product PQRT i.e P(QR)T. In the second level when we compare M13 and M24. As M13=88 is minimum
among the two we parenthesize the P and QR in the product PQRT i.e (P(QR))T. Finally we parenthesize
the whole product i.e ((P(QR))T). The top entry in the table i.e M14 gives the optimum cost of ((P(QR))T).
Verification:- The chain of matrices is P, Q, R, T, the product P x Q x R x T can be fully parenthesized in
five distinct ways:
1. (P(Q(RT))) 2. (P((QR)T)) 3. ((PQ)(RT)) 4. ((P(QR))T) 5. (((PQ) R)T)
Cost of (P(Q(RT))) = 5*4*7 + 4*6*7 + 6*2*7= 392
Cost of (P((QR)T)) = 5*4*7 + 4*6*2 + 4*2*7= 244
Cost of ((PQ)(RT)) = 5*4*6 + 6*2*7 + 5*6*7 = 414
Cost of ((P(QR))T) = 5*4*2 + 4*6*2 + 5*2*7= 158
Cost of (((PQ) R)T) = 5*4*6 + 5*6*2 + 5*2*7 = 250
blog: anilkumarprathipati.wordpress.com 3
UNIT-IV DYNAMIC PROGRAMMING
From the above manual method also we find the optimal cost is 158 and the order of matrix
multiplication is ((P(QR))T)
Algorithm Matrix_Chain_Mul(p)
{
for i = 1 to n do
M[i, i] = 0
for len = 2 to n do
{
for i = 1 to n - len + 1 do
{
j←i+l-1
M[i, j] ← ∞
for k = i to j - 1 do
q = M[i, k] + M[k + 1, j] + Pi-1PkPj
if q < M[i, j] then
{
M[i, j] ← q
}
}
}
return m
}
Time Complexity:- Algorithm Matrix_Chain_Mul uses first For loop to initialize M[i,j] which takes O(n).
M[i, j] value is computed using three For loops which takes O(n3). Thus the overall time complexity of
Matrix_Chain_Mul is O(n3).
Application-II: OBST
Binary Search Trees: - A binary search tree T is a binary tree, either it is empty or each node in the tree
contains an indentifier and,
1.All identifiers in the left subtree of T are less than the identifier in the root node T.
2.All identifiers in the right subtree are greater than the identifier in the root node T.
3.The left and right subtree of T are also binary search trees.
Optimal Binary Search Tree problem:- Given a sequence K = {k1, k2, ..., kn } of n distinct keys in sorted
order (so that k1 < k2 < ··· < kn), and we wish to build a binary search tree from these keys such that the cost
of the binary search tree is minimum. For each key ki, we have a probability pi that a search will be for ki.
Some searches may be for values not in K, and so we also have n + 1 "dummy keys" E0, E1, E2, ..., En
representing values not in K. In particular, E0 represents all values less than k1, En represents all values
greater than kn, and for i = 1, 2, ..., n -1, the dummy key Ei represents all values between ki and ki+1. For
each dummy key Ei, we have a probability qi that a search will correspond to Ei.
Number of possible binary search trees for n keys is given by the following formula.
2nCn
Cost of binary search tree is calculated as follows
blog: anilkumarprathipati.wordpress.com 4
UNIT-IV DYNAMIC PROGRAMMING
into left sub tree i.e Ti,k-1 and into right sub tree i.e Tk,j-1. The process will be repeated until Tij is reached
where i=j. At this condition the tree will become a leaf node.
Algorithm OBST(p, q, n)
{
for i = 0 to n-1 do
{
w[i,i] = q[i]; c[i,i] = 0; r[i,i] = 0;
w[i,i+1] = q[i]+q[i+1]+p[i+1];
r[i,i+1] = i+1;
c[i,i+1] = q[i]+q[i+1]+p[i+1];
}
w[n,n] = q[n]; c[n,n] = 0; r[n,n] = 0;
for m = 2 to n do
for i = 0 to n - m do
{
j=i+m;
blog: anilkumarprathipati.wordpress.com 5