Dynamic Programming
Dynamic Programming
INTRODUCTION
The drawback of the greedy method is, we will make one decision at a time. This can be
overcome in dynamic programming. In this, we will make more than one decision at a time.
Dynamic programming is an algorithm design method that can be used when the solution to a
problem can be viewed as the result of a sequence of decisions. Dynamic programming is
applicable when the sub-problems are not independent, that is when sub-problems share sub-
sub-problems. A dynamic programming algorithm solves every sub-sub-problem just once
and then saves its answer in a table, thereby avoiding the work of re-computing the answer
every time the sub-problem is encountered.
Definition:
It is a programming technique in which a solution is obtained from a sequence of decisions
A dynamic programming problem can be divided into a number of stages where an optimal
decision must be made at each stage. The decision made at each stage must take into account
its effects not only on the next stage, but also on the entire subsequent stages. Dynamic
programming provides a systematic procedure whereby starting with the last stage of the
problem and working backwards one makes an optimal decision for each stage of problem.
The information for the last stage is the information derived from the previous stage.
The Dynamic programming technique was developed by Bellman based upon his principle
known as principle of optimality. This principle states that “An optimal policy has the
property that, whatever the initial decisions are, the remaining decisions must constitute an
optimal policy with regard to the state resulting from the first decision”.
General Characteristics of Dynamic Programming:
The general characteristics of Dynamic programming are
1) The problem can be divided into stages with a policy decision required at each stage.
2) Each stage has number of states associated with it.
3) Given the current stage an optimal policy for the remaining stages is independent of
the policy adopted.
4) The solution procedure begins be finding the optimal policy for each state of the last
stage.
5) A recursive relation is available which identifies the optimal policy for each stage
with n stages remaining given the optimal policy for each stage with (n-1) stages
remaining.
Input: n matrices A1, A2, A3….An of dimensions P1xP2, P2xp3, ….PnxPn+1 respectively.
Goal: To compute the matrix product A1A2…An.
Problem: In what order should A1A2…An be multiplied so that it would take the minimum number of
computations to derive the product.
Let A and B be two matrices of dimensions pxq and qxr. Then C=AB. C is of dimension pxr. Thus Cij
takes q scalar multiplications and q-1 scalar additions.
Step 2: The sequence of decisions can be build using the principle of optimality. Consider the process of
matrix chain multiplication. Let T be the tree corresponding to the optimal way of multiplying Ai…Aj .
T has a left sub-tree L and right sub-tree R.
L corresponds to multiplying Ai…Ak and R to multiplying Ak+1…Aj. for some integer k such that
(i<=k<=j-1).
Thus we get optimal sub-chains of matrices and then the multiplication is performed. This ultimately
proves that the matrix chain multiplication follows the principle of optimality.
Step 3: We will apply following formula for computing each sequence.
For which value of k the m[i,j] entry is minimum, put that k value in s[i,j] entry.
Algorithm for Computing the optimal cost
MATRIX-CHAIN-ORDER(p)
1 n ← length[p] - 1
2 for i ← 1 to n
3 do m[i, i] ← 0
4 for l ← 2 to n //l is the chain length.
5 do for i ← 1 to n - l + 1
6 do j ← i + l - 1
7 m[i, j] ← ∞
8 for k ← i to j - 1
9 do q ← m[i, k] + m[k + 1, j] + pi-1 pkpj
10 if q < m[i, j]
11 then m[i, j] ← q
12 s[i, j] ← k
13 return m and s
Algorithm for Opitimal parenthesization
PRINT-OPTIMAL-PARENS(s, i, j)
1 if i = j
2 then print "A"i
3 else print "("
4 PRINT-OPTIMAL-PARENS(s, i, s[i, j])
5 PRINT-OPTIMAL-PARENS(s, s[i, j] + 1, j)
6 print ")"
Example:
Find a minimum number of multiplications required to multiply: A [1 × 5], B
[5 × 4], C [4 × 3], D [3 × 2], and E [2 × 1]. Also, give optimal parenthesization.
Solution:
Here d = {d , d , d , d , d , d } = {1, 5, 4, 3, 2, 1}
0 1 2 3 4 5
= {0 + 0 + 1 × 5 × 4}
= 20
m[2, 3] = {m[2, 2] + m[3, 3] + d x d x d }
1 2 3
= {0 + 0 + 5 × 4 × 3}
= 60
m[3, 4] = {m[3, 3] + m[4, 4] + d x d x d }
2 3 4
= {0 + 0 + 4 × 3 × 2}
= 24
m[4, 5] = {m[4, 4] + m[5, 5] + d x d x d }
3 4 5
= {0 + 0 + 3 × 2 × 1}
=6
In the above example the call MATRIX-CHAIN-MULTIPLY(A, s, 1, 5) computes the matrix-chain
product according to the parenthesization
Definition
Given two sequences X=x1x2x3…xm and Y=y1y2 y3…yn and find a longest subsequence
Z=z1z2z3…zk that is common to both subsequence X and Y.
Brute-force approach to solve LCS problem
Check every subsequence of x[1 . . m] to see if it is also a subsequence of y[1 . . n], keeping
track of the longest subsequence found.
Example
Determine an LCS of < A, B, C > and < B, A, C >
Solution
Let X=< A, B, C > and Y=< B, A, C >
Find all possible subsequences of < A, B, C >
They are
A
B
C
AB
BC
AC
ABC
EMPTY
Now take every subsequence of X and check whether it is also a subsequence of Y.
A √ 1
B √ 1
C √ 1
AB × -
BC √ 2
AC √ 2
ABC × -
Analysis
i) There are 2m possible subsequences of X.
iii) So total time in worst case by brute force method will take = O(n 2 m)
iv) This approach requires exponential time, making it impractical for long
sequences.
Let X =< x1, x2, ..., xm> and Y = <y1, y2, ..., yn> be sequences, and let Z = <z1, z2, ..., zk> be any
LCS of X and Y.
1. If xm = yn, then zk = xm = yn and Zk-1 is an LCS of Xm-1 and Yn-1.
2. If xm ≠ yn, then zk ≠ xm implies that Z is an LCS of Xm-1 and Y.
3. If xm ≠ yn, then zk ≠ yn implies that Z is an LCS of X and Yn-1.
0 if i = 0 or j = 0
LCS-LENGTH (X, Y)
1 m ← length[X]
2 n ← length[Y]
3 for i ← 1 to m
4 do c[i, 0] ← 0
5 for j ← 0 to n
6 do c[0, j] ← 0
7 for i ← 1 to m
8 do for j ← 1 to n
9 do if xi = yj
10 then c[i, j] ← c[i - 1, j - 1] + 1
11 b[i, j] ← ""
12 else if c[i - 1, j] ≥ c[i, j - 1]
13 then c[i, j] ← c[i - 1, j]
14 b[i, j] ← "↑"
15 else c[i, j] ← c[i, j - 1]
16 b[i, j] ← ←
17 return c and b
Running time of this algorithm(LCS-LENGTH) = O(mn)
PRINT-LCS(b, X, i, j)
1 if i = 0 or j = 0
2 then return
3 if b[i, j] = ""
4 then PRINT-LCS(b, X, i - 1, j - 1)
5 print xi
6 elseif b[i, j] = "↑"
7 then PRINT-LCS(b, X, i - 1, j)
8 else PRINT-LCS(b, X, i, j - 1)
Example
Determine an LCS of X=<A, B, C, B, D, A, B> AND y=<B, D, C, A, B, A>
Solution
i. Count the length of X and Y, These are m=7 and n=6 respectively.
ii. Draw a 8x7 table. 8 rows=7+1 7 columns=6+1, Length of x is m means row will be
m+1 abd Length of Y is n means column will be n+1
iii. Number row as i from 0 to m and column as j from 0 to n. In this case I varies from 0 to
7 and j varies from 0 to 6.
iv. Write symbols of X in row side each row one symbol. Write symbols of Y on above
the column, each column above write one symbol as follows.
v. Draw one table which will serve for two tables c table and b table. C table entries
contain numeric values and b table entries contain arrow symbols.
vii. c[i, j] values in the c table, entries are computed in row-major order. (That is, the first
row of c is filled in from left to right, then the second row, and so on.)
viii. How to fill up the table with valid entries refer class note and remember class room
discussion.
ix. To reconstruct the elements of an LCS, follow the b[i, j] arrows from the lower right-
hand corner; the path is shaded.
The tour of graph G is a directed simple cycle that includes every vertex in V. The
cost of the tour is the sum of cost of the edges on the tour. The travelling sales-
person problem is to find a tour of minimum cost.
Applications: Suppose we have to route a postal van to pick up mail from mailbox
located at ‘n’ different sites. An (n+1) vertex graph can be used to represent the
situation. One vertex represents the post office from which the postal van starts and
to which it must return. Edge <i,j> is assigned a cost equal to the distance from site
‘I’ to site ‘j’. Route taken by the postal van is a tour and we are interested in finding
tour of minimum length.
Every tour consist of an edge <1, k> for some k Є V-{1} and a path from vertex k to
vertex 1, which goes through each vertex in V-{1,k} exactly once. It is easy to see
that if the tour is optimal, then the path from k to 1 must be a shortest k to 1 path
going through all vertices in V-{1, k}. Hence, the principle of optimality holds.
Let g(i,S) be the length of a shortest path starting at vertex ‘i’ going through all
vertices in ‘S’ & terminating at vertex 1. The function g(1,V-{1}) is the length of an
optimal salesperson tour. From the principle of optimality
g (1, V {1})
m in { C1 k g ( k , V {1, k })}
2 k n
j={3,4}
g(2,{3,4}) = min c23 + g(3,s-{3}) when j=3
c24 + g(4,s-{4}) when j=4
g (2,ø)=C21 =1
g (3,ø)=C31 =6
g (4,ø)=C41 =1
g (5,ø)=C51 =3
`
g(4,{3,5})= min C43 + g(3,S-{3})
C45 + g(5,S-{5})
The all pair shortest path problem is to determine a matrix A such that A(i,j) is the
length of a shortest path from vertex i to j. Assume that this path contains no
cycles. If k is an intermediate vertex on this path, then the sub paths form i to k
and from k to j are the shortest paths from I to k and from k to j respectively.
Otherwise the path from i to j is not shortest path. If k is intermediate vertex with
highest index then the path i to k is the shortest path going through no vertex with
index greater than k-1. similarly the path k to j is shortest path going through no
vertex with index greater than k-1.
Example : Compute all pairs shortest path for the following graph
1 2
4
11
2
3
3
Graph G
0 4 11
cost Adjacency Matrix A0 (i, j) W(i, j) 6 0 2
3 0
Step 1
For k=1 i.e. going from i to j through intermediate vertex 1
When i=1 j=1/2/3
A1(1,1)min{A0(1,1), A0(1,1) A0(1,1)} min{0,0 0} 0
A1(1,2) min{A0(1, 2), A0(1,1) A0(1,2)} min{4,0 4} 4
A1(1,3) min{A0(1,3), A0(1,1) A0(1,3)} min{11,0 11} 11
When i=2 j=1/2/3
A1(2,1) min{A0(2,1), A0(2,1) A0(1,1)} min{6,6 0} 6
A1(2, 2) min{A0(2, 2), A0(2,1) A0(1, 2)} min{0,6 4} 0
A1(2,3) min{A0(2,3), A0(2,1) A0(1,3)} min{2,611} 2
When i=3 j=1/2/3
A1(3,1) min{A0(3,1), A0(3,1) A0(1,1)} min{3,3 0} 3
A1(3,2) min{A0(3, 2), A0(3,1) A0(1,2)} min{,3 4} 7
A1(3,3) min{A0(3,3), A0(3,1) A0(1,3)} min{0,311} 0
0 4 11
cost Adjacency Matrix A1 (i, j) 6 0 2
3 7 0
Step 2
For k=2 i.e. going from i to j through intermediate vertex 2
When i=1 k=2 j=1/2/3
Ak (i, j) min{Ak1(i, j), Ak1(i, k) Ak1(k, j)}
A2(1,1) min{A1(1,1), A1(1,2) A1(2,1)} min{0,4 6} 0
A2(1,2) min{A1(1, 2), A1(1,2) A1(2, 2)} min{4,4 0} 4
A2(1,3) min{A1(1,3), A1(1, 2) A0(2,3)} min{11, 4 2} 6
When i=2 j=1/2/3
A2(2,1) min{A1(2,1), A1(2, 2) A1(2,1)} min{6,0 6} 6
A2(2, 2) min{A1(2, 2), A1(2, 2) A1(2, 2)} min{0,0 0} 0
A2(2,3) min{A1(2,3), A1(2, 2) A1(2,3)} min{2,0 2} 2
When i=3 j=1/2/3
A2(3,1) min{A1(3,1), A1(3, 2) A1(2,1)} min{3,7 6} 3
A2(3, 2) min{A1(3,2), A1(3, 2) A1(2, 2)} min{7,7 0} 7
A2(3,3) min{A1(3,3), A1(3,2) A1(2,3)} min{0,0 2} 0
0 4 6
cost Adjacency Matrix A2 (i, j) 6 0 2
3 7 0
Step 3
For k=3 i.e. going from i to j through intermediate vertex 3
When i=1 k=3 j=1/2/3
Ak (i, j) min{Ak1(i, j), Ak1(i, k) Ak1(k, j)}
A3(1,1) min{A2(1,1), A2(1,3) A2(3,1)} min{0,6 3} 0
A3(1,2) min{A2(1, 2), A2(1,3) A2(3, 2)} min{4,6 7} 4
A3(1,3) min{A2(1,3), A2(1,3) A2(3,3)} min{6,6 0} 6
When i=2 j=1/2/3
A3(2,1) min{A2(2,1), A2(2,3) A2(3,1)} min{6, 2 3} 5
A3(2, 2) min{A2(2, 2), A2(2,3) A2(3, 2)} min{0,2 7} 0
A3(2,3) min{A2(2,3), A2(2,3) A2(3,3)} min{2,2 0} 2
When i=3 j=1/2/3
A3(3,1) min{A2(3,1), A2(3,3) A2(3,1)} min{3,0 3} 3
A3(3, 2) min{A2(3, 2), A2(3,3) A3(3, 2)} min{7,0 7} 7
A3(3,3) min{A2(3,3), A2(3,3) A3(3,3)} min{0,0 0} 0
0 4 6
cost Adjacency Matrix A3 (i, j) 5 0 2
3 7 0
This matrix gives the all pairs shortest path solution
Algorithm all_pairs_shortest_path(W,A,n)
// W is weighted array matrix, n is the number of vertices,
// A is the cost of shortest path from vertex i to j.
{
for i:= 1 to n do
for j:= 1 to n do
A[i,j]:= W[i,j]
for k:=1 to n do
for i:= 1 to n do
for j:= 1 to n do
A[i,j]:=min(A[i,j],A[i,k]+A[k,j]
}