Daa Vtu Module Iv
Daa Vtu Module Iv
ALGORITHMS
Design and Analysis of
MODULE-IV
Algorithms
DYNAMIC PROGRAMMING
From,
Ashwini Janagal
Associate Professor
Department of ISE,
JNN College of Engineering,
Shivamogga-577204,
Karnataka,
India.
Dynamic Programming - General Method
DECISIONS”.
“SEQUENCE OF DECISIONS”.
DECISIONS”.
Can you compare finding shortest path from ‘i’ to ‘j’ and finding
What if that for given problem we are notr able to make stepwise
decision?
We take all possible decisions and try to find which one is better!!
Greedy Nature.
Decision 1: i to i1 is chosen.
If i1, r1, r2, ....., rq, j is shortest path than i1, i2, ..... , ik, j then i, i1, r1, r2, ..... ,
.
Lets say y1, y2, y3... yn be an optimal sequence of 0/1 values for
x1,x2,.....xn respectively.
w1)
Else there is another sequence z2, z3, ... , zn such that and that is
w1)
Else there is another sequence z2, z3, ... , zn such that and that is
Example: Knapsack
Let y1, y2, ... yn be an optimal solution to KNAP(1, n,m).
g0(m)=max{g1(m), g1(m-w1)+p1}.
gi(m)=max{gi+1(y), gi+1(y-wi+1)+pi+1}
Dynamic Programming - General Method
and p1=1, p2=2, and p3=5 and m=6, then Compute g0(6).
g0(6)=max{g1(6),g1(6-2)+p1}=max{g1(6),g1(4)+1}
g1(6)=max{g2(6),g2(6-3)+p2}=max{g2(6),g2(3)+2}
g2(6)=max{g3(6),g3(6-4)+p3}=max{g3(6),g3(2)+5}=max{0,5}=5
Dynamic Programming - General Method
and p1=1, p2=2, and p3=5 and m=6, then Compute g0(6).
g1(6)=max{g2(6),g2(6-3)+p2}=max{g2(6),g2(3)+2}
and p1=1, p2=2, and p3=5 and m=6, then Compute g0(6).
g0(6)=max{g1(6),g1(6-2)+p1}=max{g1(6),g1(4)+1}
g1(4)=max{g2(4),g2(4-3)+2}
g2(4)=max{g3(4),g3(4-4)+5}=max{0,5}=5
g2(1)=max{g3(1),g}
Dynamic Programming - MULTISTAGE GRAPH
2. Directed Graph
Sink)
Dynamic Programming - MULTISTAGE GRAPH
Dynamic Programming - MULTISTAGE GRAPH
Cost(i,j)=min{c(j,l)+cost(i+1,l)}
Cost(k-1,j)=c(j,t)
Cost(4,9)=4 ; cost(4,10)=2 ; cost(4,11)=5;
Cost(k-2,j)
cost(3,6)=min{6+cost(4,9), 5+cost(4,10)}
cost(3,7)=min{5+cost(4,9),3+cost(4,10)}
cost(3,8)=min{5+cost(4,10),6+cost(4,11)}
Dynamic Programming - MULTISTAGE GRAPH
Cost(i,j)=min{c(j,l)+cost(i+1,l)}
Cost(k-3,j))
Cost(2,2)=min{4+cost(3,6),2+cost(3,7),1+cost(3,8)}
cost(2,3)=min{2+cost(3,6),7+cost(3,7)}
cost(2,4)=min{11+cost(3,8)}
cost(2,5)=min{11+cost(3,7),8+cost(3,8)}
Dynamic Programming - MULTISTAGE GRAPH
Cost(i,j)=min{c(j,l)+cost(i+1,l)}
Cost(k-1,j)=c(j,t)
Cost(4,9)=4 ; cost(4,10)=2 ; cost(4,11)=5;
Cost(k-2,j) 2
4
cost(3,6)=min{6+cost(4,9), 5+cost(4,10)} = min{10,7} = 7
4 2
cost(3,7)=min{5+cost(4,9),3+cost(4,10)} = min{9,5} = 5
2 5
cost(3,8)=min{5+cost(4,10),6+cost(4,11)} = min{7,11} = 7
Dynamic Programming - MULTISTAGE GRAPH
Cost(3,6)= 7
Cost(i,j)=min{c(j,l)+cost(i+1,l)} Cost(3,7)= 5
Cost(k-3,j)) Cost(3,8)= 7
7 5 4
Cost(2,2)=min{4+cost(3,6),2+cost(3,7),1+cost(3,8)} = min{11,10,5} = 5
7 5
cost(2,3)=min{2+cost(3,6),7+cost(3,7)} = min{9, 12} = 9
7
cost(2,4)=min{11+cost(3,8)} = min{19} =19
cost(2,5)=min{11+cost(3,7),8+cost(3,8)} = min{16, 15} =15
5 7
Cost(k-4,j)
cost(1,1)=min(9+cost(2,2), 7+cost(2,3), 3+cost(2,4),
2+cost(2,5)) = min{14, }
Dynamic Programming - MULTISTAGE GRAPH
profit is maximum.
Dynamic Programming - MULTISTAGE GRAPH
Except 1st and ‘r+1’ stage each stage has n+1 vertices
Graph is
WARSHALLS ALGORITHM
directed path.
If
WARSHALLS ALGORITHM
and the element in its column j and row k are both 1's in
R(k-l)
WARSHALLS ALGORITHM
and the element in its column j and row k are both 1's in
R(k-l)
WARSHALLS ALGORITHM
WARSHALLS ALGORITHM
[i,k] [k,j]
b
[1,1] [1,1]
[1,2]
[1,3]
[1,4]
WARSHALLS ALGORITHM
[ ]
[i,k] [k,j]
[2,1] [1,1]
¿ 1 23 4
[1,2] 1 0 10 0
[1,3]
[1,4] 𝑅(0)= 2 0 00 1
3 0 00 0
4 1 01 0
WARSHALLS ALGORITHM
[ ]
[i,k] [k,j]
[3,1] [1,1]
¿ 1 23 4
[1,2] 1 0 10 0
[1,3]
[1,4] 𝑅(0)= 2 0 00 1
3 0 00 0
4 1 01 0
WARSHALLS ALGORITHM
[ ][ ]
k=1 i=4 j=
b [1,2,3,4]
¿ 1 23 4
[i,k] [k,j]
[4,1] [1,1]
1 0 10 0
[1,2] 𝑅(0)= 2 0 00 1 𝑎 𝑎 𝑏𝑐 𝑑
[1,3]
[1,4] 3 0 00 0 𝑏 0 10 0
4 1 01 0 0 00 1
𝑐
0 00 0
𝑑
1 11 0
WARSHALLS ALGORITHM
[ ][ ]
k=2 i=1 j=
[1,2,3,4]
b
[i,k] [k,j] ¿ 1 23 4
[1,2] [2,1] 1 0 10 0 ¿ 1 23 4
[2,2]
[2,3] 𝑅 (0)= 2 0 00 1 1 0 10 1
[2,4]
3 0 0 0 𝑅 0(0)= 2 0 00 1
4 1 11 0 3 0 00 0
4 1 11 0
WARSHALLS ALGORITHM
[ ]
[1,2,3,4]
b
[i,k] [k,j] ¿ 1 23 4
[2,2] [2,1] 1 0 10 1
[2,2]
[2,3] 𝑅 (0)= 2 0 00 1
[2,4]
3 0 00 0
4 1 11 0
WARSHALLS ALGORITHM
[ ]
k=2 i=3 j=
[1,2,3,4]
b
[i,k] [k,j] ¿ 1 23 4
[3,2] [2,1] 1 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1 11 1
WARSHALLS ALGORITHM
[ ]
k=4 i=2 j=
b [1,2,3,4] ¿ 1 23 4
[i,k] [k,j] 1 1 11 1
[2,4] [4,1]
𝑅 (0)= 2 0 00 1
[ ][ ][ ]
[4,2]
¿ 13 02 3 0 40 0 ¿ 1 23 4
[4,3]
¿ 1 [4,4]
23 4
1 1 11 1 1 14 111 111 1 1 1 11 1
𝑅 (0)= 2 1 0 0 𝑅 1(0)= 2 1 10 1 𝑅 (0)= 2 1 11 1
3 0 00 0 3 0 00 0 3 0 00 0
4 4 1 11 1
WARSHALLS ALGORITHM
[ ]
k=4 i=3 j=
b [1,2,3,4] ¿ 1 23 4
[i,k] [k,j] 1 1 11 1
[3,4] [4,1]
[4,2]
𝑅 (0)= 2 1 11 1
[4,3]
[4,4]
3 0 00 0
4 1 11 1
WARSHALLS ALGORITHM
[ ]
k=4 i=4 j=
b [1,2,3,4] ¿ 1 23 4
[i,k] [k,j] 1 1 11 1
[4,4] [4,1]
[4,2]
𝑅 (0)= 2 1 11 1
[4,3]
[4,4]
3 0 00 0
4 1 11 1
WARSHALLS ALGORITHM
[ ]
¿ 1 23 4
1 1 11 1
𝑅 (0)= 2 1 11 1
3 0 00 0
4 1 11 1
FLOYD’s ALGORITHM - ALL PAIR SHORTEST PATH
vertices.
FLOYD’s ALGORITHM - ALL PAIR SHORTEST PATH
‘i’ to ‘j’.
FLOYD’s ALGORITHM - ALL PAIR SHORTEST PATH
‘i’ to ‘j’.
FLOYD’s ALGORITHM - ALL PAIR SHORTEST PATH
FLOYD’s ALGORITHM - ALL PAIR SHORTEST PATH
Optimal Binary Search Trees
Optimal Binary Search Tree
=2.9
Optimal Binary Search Tree
=2.1
=2.1
largest.
Find root ak
Find root ak
Find root ak
GO TO NOTES
Optimal Binary Search Tree
Optimal Binary Search Tree
KNAPSACK PROBLEM - 0/1
KNAPSACK PROBLEM - 0/1
BELLMAN FORD
edges.
Initial -- >
BELLMAN FORD
Initial -- >
Let
MINIMUM COST.
TRAVELLING SALESMAN PROBLEM
Every tour consists of an edge {1,k} for some k in V-{1} and a path
MINIMUM)
g(1,V−{1})=𝑚𝑖𝑛2≤𝑘≤𝑛{𝑐1𝑘+g(k,V−{1,k})
TRAVELLING SALESMAN PROBLEM
g(1,V−{1})=𝑚𝑖𝑛2≤𝑘≤𝑛{𝑐1𝑘+g(k,V−{1,k})