AOA Module 4 - Dynamic Programming
AOA Module 4 - Dynamic Programming
Programming
● General Method,
● Multistage graphs,
● Single source shortest path:Bellman Ford Algorithm
● All pair shortest path: Floyd Warshall Algorithm,
● Assembly-line scheduling Problem
● 0/1 knapsack Problem,
● Travelling Salesperson problem,
● Longest common subsequence
Multistage Graphs
• Similar to divide-and-conquer algorithm
• Differs in the fact that sub problems are not
independent
• Solves each subproblem and saves answer to
avoid repetition
• Typically applied to optimization problems
Algorithm Steps
1. Characterize the structure of an optimal solution.
2. Recursively define the value of an optimal solution.
3. Compute the value of an optimal solution in a
bottomup fashion.
4. Construct an optimal solution from computed
information.
Examples(wiki)
• In 1940s by Richard Bellman to describe the process of solving problems where one needs to
find the best decisions one after another.
• Tower of Hanoi puzzle
• Graph theory; game theory; AI and machine learning; economics and finance
problems; bioinformatics; as well as calculating the shortest path, which is used in
GPS.
• The Bellman–Ford algorithm for finding the shortest distance in a graph.
• The Viterbi algorithm (used for hidden Markov models, and particularly in part of
speech tagging)
• The Earley algorithm (a type of chart parser)
• Many string algorithms including longest common subsequence, longest increasing
subsequence, longest common substring, Levenshtein distance (edit distance)
• The Selnninger (a.k.a. System R) algorithm for relational database query
optimization
Fibo/ Tower of Hanoi example
• https://www.cs.usfca.edu/~galles/visualization/DPFib.html...
Simulation of fibo
• https://haubergs.com/hanoi
The complexity of tracing the minimum cost path would be O(k), k<n.
Thus total time complexity of multistage graph using dynamic
programming would be O(n+ |E|).
Solve the below problem by forward approach
d(S,A) =1 S-A
d(S,B) =2 S-B
d(S,C) =7 S-C
d(S,D) = min{d(S,A)+c(A,D) , d(S,B)+c(B,D)}
= min{1+3 , 2+4} = 4 S-A-D
d(S,E) = min{d(S,A)+c(A,E) , d(S,B)+c(B,E) , d(S,C)+c(C,E)}
= min{1+6 , 2+10, 7+3} =7
S-A-E
d(S,T) = min{d(S,D)+c(D,T) , d(S,E)+c(E,T) , d(S,C)+c(C,T)}
= min{4+8, 7+2, 7+10} =9
S-A-E-T
Solve the below problem by backward
approach
V 1 2 3 4 5 6 7 8 9 10 11 12
Cost 16 7 9 18 15 7 5 7 4 2 5 0
d 2/3 7 6 8 8 10 10 10 12 12 12 12
d(1,1) = 2 d(1,1) = 3
d(2,2) = 7 d(2,3) = 6
d(3,7) = 10 d(3,6) = 10
d(4,10) = 12 d(4,10) = 12
6 3
-2
1
5
1 3 7
-2
5 3
6
4
-1
List of edges - (1,2), (1,3), (1,4), (3,2), (4,3), (2,5), (3,5), (4,6), (5,7), (6,7)
v 1 2 3 4 5 6 7
d[v] 0 ∞ ∞ ∞ ∞ ∞ ∞
PN - - - - - - -
1 3 7
6
4
1
7 10
8 5
3 5 2
2
3 8
4
1
2 4
8
2 5
1 -3 4 6
-4
4 6
3 5
-2
All pair shortest path (Floyd-Warshall Algorithm)
All pair shortest path (Floyd-Warshall Algorithm)
7
1 2
10 4
3
3
2
1 2
3
6 7
4 3
1
Assembly Line Scheduling
Assembly Line Scheduling
Assembly Line Scheduling
Assembly Line Scheduling
f1[j] 9 18 20 24 32 35
f2[j] 12 16 22 25 30 37 fopt=38
j 1 2 3 4 5 6
l1[j] 1 2 1 1 2
l2[j] 1 2 1 2 2 lopt=1
j 2 3 4 5 6
Assembly Line Scheduling
0/1 Knapsack
ALGORITHM Dynamic-0-1-knapsack (v, w, n, W)
for w = 0 to W do
c[0, w] = 0
for i = 1 to n do
c[i, 0] = 0
for w = 1 to W do
if wi ≤ w then
if vi + c[i-1, w-wi] then
c[i, w] = vi + c[i-1, w-wi]
else
c[i, w] = c[i-1, w]
end
else
c[i, w] = c[i-1, w]
end
end
end
Travelling Salesman problem
Given a set of n cities , find the minimum length path such that it
covers each city exactly once and terminates the tour at starting city”
Travelling Salesman Problem
Longest Common Subsequence (LCS)
ALGORITHM LCS(X,Y)
for i ← 1 to m do
LCS[i,0] ← 0
end
for j ← 1 to n do
LCS[0,j] ← 0
end
for i ← 1 to m do
for j ← 1 to n do
if Xi == Yj then
LCS[i,j] ← LCS[i-1, j-1] +1
elseif LCS[i-1, j] ≥ LCS[i, j-1] then
LCS[i,j] ← LCS[i-1, j]
else
LCS[i,j] ← LCS[i, j-1]
end
end
end
return LCS
Complexity Analysis of Longest
Common Subsequence
0 A C B A E D
0 0 0 0 0 0 0 0
A 0 1 1 1 1 1 1
B 0 1 1 2 2 2 2
C 0 1 2 2 2 2 2
A 0 1 2 2 3 3 3
B 0 1 2 3 3 3 3
E 0 1 2 3 3 4 4