Dynamic Programming - Unit IV
Dynamic Programming - Unit IV
DYNAMIC PROGRAMMING
DYNAMIC PROGRAMING
The idea of dynamic programming is thus quit simple: avoid calculating the same thing
twice, usually by keeping a table of known result that fills up a sub instances are solved.
When a problem is solved by divide and conquer, we immediately attack the complete
instance, which we then divide into smaller and smaller sub-instances as the algorithm
progresses.
We usually start with the smallest and hence the simplest sub- instances.
The essential difference between the greedy method and dynamic programming is that the
greedy method only one decision sequence is ever generated.
MULTISTAGE GRAPH
1. A multistage graph G = (V,E) is a directed graph in which the vertices are portioned into
K≥ 2 disjoint sets Vi, 1 ≤ i≤ k.
2. In addition, if < u,v > is an edge in E, then u < = Vi and V Vi+1 for some i, 1≤ i < k.
3. If there will be only one vertex, then the sets Vi and Vk are such that [Vi]=[Vk] = 1.
4. Let ‘s’ and ‘t’ be the source and destination respectively.
1
5. The cost of a path from source (s) to destination (t) is the sum of the costs of the edger on
the path.
6. The MULTISTAGE GRAPH problem is to find a minimum cost path from ‘s’ to ‘t’.
7. Each set Vi defines a stage in the graph. Every path from ‘s’ to ‘t’ starts in stage-1, goes to
stage-2 then to stage-3, then to stage-4, and so on, and terminates in stage-k.
8. This MULISTAGE GRAPH problem can be solved in 2 ways.
a) Forward Method.
b) Backward Method.
FORWARD METHOD
PROCEDURE:
V1 V2 V3 V4 V5
4 6
2 2
5 4
9 1
4
7 3 2
7 t
s
3
11 5 5
2
11 6
2
Maintain a cost matrix cost (n) which stores the distance from any vertex to the
destination.
If a vertex is having more than one path, then we have to choose the minimum distance
path and the intermediate vertex, which gives the minimum distance path, will be stored
in the distance array ‘D’.
In this way we will find out the minimum cost path from each and every vertex.
Finally cost(1) will give the shortest distance from source to destination.
For finding the path, start from vertex-1 then the distance array D(1) will give the
minimum cost neighbor vertex which in turn give the next nearest vertex and proceed in
this way till we reach the Destination.
For a ‘k’ stage graph, there will be ‘k’ vertex in the path.
In the above graph V1…V5 represent the stages. This 5 stage graph can be solved by
using forward approach as follows,
STEPS: - DESTINATION, D
Cost (12)=0 D (12)=0
Cost (11)=5 D (11)=12
Cost (10)=2 D (10)=12
Cost ( 9)=4 D ( 9)=12
3
= min(11+5 , 8 +7)
= min(16,15)
= 15
cost(5) = 15 =>D(5) = 18
cost(2) = 7 =>D(2) = 7
The path through which you have to find the shortest distance.
(i.e.)
D ( 1) = 2
D ( 2) = 7
D ( 7) = 10
D (10) = 12
4
9 2 3 2
P[1]=1;
P[k]=n;
For j=2 to k-1 do
P[j]=d[p[j-1]];
}
ANALYSIS:
The time complexity of this forward method is 𝑂(|𝑉| + |𝐸|)
BACKWARD METHOD
If there are ‘K’ stages in a graph using back ward approach. we will find out the cost of
each & every vertex starting from 1st stage to the kth stage.
We will find out the minimum cost path from destination to source (i.e.,) from stage k
to stage 1.
PROCEDURE:
STEP:
cost(6) = 9 =>D(6)=3
cost(7) = 11 =>D(7)=2
cost(8) = 10 =>D(8)=2
cost(9) = 15 =>D(9)=6
cost(11) = 16 =>D(11)=8
cost(12)=min(c(9,12)+cost(9),c(10,12)+cost(10),c(11,12)+cost(11))
=min(19,16,21)
cost(12) = 16 =>D(12)=10
PATH:
6
Start from vertex-12
D(12) = 10
D(10) = 6
D(6) = 3
D(3) = 1
1 7 3 2 6 5 10 2 12
P[1]=1;
P[k]=n;
For j= k-1 to 2 do
P[j]=d[p[j+1]];
}
We want to calculate the length of the shortest path between each pair of nodes.
7
Suppose the nodes of G are numbered from 1 to n, so V={1,2,...N},and suppose G matrix
L gives the length of each edge, with L(i,j)=0 for i=1,2...n,L(i,j)>=for all i & j, and
L(i,j)=infinity, if the edge (i,j) does not exist.
The principle of optimality applies: if k is the node on the shortest path from i to j then the
part of the path from i to k and the part from k to j must also be optimal, that is shorter.
Copy the above matrix-to-matrix D, which will give the direct distance between nodes.
We have to perform N iteration after iteration k. the matrix D will give you the distance
between nodes with only (1,2...,k) as intermediate nodes.
At the iteration k, we have to check for each pair of nodes (i,j) whether or not there exists
a path from i to j passing through node k.
Example: 6
1 2
4
11
3 2
Matrix Representation:
0 4 11
[6 0 2]
3 𝛼 0
8
A0 =
0 4 11
[6 0 2]
3 𝛼 0
At 1st iteration we have to check the each pair(i,j) whether there is a path through node
1.if so we have to check whether it is minimum than the previous value and if I is so than
the distance through 1 is the value of d1(i,j).at the same time we have to solve the
intermediate node in the matrix position p(i,j).
ALGORITHM :
D=L
For k = 1 to n do
For i = 1 to n do
For j = 1 to n do
D [ i , j ] = min (D[ i, j ], D[ i, k ] + D[ k, j ]
Return D
ANALYSIS:
9
OPTIMAL BINARY SEARCH TREES
If n-identifiers exists, every internal node represents a P and every external node represents a
point where an unsuccessful search may terminate.
Solution:
The possible binary trees are many.
Possiblity-1:
do
if
q(0)
while
q(1)
q(2) q(3)
Cost=(0.5*1+0.1*2+0.05*3)+(0.15*1+0.1*2+0.05*3+0.05*3)=1.50
10
Possiblity-2:
do
while
q(0)
if
q(1)
q(2) q(3)
Cost=(0.5*1+0.05*2+0.1*3)+(0.15*1+0.1*2+0.05*3+0.05*3)=0.9+0.65=1.7
Possiblity-3:
Possiblity-4:
:
:
:
We get many possible binary search trees and out of which first tree is having the minimum
cost i.e., the optimal binary search tree.
Example1: Compute w(i,j), c(i,j), r(i,j) for the identifier set n = 4, (a1,a2,a3,a4) =
{do,if,int,while} with P(1:4) = {3,3,1,1} and q = {2,3,1,1,1}. Using the r (i,j)s construct the
optimal binary search tree.
Solution:
𝑤(𝑖, 𝑗) = 𝑃(𝑗) + 𝑞(𝑗) + 𝑤(𝑖, 𝑗 − 1)
𝑐𝑜𝑠𝑡(𝑖, 𝑗) = min {𝑐𝑜𝑠𝑡(𝑖, 𝑘 − 1) + 𝑐𝑜𝑠𝑡(𝑘, 𝑗)} + 𝑤(𝑖, 𝑗)
𝑖<𝑘≤𝑗
w(i,i)=q(i), C(i,i)=r(i,i)=0
11
0 1 2 3 4
j-i=0 W(0,0)=2 W(1,1)=3 W(2,2)=1 W(3,3)=1 W(4,4)=1
C(0,0)=0 C(1,1)=0 C(2,2)=0 C(3,3)=0 C(4,4)=0
r(0,0)=0 r(1,1)=0 r(2,2)=0 r(3,3)=0 r(4,4)=0
j-i=1 W(0,1)=8 W(1,2)=7 W(2,3)=3 W(3,4)=3
C(0,1)=8 C(1,2)=7 C(2,3)=3 C(3,4)=3
r(0,1)=1 r(1,2)=2 r(2,3)=3 r(3,4)=4
j-i=2 W(0,2)=12 W(1,3)=9 W(2,4)=5
C(0,2)=19 C(1,3)=12 C(2,4)=8
r(0,2)=1 r(1,3)=2 r(2,4)=3
j-i=3 W(0,3)=14 W(1,4)=11
C(0,3)=25 C(1,4)=19
r(0,3)=2 r(1,4)=2
j-i=4 W(0,4)=16
C(0,4)=32
r(0,4)=2
C04
C01 C24
C34
t00 t11
t22
t33 t44
if
do int
while
12
This algorithm takes a time of O(n3).
We are given ‘ N ‘ objects with weight Wi and profits Pi where i varies from l to N and
also a knapsack with capacity ‘ M ‘.
The problem is, we have to fill the bag with the help of ‘ N ‘ objects and the resulting
profit has to be maximum.
n
Formally, the problem can be started as, maximize Xi Pi
i=l
n
subject to Xi Wi ≤ M
i=l
Time complexity is O(nW).n is the number of items and W is the weight of the
knapsack.
Therefore, we have to select a state such that it maximizes the result. This can be shown as
13
𝑆1𝑖 = {(𝑃, 𝑊)|(𝑃 − 𝑃𝑖), (𝑊 − 𝑊𝑖 )𝜖𝑆𝑖}
Si+1 is obtained by merging and purging the order pairs of S i and 𝑆𝑖1. Purging is done by
applying dominance rule.
Dominance Rule: If Si+1continas two ordered pairs (Pj,Wj), (Pk,Wk) such that Pj<Pk and
Wj>Wk then the ordered pair (Pj,Wj) is discarded.
Example: Given n=3 and M=6, (P1,P2,P3)=(1,2,5), (W1,W2,W3)=(2,3,4). Find the optimal
solution for 0/1 knapsack.
Solution:
S0 ={(0,0)}
𝑆12 = {(5 + 0,4 + 0), (5 + 1,4 + 2), (5 + 2,4 + 3), (5 + 3,4 + 5)}
={(5,4),(6,6),(7,7),(8,9)}
S3= {(0,0),(1,2),(2,3),(3,5), (5,4),(6,6),(7,7),(8,9)}
(7,7) , (8,9) are discarded, as maximum capacity is only 6.
Apply dominance rule for purging. (3,5) is discarded using purging (3<5,5>4) as we need
maximum profit.
Hence, S3= {(0,0),(1,2),(2,3), (5,4),(6,6)}
Select the largest ordered pair i.e., (6,6)
X3=(6,6) ϵS3
(6,6) ∉ S2
Therefore, X3=1
This implies the knapsack is filled now and the profit is 6. For next ordered pair, remove 3 rd
object profit and weight.
i.e., (6-5,6-4)=(1,2)
X2=(1,2) ϵ S2
(1,2) ϵ S1
Therefore, X2=0 . It cannot be placed.
RELIABILITY DESIGN
The problem is to design a system that is composed of several devices connected in series. Let
ri be the reliability of device Di (that is ri is the probability that device i will function properly)
then the reliability of the entire system is ∏ ri. Even if the individual devices are very reliable
(the ri’s are very close to one), the reliability of the system may not be very good. For example,
if n = 10 and ri = 0.99, i < i < 10, then ∏ri = .904.
Hence, it is desirable to duplicate devices. Multiple copies of the same device type are
connected in parallel. If stage i contains mi copies of device Di. Then the probability that all mi
have a malfunction is (1 - ri)mi. Hence the reliability of stage i becomes 1 – (1 - r )mi. The
reliability of stage ‘i’ is given by a function Φi (mi).
Our problem is to use device duplication. This maximization is to be carried out under a cost
constraint. Let ci be the cost of each unit of device i and let c be the maximum allowable cost of
the system being designed.
We wish to solve:
Maximize
∏ 𝜑𝑖(𝑚𝑖)
1≤𝑖≤𝑛
Subject to
∑ 𝐶𝑖𝑚𝑖 < 𝐶
1≤𝑖≤𝑛
mi ≥1
mi > 1 and interger, 1 < i < n
Example 1:
15
Design a three stage system with device types D1, D2 and D3. The costs are $30, $15 and $20
respectively. The Cost of the system is to be no more than $105. The reliability of each device
is 0.9, 0.8 and 0.5 respectively.
Solution:
Example2: Consider three stages of a system with r1=0.3, r2=0.5, r3=0.2, c1=30, c2=22,
c3=30. Where the total cost of the system is C=80 and u1=2, u2=3, u3=2 find the reliability
design.
16
17
TRAVELLING SALESMAN PROBLEM
Let G(V,E) be a directed graph with edge cost cij is defined such that cij >0 for all i and
j and cij = ,if <i,j> E.
Let V =n and assume n>1.
The traveling salesman problem is to find a tour of minimum cost.
A tour of G is a directed cycle that include every vertex in V.
The cost of the tour is the sum of cost of the edges on the tour.
The tour is the shortest path that starts and ends at the same vertex i.e., 1.
APPLICATION:
1. Suppose we have to route a postal van to pick up mail from the mail boxes located at ‘n’
different sites.
2. An n+1 vertex graph can be used to represent the situation.
3. One vertex represents the post office from which the postal van starts and returns.
4. Edge <i,j> is assigned a cost equal to the distance from site ‘i’ to site ‘j’.
5. The route taken by the postal van is a tour and we are finding a tour of minimum length.
6. Every tour consists of an edge <1,k> for some k V-{} and a path from vertex k to
vertex 1.
7. The path from vertex k to vertex 1 goes through each vertex in V-{1,k} exactly once.
8. The function which is used to find the path is
1. Find g(i,) =ci1, 1<=i<n, hence we can use equation(2) to obtain g(i,s) for all s to size 1.
2. That we have to start with s=1,(ie) there will be only one vertex in set ‘s’.
3. Then s=2, and we have to proceed until |s| <n-1.
Example1:
10
15
10
15
20 8 9 13
8 6
12
7
18
Cost matrix
0 10 15 20
5 0 9 10
6 13 0 12
8 8 9 0
starting position
STEP 1:
g(1,{2,3,4})=min{c12+g(2{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}
min{10+25,15+25,20+23}
min{35,35,43}
=35
STEP 2:
g(2,{3,4}) = min{c23+g(3{4}),c24+g(4,{3})}
min{9+20,10+15}
min{29,25}
=25
g(3,{2,4}) =min{c32+g(2{4}),c34+g(4,{2})}
min{13+18,12+13}
min{31,25}
=25
g(4,{2,3}) = min{c42+g(2{3}),c43+g(3,{2})}
min{8+15,9+18}
min{23,27}
=23
STEP 3:
STEP 4:
g{4,} =c41 = 8
g{3,} =c31 = 6
g{2,} =c21 = 5
|𝑠|=0
i =1 to n.
|𝑠|=1
i =2 to 4
|𝑠|=2
i 1, 1 s and i s.
g(2,{3,4}) = min{c23+g(3{4}),c24+g(4,{3})}
min{9+20,10+15}
min{29,25}
=25
g(3,{2,4}) =min{c32+g(2{4}),c34+g(4,{2})}
min{13+18,12+13}
min{31,25}
=25
g(4,{2,3}) = min{c42+g(2{3}),c43+g(3,{2})}
min{8+15,9+18}
min{23,27}
=23
|𝑠|=3
g(1,{2,3,4})=min{c12+g(2{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}
min{10+25,15+25,20+23}
min{35,35,43}
=35
optimal cost is 35
21
g(4,{3}) = c43 + g(3{}) => 1->2->4->3->1
DEFINING GRAPH:
2 3
FIG: Graph G
UNDIRECTED GRAPH:
An undirected graph is that in which, the pair of vertices representing the edges is
unordered.
DIRECTED GRAPH:
An directed graph is that in which, each edge is an ordered pair of vertices, (i.e.)
each edge is represented by a directed pair. It is also referred to as digraph.
DIRECTED GRAPH
22
COMPLETE GRAPH:
An n vertex undirected graph with exactly n(n-1)/2 edges is said to be complete
graph. The graph G is said to be complete graph .
In Breadth First Search we start at a vertex ‘v’ and mark it as having been reached
(visited).
The vertex ‘v’ is at this time said to be unexplored.
A vertex is said to have been explored by an algorithm when the algorithm has visited
all vertices adjust from it.
All unvisited vertices adjust from ‘v’ are visited next. These are new unexplored
vertices.
Vertex ‘v’ has now been explored. The newly visit vertices have not been explored and
are put on the end of a list of unexplored vertices.
The first vertex on this list in the next to be explored. Exploration continues until no
unexplored vertex is left.
The list of unexplored vertices operates as a queue and can be represented using any of
the start queue representation.
23
ALGORITHM:
Here the time and space required by BFT on an n-vertex e-edge graph one O(n+e) and O(n)
resp. if adjacency list and adjacency matrix is used then the bounds are O(n2) and O(n) resp.
A depth first search of a graph differs from a breadth first search in that the exploration
of a vertex v is suspended as soon as a new vertex is reached. At this time the exploration of the
new vertex u begins. When this new vertex has been explored, the exploration of u continues.
The search terminates when all reached vertices have been fully explored. This search process
is best-described recursively.
Algorithm DFS(v)
{
visited[v]=1
for each vertex w adjacent from v do
24
{
If (visited[w]=0)then
DFS(w);
}
}
Articulation Point: An articulation point in a connected graph is a vertex that if deleted, would
break the graph into two or more pieces (Connected components)
H I
A
G
C
B
J K
D
F E
L M
1 2
5 3
25
Bi-connected Component: A bi-connected component of a graph is a maximal bi-connected
subgraph. A graph which is not bi-connected can divide into bi-connected components set of
nodes mutually accessible via two distinct paths.
2. What do you understand by all pairs shortest path problem and compare different algorithms
pertinent to this problem.
3. Describe dynamic programming method. How dynamic programming can be used to solve
optimal binary search trees?
4. Explain the detail about travelling salesperson problem with suitable example.
6. What is multi-stage graph? Write dynamic programming expressions for Multistage graph
corresponding to forward and backward approaches and explain with an example.
26
7. Write an algorithm to find the shortest path in a multi stage graph using dynamic programming.
8. Write the algorithm for Optimal Binary Search Tree. Show that the computing time of function
OBST is Θ( n2 ).
9. What are the differences between dynamic programming and divide-and-conquer techniques?
10. Consider the three stages of a system with (r1,r2,r3) = (0.2,0.5,0.3) and (c1,c2,c3) = (20,30,20)
where the total cost of system 70 and (u1,u2,u3) = (3,2, 2) find the reliability design.
11. Using the optimal binary search tree algorithm compute w(i,j),R(i,j) and C(I,j), 0<=i<=j<=4 for
the identifier set (a1,a2,a3,a4)=(end,goto,print,stop) with P(1)=1/20, P(2)=1/5, P(3)=1/10,
P(4)=1/20,Q(0)=1/5,Q(1)=1/10,Q(2)=1/5,Q(3)=1/20,Q(4)=1/20. Using R(i.j)’s constru t OBST.
12. Compute w(i,j), c(i,j), r(i,j) for the identifier set n = 4, (a1,a2,a3,a4) = {cout, float, int, while}
with P(1:4) = {2,1,1,2} and q = {2,3,1,1,1}. Using the r (i,j)s construct the optimal binary
search tree.
13. Briefly argue how the principle of a optimality holds for 0/1 knapsack problem, generate the
sets. Si, 0<=i<=4. Where (w1,w2,w3,w4)=(10,15,6,9) and (P1,P2,P3,P4)=(2,5,8,9)—State the
purging rules used. If knapsack capacity is m=25, what is optimal solution.
27