Unit-IV Dynamic Programming
Unit-IV Dynamic Programming
Dr Pradosh Kumar
Department of Artificial Intelligence and Data Science
Dynamic Programming
0 if n=0
F(n) = 1 if n=1
F (n-1)+F (n-2) if n >1
f(n)
If (n ≤ 1)
return n;
If ( A[n] != -1)
return A[n];
else
return
A[n]=f(n-1)+f(n-2);
Bottom-Up Approach (Tabulation)
• Start with the smallest subproblems and gradually build up to the final
solution.
• First fill the solution for base cases and then fill the remaining entries of
f(n)
{
A[0]=0;
A[1]=1
for (i=2;i≤n;i++)
{
A[i]=A[i-1]+A[i-2];
}
return A [n];
• Using Memoization Approach –
– Time Complexity : O(n)
– Space Complexity: O(n)
• Using Tabulation Approach
– Time Complexity : O(n)
– Space Complexity: O(n)
Advantages of Dynamic Programming (DP)
• S2 = {A, C, D, B, A, C}
LCS(X, Y) = BCB
X=AB C B
Y= BDCAB 20
ABCB
LCS Example (0) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
A
1
2 B
3 C
4 B
X = ABCB; m = |X| = 4
Y = BDCAB; n = |Y| = 5
Allocate array c[5,4]
21
ABCB
LCS Example (1) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0
2 B
0
3 C 0
4 B 0
for i = 1 to m c[i,0] = 0
for j = 1 to n c[0,j] = 0
22
ABCB
LCS Example (2) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
05/24/2025 23
ABCB
LCS Example (3) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
05/24/2025 24
ABCB
LCS Example (4) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
05/24/2025 25
ABCB
LCS Example (5) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
05/24/2025 26
ABCB
LCS Example (6) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
05/24/2025 27
ABCB
LCS Example (7) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
05/24/2025 28
ABCB
LCS Example (8) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
05/24/2025 29
ABCB
LCS Example (10) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
05/24/2025 30
ABCB
LCS Example (11) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
05/24/2025 31
ABCB
LCS Example (12) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
05/24/2025 32
ABCB
LCS Example (13) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
05/24/2025 33
ABCB
LCS Example (14) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
05/24/2025 34
ABCB
LCS Example (15) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
05/24/2025 35
How to find actual LCS
• So far, we have just found the length of LCS,
but not LCS itself.
• We want to modify this algorithm to make it
output Longest Common Subsequence of X
and Y
Each c[i,j] depends on c[i-1,j] and c[i,j-1]
or c[i-1, j-1]
For each c[i,j] we can say how it was acquired:
2 2 For example, here
2 3 c[i,j] = c[i-1,j-1] +1 = 2+1=3
36
How to find actual LCS - continued
• Remember that
c[i 1, j 1] 1 if x[i ] y[ j ],
c[i, j ]
max(c[i, j 1], c[i 1, j ]) otherwise
05/24/2025 38
Finding LCS (2)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3
LCS (reversed order): B C B
LCS (straight order): B C B
(this string turned out to be a palindrome) 39
LCS Algorithm
LCS-Length(X, Y)
1. m = length(X) // get the # of symbols in X
2. n = length(Y) // get the # of symbols in Y
3. for i = 1 to m c[i,0] = 0 // special case: Y0
4. for j = 1 to n c[0,j] = 0 // special case: X0
5. for i = 1 to m // for all Xi
6. for j = 1 to n // for all Yj
7. if ( Xi == Yj )
8. c[i,j] = c[i-1,j-1] + 1
9. else c[i,j] = max( c[i-1,j], c[i,j-1] )
05/24/2025 40
Time Complexity of LCS Algorithm
05/24/2025 41
Examples
X= ABAABA
Y= BABBAB
X= STONE
Y= LONGEST
X=ABCBDAB
Y= BDCABA
X=ABCB
Y= BDCAB
Dijkstra Algorithm(Recap)
Update/Relaxation
if (dist[v] > dist[u] + cost[u, v])) then
dist[v] := dist[u] + cost[u ,v]
Dijkstra Algorithm(Recap)
Limitation of Dijkstra’s Algorithm:
A= 0
B= -2
C= 8
D= 5
Bellman Ford Algorithm
Bellman Ford
Example
Iteration-1
0 10
10
S A
8 1
8 G -4 B
2
1 1
-2
F C
-1 3
E D
-1
Iteration-2
Iteration-3
Iteration-4
Iteration-5
Iteration-6
Iteration-7
Bellman-Ford
5 3 0
Floyd’s Algorithm 64
The subproblems
• Let D(k)[i,j]=weight of a shortest path from
vi to vj using only vertices from {v1,v2,…,vk}
as intermediate vertices in the path
– D(0)=W
– D(n)=D which is the goal matrix
Vj
Vi
We conclude:
1 5 2
3
4 2 3
2
-3
Example
1 2 3
1 0 4 5
W=D =
0
1 5 2 2 0
3 -3 0
4 2 3
2
-3
1 5
4 3
2
2
-3
1 2 3
1 0 4 5 D1[2,3] = min( D0[2,3], D0[2,1]+D0[1,3] )
= min (, 7)
D0 = 2 2 0
=7
3 -3 0
1 2 3
1 0 4 5
D1 = 2 2 0 7
D2[1,3] = min( D1[1,3], D1[1,2]+D1[2,3] )
3 -3 0
= min (5, 4+7)
=5
1 2 3
1 D2[3,1] = min( D1[3,1], D1[3,2]+D1[2,1] )
D2 = 2 = min (, -3+2)
= -1
3
1 2 3
1 5 D1 = 1 0 4 5
3
4 2 2 2 0 7 k=2
2
-3 3 -3 0
1 2 3
1 0 4 5 D2[1,3] = min( D1[1,3], D1[1,2]+D1[2,3] )
D2 = 2 2 0 7 = min (5, 4+7)
=5
3 -1 -3 0
4 3
2
2
-3
1 2 3
1 0 4 5
k=3
2 2 0 7
D =
2
3 -1 -3 0
1 2 3
1 0 2 5
D3[1,2] = min(D2[1,2], D2[1,3]+D2[3,2] )
D = 3
2 2 0 7 = min (4, 5+(-3))
3 -1 -3 0 =2