Wa0005.
Wa0005.
Dynamic Programming
1
Dynamic Programming
2
Chapter 2
Dynamic Programming
3
Dynamic Programming
4
Chapter 3
Dynamic Programming
5
Dynamic Programming
6
Chapter 4
Dynamic Programming
The multi stage graph is a directed weighted graph in which the nodes are
classied in various stages such that all edges are from a particular stage to
its next stage only. In k-stage graph problems every path from source (s) to
sink(t) is the result of a sequence of k-2 decisions. The Figure. 4.1 depicts a
5-stage graph. If p(i,j) be the minimum cost path from a vertex j in G(V,E)
7
Dynamic Programming
where l is the nearest neighbor of the current processing node. Figure. 4.3
shows the multistage graph operations to solve the graph given in Figure.
4.1 in which cost(i,j) is computed using (1.2). The Algorithm. 4.2 gives the
implementation details of multistage graph using backward approach.
8
Dynamic Programming
MultistageGraphF(G,k,n)
{
Data: A k-stage graph G(V,E) with n nodes.
Result: A minimum cost path.
cost[n]=0
for j=n-1 to 1 do
{
let r be a vertex such that (j,r) is an edge of G
c[j,r]+cost[r] is minimum
cost[j]=c[j,r]+cost[r]
d[j]=r
}
p[1]=1, p[k]=n
for j=2 k-1 do
p[j]=d[p[j-1]]
}
Algorithm 4.1: The multistage graph using forward approach.
MultistageGraphB(G,k,n)
{
Data: A k-stage graph G(V,E) with n nodes.
Result: A minimum cost path.
cost[n]=0
for j=2 to n do
{
let r be a vertex such that (r,j) is an edge of G
c[j,r]+cost[r] is minimum
cost[j]=cost[r]+c[r,j]
d[j]=r
}
p[1]=1, p[k]=n
for j=k-1 to 2 do
p[j]=d[p[j+1]]
}
Algorithm 4.2: The multistage graph using backward approach.
9
Dynamic Programming
10
Dynamic Programming
jth column of matrix Rk is 1 if and only if there exists a directed path from the
ith vertex to jth vertex with each intermediate vertex if any, not numbered
higher than k where k=(0, 1, ...n).
The process of generating the elements of matrix Rk from the elements
of matrix Rk-1 is given as
11
Dynamic Programming
Warshall(a)
{
Data: An adjacency matrix a for the given digraph.
Result: The transitive closure of the digraph.
R0 =a
for k=1 to n do
for i=1 to n do
for j=1 to n do
rij k = rij k-1 or rik k-1 and rkj k-1
return Rn
}
Algorithm 4.3: The warshall algorithm.
12
Dynamic Programming
The Floyd's algorithm calculates the distance from each vertex to all other
vertices. The algorithm constructs nxn matrix D to record the distance Dij
from each vertex to all other vertices where i and j are rows and columns of
the matrix D.
During operation the algorithm computes the distance matrix of a weighted
graph with n vertices through a series of n by n matrix such as. D0 ...Dk-1 ,Dk ....Dn .
Each of these matrices contain length of the shortest path with some
constraints such as Dij k in the ith row and jth column of matrix Dk , where
k=0...n is equal to the length of the shortest path among all paths from the
ith vertex to jth vertex with each intermediate vertex numbered not higher
than k. The implementation of Floyd's algorithm for the Figure. 4.8 is given
in the Figure. 4.9.
13
Dynamic Programming
Floyds(a)
{
Data: An adjacency matrix a for the given digraph.
Result: The shortest path from any vertex to any other vertex of
the digraph.
R =a
0
for k=1 to n do
for i=1 to n do
for j=1 to n do
Dij = min {Dij , Dik +Dkj }
return D
}
Algorithm 4.4: The Floyd's algorithm.
14
Dynamic Programming
Let a1, a2...an be distinct keys ordered from the smallest to largest, and
let p2...pn be the probabilities of searching for them. Let Cij be the smallest
average number of comparisons made in a successful search in a binary search
tree Tj i made up of keys ai,....aj, here i,j are some integer indices 1≤i≤j≤n. In
such an arrangement root contains key ak , the left sub tree Tk-1 i contains key
ai,..ak-1 and right sub tree Tk+1 j contains key ak+1,...aj which are optimally
arranged. The recurrence relation given in (4.4) is used to compute the Cij
values. Two two dimensional tables called main table and root table are used
to represent the Cij values and the root (sub tree) information respectively.
(4.5)
X
C(i, j) = min{C(i, k − 1) + C(k + 1, j)} + ijP.
Example. 4.3: Compute C values for the given key set A, B, C,
ij
15
Dynamic Programming
Figure 4.12: The main table and root table at initial phase.
16
Dynamic Programming
OptimalBT(p[1...n])
{
Data: The p[1...n] array with search probabilities of keys.
Result: The root table of optimal binary tree.
for i=1 to n do
C[i,i-1]=0
C[i,i]=P[i]
R[i,i]=i
C[n+1,n]=0 for d=1 to n-1 do
for i=1 to n-d do
j=i+d
minval=∞
for k=i to j do
if C[i,k-1]+C[k+1,j]<minval
minval=C[i,k-1]+C[k+1,j]
kmin=k
R[i,j]=kmin
sum=P[i]
for s=i+1 to j do
sum=sum+P[s]
C[i,j]=minval+sum
return C[1,n],R
}
Algorithm 4.5: The Optimal binary search tree implementation algo-
rithm.
17
Dynamic Programming
Figure 4.14: The main table and root table after updation.
The dynamic version of the knapsack problem nds the most valuable subset
of the items that t into the knapsack for the given n items of known weights
w1,w2...n and values v1,v2...vn and a knapsack of capacity W. The problem
can solve by a recurrence relation by considering an instance dened by
the rst i items, 1≤i≤n with weights w1,w2...wn and values v1,v2...vn and
knapsack capacity j, 1≤j≤W. The V[i,j] is the value of an optimal solution.
The recurrence relation for solving the problem is given below.
max{V[i-1,j],vi+V[i-1,j-wi]}
V [i, j] = if j − wi ≥ 0 (4.6)
V[i-1,j]
if j − wi ≤ 0
18
Dynamic Programming
Knapsack(i,j)
{
Data: The number of rst item considered i and sack capacity j.
Result: The value of optimal feasible solution.
if V[i,j]<0
if j<weights[i]
value=Knapsack(i-1,j)
else
value=max(Knapsack(i-1,j), values[i]+Knapsack(i-1,j-weights[i])
V[i,j]=value
return V[i,j]
}
Algorithm 4.6: The Knap sack using dynamic programming.
19
Dynamic Programming
The Figure. 4.16 shows a seven node graph with positive and nega-
tive edge weights along with the vertex arrays distance vector distk , where
k=1,2...6. The distance vector distk [u] is written as,
20
Dynamic Programming
dist1 [1]=0
dist2 [1]=0
dist3 [1]=0
dist4 [1]=0
dist5 [1]=0
dist6 [1]=0
and
dist1 [2]=6
dist1 [3]=5
dist1 [4]=5
dist1 [5]=∞
dist1 [6]=∞
dist1 [7]=∞
21
Dynamic Programming
Let G(V,E) be a directed graph with edge costs ci,j , where ci,j >0 for all i
and j and ci,j =∞ if (i,j) ∈/ E. A tour of G is a directed simple cycle that
includes every vertex in V. The cost of a tour is the sum of the cost of the
edges on the tour. The traveling sales person problem is used to nd the
tour of minimum cost. Let g(i,S) be the length of of a shortest path starting
at vertex i, going through all vertices in S and terminating at vertex 1. The
function g(1,V-1) is the length of an optimal sales persons tour. Using the
principle of optimality g(i,S)is written as
A directed graph and the corresponding edge length matrix is given in Figure.
4.18. The operations of the traveling sale person problem is given in Table.
Figure 4.18: The directed graph and the edge length matrix.
4.3.
The optimal tour of the graph has length 35. It is calculated as J(1,{2,3,4})=2
because the node two has the minimum g(i,S) value. The remaining tour can
be obtained from J(2,{3,4})=4, because g(4,{3}) has minimum value. Then
the tour continued to node three because J(4,{3})=3, hence the optimal tour
is 1-2-4-3-1.
22
Dynamic Programming
where u is the upper bound. There is at most one tuple for each dierent x
that results from a sequence of decisions on m1 ,m2 .....mn .
23