Professional Documents
Culture Documents
Module 4: Dynamic Programming: Design and Analysis of Algorithms 21CS42
Module 4: Dynamic Programming: Design and Analysis of Algorithms 21CS42
Cost of node j at
stage i to reach
terminal node t
Stage number
Node
Stage 5
cost(5,12)=0
Stage 4
cost(4,9) = c(9,12) = 4
cost(4,10) = c(10,12)=2
cost(4,11) = c(11,12)=5
11
18CS42-Design
21CS42-Design and
and Analysis of Analysis of Algorithms
Algorithms
Stage 3
cost(3,6) = min { c(6,9) + cost(4,9), c(6,10) + cost(4,10)}
= min { 6 + 4, 5 + 2}= 7 d[6]=10
Stage node
Principle of Optimality:
Suppose that in solving a problem, we have to make a
sequence of decisions D1, D2, ……, Dn. If this sequence is
optimal, then the last k decisions, 1 < k < n must be
optimal.
1 2 3 4 5
P 1 2 7 10 12
Multistage Graph Pseudocode Corresponding to the Multistage Graph Pseudocode Corresponding to the
Forward Approach Backward Approach
21CS42-Design and Analysis of Algorithms
Analysis
Θ( |V| + |E|)
21CS42-Design
18CS42-Design and Analysis of Algorithms 27
Outline
• Introduction to • Optimal Binary Search
Dynamic Programming Trees
– General method with • Knapsack problem
Examples
• Bellman-Ford Algorithm
– Multistage Graphs
• Travelling Sales Person
• Transitive Closure:
problem
– Warshall’s Algorithm,
• Reliability design
• All Pairs Shortest Paths:
– Floyd's Algorithm,
• This means that there exists a path from the ith vertex vi
to the jth vertex vj with each intermediate vertex
numbered not higher than k:
vi, a list of intermediate vertices each numbered not higher
than k, vj
• Applications:
– Communications, transportation networks, and operations
research.
– pre-computing distances for motion planning in computer
games.
(a) Digraph. (b) Its weight matrix. (c) Its distance matrix
Example
Example
2
0.4
3
0.8
3
1
0.4
C(2,3) = Min k = 2 , C(2,1) + C(3,3), + 0 . 6 = Min 0 . 2 + 0 . 6 = 0 . 8
k = 3 , C(2 ,2) + C(4,3) 0.2
3 1.7 3
1.1
1.4 3
Example
vi
vi
k=1
k=2
k=3
k=4
k=5
k=6
Time Complexity: ?
n!
g(1, V-{1}) is the Shortest path starting at vertex 1, going through all vertices
say k(for some k ∈ V-{1}) and terminating at vertex 1.
• Generalizing
g(i , S) is the Shortest path starting at vertex i, going through all vertices
in S and terminating at vertex i.
• Example k = 2, c 12 + g(2,{3,4})
k = 3, c 13 + g(3,{2,4})
g(1,{2,3,4}) = min
k = 4, c 14 + g(4,{2,3})
Case 3:
Since |S| < n-1 ie |S|< 3, We should not find out g(i,S) when |S|=3.
Therefore, Final Relation is
g(1, V-{1}) = min{C1k + g(k, V-{1,k})}
2≤k≤n
g(1,{1,2,3,4}-{1}) = min{C1k + g(k, {1,2,3,4}-{1,k})}
k=2,3,4
• If
the reliability of whole system is
𝑛
ෑ 𝑟𝑖
𝑖=1
Devices A B C
cost 30 15 20
Reliability 0.9 0.8 0.5
Maximum number of devices can be
purchased (by guaranteed purchase of u1=2 u2=3 u3=2
one unit of other devices)
• 𝑺
𝒊
- Set of all tuples of the from (f,x) generated from various sequences of
m1, m2.. mi considering i stages where f is reliability and x is cost
• 𝑺 𝒊 - i is the stage no., j is the no. of devices used at stage i
𝒋
• Example
– 𝑺 𝟏 means 1 device of type A at stage
1
– 𝑺 𝟏 means 2 device of type A at stage
1
𝑺𝟏 𝟎. 𝟗, 𝟑 𝟎 m1=1 m1=2
=𝑺 𝟏 𝟎. 𝟗 𝟗 , 𝟔 𝟎 𝐶 𝑜 𝑚 𝟎. 𝟗, 𝟑 𝟎 , 𝟎. 𝟗 𝟗 , 𝟔 𝟎
= .9*.8 .99*.8
𝑏 𝑖 𝑛 𝑖 𝑛
𝑺𝟐 𝟎 . 𝟕 𝟐 , 𝟒 𝟓 , ( 𝟎: . 𝑺
= 𝟕.9*.96
𝟗𝟐, 𝟕𝟓) =
.99*.96
𝑺 𝟐 = (𝟎. 𝟖 𝟔 𝟒 , For (0.9504,90) refer Note #1
𝟔𝟎)
.9*.992 .99*.992
𝑺𝟐 𝟎. For (0.982,105) refer Note #1
= 𝟖𝟗𝟐𝟖, Note #2
𝐶 𝟕𝑜 𝟓 𝑚 𝟎. 𝟕 𝟐 , 𝟒 𝟓 , 𝟎. 𝟕 𝟗 𝟐 , 𝟕 𝟓 , 𝟎.
𝑏 𝑖 𝑛 𝑖 𝑛 𝟖𝟔𝟒, 𝟔 𝟎 , 𝟎. 𝟖 𝟗 𝟐 𝟖 , 𝟕 𝟓
𝑔 : 𝟎 . 𝟕m1=1 𝟐 , 𝟒 𝟓 ,m1=1 𝟎m1=1
. 𝟖 𝟔 𝟒 , 𝟔 𝟎 , 𝟎.
𝑺 m2=1 m2=2 m2=3
𝟖𝟗𝟐𝟖, 𝟕𝟓
𝟐 This configuration will not leave adequate funds to complete system
Note #1: =
NoHtaeriv#in2o:dFNorthe same
𝟐 c121CS42-Design
o8CsSt4,2-rDeetsagiinnanand
tdhAeAnalysis
naoylnsiseowof
fAiAlgorithms
lgtohrithhmigshestreliabilFiteyb-May 2020 108
# of devices in A B C
llel Reliability Cost Reliability Cost Reliability Cost
1 0.9 m1=1 30 0.8 m2=1 15 0.5 m3=1 20
2 0.99 m1=2 60 0.96 m2=2 30 0.75 m3=2 40
3 - - 0.992 m2=3 45 0.875 m3=3 60
𝑺𝟐 𝟎. 𝟕 𝟐 , 𝟒 𝟓 , 𝟎. 𝟖 𝟔 𝟒 ,
= 𝟔 𝟎m1=1
, 𝟎 m1=1
. 𝟖 𝟗 𝟐 𝟖 , 𝟕m1=1
𝟓
m2=1 m2=2 m2=3
𝑺𝟑
=
𝑺𝟑
= m1=1
𝑺𝟑 m2=1
M3 =3
=
𝐶𝑜𝑚𝑏𝑖𝑛𝑖𝑛𝑔:
𝑺𝟑
=
Best Reliability 0.648 with cost=100 where A -1 unit, B -1 unit, C- 2 units in ||el.