1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
CHAPTERS 4 – DYNAMIC PROGRAMMING
1. Introduction
Dynamic programming, like the divide-and-conquer method, solves problems by
combining the solutions to sub problems.
Divide-and-conquer algorithms partition the problem into independent sub problems,
solve the sub problems recursively, and then combine their solutions to solve the original
problem.
In contrast, dynamic programming is applicable when the sub problems are not
independent, that is, when sub problems share sub problems.
In this context, a divide-and-conquer algorithm does more work than necessary,
repeatedly solving the common sub problems.
A dynamic-programming algorithm solves every sub problem just once and then saves
its answer in a table, thereby avoiding the work of recomputing the answer every time
the sub problem is encountered.
The development of a dynamic-programming algorithm can be broken into a sequence of
four 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 bottom-up fashion.
4. Construct an optimal solution from computed information.
2. Principle of Optimality
The dynamic programming algorithm obtains the solution using principle of optimality.
The principle of optimality states that “in an optimal sequence of decisions or choices,
each sub-sequence must also be optimal”.
DEPARTMENT OF COMPUTER ENGINEERING Page | 1
*Proprietary material of SILVER OAK UNIVERSITY
1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
When it is not possible to apply the principle of optimality it is almost impossible to
obtain the solution using the dynamic programming approach.
The principle of optimality: “If k is a 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 i, must also be optimal.”
3. Calculating the Binomial coefficient
Consider the problem of calculating binomial coefficient
Suppose 0 ≤ k ≤ n. if we calculate ( ) directly by
function C(n, k)
if k=0 or k=n then return 1
else return C(n-1, k-1)+ C(n-1, k)
Many of the values C(i, j), i < n, j < k, are calculated over and over, for example the
algorithm calculates C(5,3) as sum of C(4,2) and C(4,3) Both these intermediate results
require us to calculate C(3,2). Similarly the value of C (2,2) is used several times.
4. Making change problem using dynamic programming
4.1. Algorithm
function coins (N )
{Gives the minimum number og coins needed to make change for N units. Array
d[1..n] specifies the coinage: in the example there are coins for 1, 4 and 6 units.}
array d[1..n] =[1, 4, 6]
array c[1..n,0..N]
for i←1 to n do c[i,0]←0
DEPARTMENT OF COMPUTER ENGINEERING Page | 2
*Proprietary material of SILVER OAK UNIVERSITY
1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
for i←1 to n do
for j←1 to N do
c[i,j]←if i=1 and j < d[i] then +∞
else if i=1 then 1+ c[1,j-d[1]]
else if j < d[i] then c[i-1,j]
else min (c[i-1,j],1+ c[1,j-d[i]]
return c[n,N]
We need to generate table c[n][N]. Where,
n= number of denominations. Here we are having 3 denomination so n=3.
N= number of units that you need to make change. Here we need change of 8 units so
N=8
To generate table c[i][j] use following steps:
Step-1: Make c[i][0]=0 for 0 < i ≤ n
Step-2: Repeat step-2 to step-4 for remaining matrix Values
if i=1 then c[i][j] = 1+c[1][j-d1], here d1=1
Step-3: if j<di then c[i][j] = c[i-1][j]
Step-4L: otherwise c[i][j] = min(c[i-1][j],1+c[i][j-di]
Example: Denominations: d1=1, d2=4, d3=6. Make a change of Rs. 8.
c[i][j]=
DEPARTMENT OF COMPUTER ENGINEERING Page | 3
*Proprietary material of SILVER OAK UNIVERSITY
1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
We need minimum C[3][8]=2 coins for change
DEPARTMENT OF COMPUTER ENGINEERING Page | 4
*Proprietary material of SILVER OAK UNIVERSITY
1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
5. Assembly line scheduling
Each line has n stations: S1,1, . . . , S1,n and S2,1, . . . , S2,n
Corresponding stations S1, j and S2, j perform the same function but can take different
amounts of time a1, j and a2, j
Entry times are: e1 and e2; exit times are: x1 and x2
After going through a station, can either:
stay on same line at no cost, or
transfer to other line: cost after Si,j is ti,j , j = 1, . . . , n – 1
Steps:
f* : the fastest time to get through the entire factory
fi[j] : the fastest time to get from the starting point through station S i,j
f* = min (f1[n] + x 1, f2[n] + x2)
Base case: j = 1, i=1,2 (getting through station 1)
f1[1] = e1 + a1,1
f2[1] = e2 + a2,1
General Case: j = 2, 3, …,n, and i = 1, 2
Fastest way through S1, j is either:
the way through S 1, j - 1 then directly through S1, j, or
f1[j - 1] + a1, j
the way through S 2, j - 1, transfer from line 2 to line 1, then through S 1, j
DEPARTMENT OF COMPUTER ENGINEERING Page | 5
*Proprietary material of SILVER OAK UNIVERSITY
1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
f2[j -1] + t2,j-1 + a1,j
6. Knapsack problem
We need to generate table V(1…n,0…W) where, n= number of objects. Here n=5
DEPARTMENT OF COMPUTER ENGINEERING Page | 6
*Proprietary material of SILVER OAK UNIVERSITY
1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
W= capacity of knapsack. Here W=11
To generate table V[i][j] use following steps
Step-1: Make V[i][0] = 0 for 0 < i ≤ n
Step-2: if j < ωi then take V[i][j] = V[i-1][j]
Step-3: if j ≥ ωi then take V[i][j] = max(V[i-1][j], V[i-1][j-ωi]+Vi)
Solution: V[i][j]=
DEPARTMENT OF COMPUTER ENGINEERING Page | 7
*Proprietary material of SILVER OAK UNIVERSITY
1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
DEPARTMENT OF COMPUTER ENGINEERING Page | 8
*Proprietary material of SILVER OAK UNIVERSITY
1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
DEPARTMENT OF COMPUTER ENGINEERING Page | 9
*Proprietary material of SILVER OAK UNIVERSITY
1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
7. Shortest path
7.1. Algorithm
function Floyd(L[1..n, 1..n]) :array [1..n, 1..n]
array D[1..n, 1..n]
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
We construct a matrix D that gives the length of shortest path between each pair of
nodes.
The algorithm initializes D to L, that is, to the direct distances between nodes. It then
does n iterations, after iteration k, D gives length of the shortest paths that only use nodes
in {1, 2… k} as intermediate nodes.
DEPARTMENT OF COMPUTER ENGINEERING Page | 10
*Proprietary material of SILVER OAK UNIVERSITY
1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
After n iterations, D therefore gives the length of shortest paths using any of the nodes in
N as an intermediate node.
If Dk represents the matrix D after k th iteration it can be implemented by
Dk [i,j] = min(Dk-1 [i,j], Dk-1 [i,k]+ Dk-1 [k,j])
We use principle of optimality to compute length from i to j passing through k.
7.2. Example
DEPARTMENT OF COMPUTER ENGINEERING Page | 11
*Proprietary material of SILVER OAK UNIVERSITY
1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
8. Matrix chain multiplication
Matrix table M[i][j] stores cost of multiplications
Matrix chain orders table S[i][j] stores order of multiplication
To generate M[i][j] use following steps
Step-1 if i = j then M[i][j] = 0
Step-2 if i < j then M[i][j] = min( M[i][k]+M[k+1][j]+P [i-1]* P [k]* P [j]) with
i≤k< j
8.1. Example
Here dimensions are p0=5,p1=4,p2=6,p3=2,p4=7
DEPARTMENT OF COMPUTER ENGINEERING Page | 12
*Proprietary material of SILVER OAK UNIVERSITY
1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
DEPARTMENT OF COMPUTER ENGINEERING Page | 13
*Proprietary material of SILVER OAK UNIVERSITY
1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
PRINT_OPTIMAL (A, i, j)
1. If i=j
2. print Ai
3. Else
4. print ‘(‘
5. PRINT_OPTIMAL (A, i, k)
6. PRINT_OPTIMAL (A, k+1, j)
7. print ‘)‘
Here we are having 4 matrices let us denote it as A1,A2,A3,A4 so Calling
PRINT_OPTIMAL (A, 1, 4)
Note :For values of k see matrix S[i][j]
DEPARTMENT OF COMPUTER ENGINEERING Page | 14
*Proprietary material of SILVER OAK UNIVERSITY
1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
9. Longest chain sub-sequence
9.1. Algorithm
LCS-LENGTH(X, Y)
m ← length[X]
n ← length[Y]
for i ← 1 to m
do c[i, 0] ← 0
for j ← 0 to n
do c[0, j] ← 0
for i ← 1 to m
do for j ← 1 to n
do if xi = yj
then c[i, j] ← c[i - 1, j - 1] + 1
b[i, j] ← "↖ "
else if c[i - 1, j] ≥ c[i, j - 1]
DEPARTMENT OF COMPUTER ENGINEERING Page | 15
*Proprietary material of SILVER OAK UNIVERSITY
1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
then c[i, j] ← c[i - 1, j]
b[i, j] ← "↑"
else c[i, j] ← c[i, j - 1]
b[i, j] ← "←"
return c and b
We need to generate table c(1..m, 1..n) where m=length of string S1 and n= length of
string S2
b[i][j] stores directions like(←, ↑, ↖ )
To generate table c[i][j] use following steps
Step-1: Make c[i][0]=0 and c[0][j]=0
Step-2: if xi = yj then c[i,j] ← c[i-1,j-1]+1 and b[i,j]←”↖ ”
Step-3: else if c[i-1,j] ≥ c[I,j-1] then c[i,j] ← c[i-1,j] and b[i,j] ← ”↑”
Step-4 else c[i,j] ← c[i,j-1] and b[i,j] ← ”←”
9.2. Example
Find any one Longest Common Subsequence of given two strings using Dynamic
Programming.
S1=abbacdcba S2=bcdbbcaa
Solution:
C[i][j]=
DEPARTMENT OF COMPUTER ENGINEERING Page | 16
*Proprietary material of SILVER OAK UNIVERSITY
1010043316
(ANALYSIS & DESIGN OF
ALGORTIHM)
LECTURE COMPANION SEMESTER: 5 PREPARED BY: PARTH S WADHWA
C[i][j]=
Longest common subsequence = bcdca, Length =5
DEPARTMENT OF COMPUTER ENGINEERING Page | 17
*Proprietary material of SILVER OAK UNIVERSITY