0% found this document useful (0 votes)
0 views

Module 3 Dynamic Programming

The document covers various dynamic programming problems, including the Knapsack Problem, Longest Common Subsequence, and related algorithms. It explains different types of the Knapsack Problem, such as Fractional, Bounded, Unbounded, and 0-1 Knapsack, along with methodologies for solving them. Additionally, it discusses the concept of subsequences and provides algorithms for finding the Longest Common Subsequence between two strings.

Uploaded by

hppatilhpp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Module 3 Dynamic Programming

The document covers various dynamic programming problems, including the Knapsack Problem, Longest Common Subsequence, and related algorithms. It explains different types of the Knapsack Problem, such as Fractional, Bounded, Unbounded, and 0-1 Knapsack, along with methodologies for solving them. Additionally, it discusses the concept of subsequences and provides algorithms for finding the Longest Common Subsequence between two strings.

Uploaded by

hppatilhpp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 86

Dynamic Programming

Module 5
Knapsack Problem
Longest Common Subsequence
All Pair Shortest Path
Multistage Graph
Bellman Ford Algorithm
Travelling Salesman Problem
Flow shop Scheduling
Knapsack Problem

2 Mr. Atul Haribhau Kachare Friday, May 23, 2025


Knapsack Problem

3 Mr. Atul Haribhau Kachare 5/23/2025


Knapsack Problem
 There are two versions of the problem:
 Fractional Knapsack Problem
 Bounded Knapsack Problem
 Unbounded Knapsack Problem
 0-1 Knapsack Problem

4 Mr. Atul Haribhau Kachare 5/23/2025


Sample Knapsack Problem

Item 1 2 3 4 5
Weight 3 4 2 5 1
Profit 15 4 8 10 3

And m = 12

5 Mr. Atul Haribhau Kachare 5/23/2025


Greedy Approach
to
Dynamic Programming
Greedy 0-1 Knapsack Optimal Solution
Item 1 2 3 Item 1 2 3
Weight 3 2 2 Weight 3 2 2
Profit 300 190 180 Profit 300 190 180

Total Profit = 300 Total Profit =


190+180=370

13 Mr. Atul Haribhau Kachare 5/23/2025


Dynamic Programming Methodology

14 Mr. Atul Haribhau Kachare 5/23/2025


Dynamic Programming Methodology

15 Mr. Atul Haribhau Kachare 5/23/2025


Dynamic Programming Methodology
Consider an example,

Item 1 2 3 4 5

Weight 1 2 3 4 5

Profit 2 6 12 18 25

And m = 12

16 Mr. Atul Haribhau Kachare 5/23/2025


Dynamic Programming Methodology
Solution:
i/j 0 1 2 3 4 5 6 7 8 9 10 11 12
0
1
2
3
4
5

17 Mr. Atul Haribhau Kachare 5/23/2025


Dynamic Programming Methodology
Solution: After 1st for Loop
i/j 0 1 2 3 4 5 6 7 8 9 10 11 12
0 0 0 0 0 0 0 0 0 0 0 0 0 0
1
2
3
4
5

18 Mr. Atul Haribhau Kachare 5/23/2025


Dynamic Programming Methodology
Solution: After 1st and 2nd for Loop
i/j 0 1 2 3 4 5 6 7 8 9 10 11 12
0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
5 0

19 Mr. Atul Haribhau Kachare 5/23/2025


Dynamic Programming Methodology
Solution: After 1st and 2nd for Loop
V [1,1] = max { V [0,1] , p[1] + V [ 0 , 1 – w[1]]}
V [1,1] = max { 0 , 2+V [0 , 0 ] } = max { 0 , 2} = 2

i/j 0 1 2 3 4 5 6 7 8 9 10 11 12
0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 2
2 0
3 0
4 0
5 0
20 Mr. Atul Haribhau Kachare 5/23/2025
Dynamic Programming Methodology
Solution: Final Solution will be
i/j 0 1 2 3 4 5 6 7 8 9 10 11 12
0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 2 2 2 2 2 2 2 2 2 2 2 2
2 0 2 6 8 8 8 8 8 8 8 8 8 8
3 0 2 6 12 14 18 20 20 20 20 20 20 20
4 0 2 6 12 18 20 24 30 32 36 38 38 38
5 0 2 6 12 18 25 27 31 37 43 45 49 55
21 Mr. Atul Haribhau Kachare 5/23/2025
Knapsack Problem
- Application
 Construction and Scoring Test.
 Finding least wasteful way to cut raw materials.

22 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
 Substring
 Part of String
 Continuous set of characters
 Subsequence
 Part of String
 May or not ne continuous
 Detected while traversing in a string from Left to Right
 Example:
 String: analysis of algorithms
 Substring: lysis, algo, algorithm, etc.
 Subsequence: aaatm, lyth, analysts, etc
23 Mr. Atul Haribhau Kachare 5/23/2025
Longest Common Subsequence
 Common Subsequence:
 A subsequence present between two strings
 There can be multiple subsequences
 Longest Common Subsequence
 A common subsequence of largest length

24 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
 Overlapping Subproblem:
 Break string X & Y character by character.
 Recursive Solution:
 C [i, j ] = max { max (C[i-1][j], c[i][j-1]) , C[i-1][j-1] + 1}
 Accept: C[i-1][j-1] + 1
 Reject:
 SKIP X: C[i-1][j]
 SKIPY: C[i][j-1]

25 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
Algorithm
//X : String 1
//Y: String 2
m → length of X
n → length of Y
for i=0 to m do
C[i][0] = 0
for j=0 to n do
C[0][j] = 0

26 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
for i =1 to m do
for j =1 to n do
if X[i] = Y [j] then
C[i][j] = C[i-1][j-1] + 1
B[i][j] = “ ”
else if C[i-1][j] > = C [i][j-1] then
C[i][j] = C[i-1][j]
B[i][j] = “ ”
else
C[i][j] = C[i][j-1]
27 Mr. Atul Haribhau Kachare B[i][j] = “ ” 5/23/2025
Longest Common Subsequence
Let X = aabaaba & Y = abaa

‘\0’ a b a a
‘\0’ 0 0 0 0 0
a 0
a 0
b 0
a 0
a 0
b 0
a 0

28 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
Let X = aabaaba & Y = abaa

‘\0’ a b a a
‘\0’ 0 0 0 0 0
a 0 1
a 0
b 0
a 0
a 0
b 0
a 0

29 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
Let X = aabaaba & Y = abaa

‘\0’ a b a a
‘\0’ 0 0 0 0 0
a 0 1 1
a 0
b 0
a 0
a 0
b 0
a 0

30 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
Let X = aabaaba & Y = abaa

‘\0’ a b a a
‘\0’ 0 0 0 0 0
a 0 1 1 1
a 0
b 0
a 0
a 0
b 0
a 0

31 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
Let X = aabaaba & Y = abaa

‘\0’ a b a a
‘\0’ 0 0 0 0 0
a 0 1 1 1 1
a 0
b 0
a 0
a 0
b 0
a 0

32 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
Let X = aabaaba & Y = abaa

‘\0’ a b a a
‘\0’ 0 0 0 0 0
a 0 1 1 1 1
a 0 1 1 2 2
b 0
a 0
a 0
b 0
a 0

33 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
Let X = aabaaba & Y = abaa

‘\0’ a b a a
‘\0’ 0 0 0 0 0
a 0 1 1 1 1
a 0 1 1 2 2
b 0 1 2 2 2
a 0
a 0
b 0
a 0

34 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
Let X = aabaaba & Y = abaa

‘\0’ a b a a
‘\0’ 0 0 0 0 0
a 0 1 1 1 1
a 0 1 1 2 2
b 0 1 2 2 2
a 0 1 2 3 3
a 0
b 0
a 0

35 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
Let X = aabaaba & Y = abaa

‘\0’ a b a a
‘\0’ 0 0 0 0 0
a 0 1 1 1 1
a 0 1 1 2 2
b 0 1 2 2 2
a 0 1 2 3 3
a 0 1 2 3 4
b 0 1 2 3 4
a 0 1 2 3 4

36 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
Let X = aabaaba & Y = abaa
Longest Common Subsequence Length: 4
Longest Common Subsequence: abaa

37 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
 X = ACBAED
 Y = ABCABE

38 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
 X = ACBAED & Y = ABCABE 4, ABAE
‘\0’ A B C A B E

‘\0’ 0 0 0 0 0 0 0

A 0 1 1 1 1 1 1

C 0 1 1 2 2 2 2

B 0 1 2 2 2 3 3

A 0 1 2 2 3 3 3

E 0 1 2 2 3 3 4

D 0 1 2 2 3 3 4

39 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
 X = GCGCAATG
 Y = GCCCTAGCG

40 Mr. Atul Haribhau Kachare 5/23/2025


Longest Common Subsequence
 X = GCGCAATG & Y = GCCCTAGCG 5, GCCTG
‘\0’ G C C C T A G C G

‘\0’ 0 0 0 0 0 0 0 0 0 0

G 0 1 1 1 1 1 1 1 1 1

C 0 1 2 2 2 2 2 2 2 2

G 0 1 2 2 2 2 2 3 3 3

C 0 1 2 3 3 3 3 3 4 4

A 0 1 2 3 3 3 4 4 4 4

A 0 1 2 3 3 3 4 4 4 4

T 0 1 2 3 3 4 4 4 4 4

41 G
Mr. Atul Haribhau Kachare 0 1 2 3 3 4 4 5 5 5 5/23/2025
All Pair Shortest Path
 The all-pair shortest path algorithm is also known as Floyd-Warshall algorithm is
used to find all pair shortest path problem from a given weighted graph.
 As a result of this algorithm, it will generate a matrix, which will represent the
minimum distance from any node to all other nodes in the graph.

42 Mr. Atul Haribhau Kachare 5/23/2025


All Pair Shortest Path
Algorithm:
//n : number of vertices
for i = 0 to n do
for j = 0 to n do
a[i][j]=cost[i][j]
for k = 0 to n do
for i = 0 to n do
for j = 0 to n do
a[i][j]=min{a[i][j] , a[i][k]+a[k][j]}

43 Mr. Atul Haribhau Kachare 5/23/2025


All Pair Shortest Path
1
6 3
4 2 0 4 2 0 4 2
2 3 𝐴0 = 6 0 5 𝐴1 = 6 0 5
5 3 ∞ 0 3 7 0

0 4 2 0 4 2 0 4 2
𝐴2 = 6 0 5 𝐴3 = 6 0 5
6 0 5 3 7 0 3 7 0
3 ∞ 0

44 Mr. Atul Haribhau Kachare 5/23/2025


All Pair Shortest Path
A B C D E A B C D E
A 0 6 3 ∞ ∞ A 0 6 3 ∞ ∞
B 4 0 ∞ 1 ∞ B 4 0 7 1 ∞
𝐴0 = C ∞ ∞ 0 5 1 𝐴𝐴 = C ∞ ∞ 0 5 1
D ∞ 3 ∞ 0 ∞ D ∞ 3 ∞ 0 ∞
E ∞ ∞ ∞ 2 0 E ∞ ∞ ∞ 2 0
A B C D E A B C D E
A 0 6 3 7 ∞ A 0 6 3 7 4
A B C D E
B 4 0 7 1 ∞ B 4 0 7 1 8
A 0 6 3 ∞ ∞
𝐴𝐵 = C ∞ ∞ 0 5 1 𝐴𝐶 = C ∞ ∞ 0 5 1
B 4 0 ∞ 1 ∞
D 7 3 10 0 ∞ D 7 3 10 0 11
C ∞ ∞ 0 5 1
E ∞ ∞ ∞ 2 0 E ∞ ∞ ∞ 2 0
D ∞ 3 ∞ 0 ∞
E ∞ ∞ ∞ 2 0
45 Mr. Atul Haribhau Kachare 5/23/2025
All Pair Shortest Path
A B C D E
A 0 6 3 7 4
B 4 0 7 1 8
𝐴𝐷 = C 12 8 0 5 1
D 7 3 10 0 11
E 9 5 12 2 0

A B C D E A B C D E
A 0 6 3 ∞ ∞ A 0 6 3 6 4
B 4 0 ∞ 1 ∞ B 4 0 7 1 8
C ∞ ∞ 0 5 1 𝐴𝐸 = C 10 6 0 3 1
D ∞ 3 ∞ 0 ∞ D 7 3 10 0 11
E ∞ ∞ ∞ 2 0 E 9 5 12 2 0
46 Mr. Atul Haribhau Kachare 5/23/2025
All Pair Shortest Path
1 2 3 4
1 0 8 ∞ 1
2 ∞ 0 1 ∞
𝐶𝑜𝑠𝑡 =
3 4 ∞ 0 ∞
4 ∞ 2 9 0
1 2 3 4 1 2 3 4
1 0 8 ∞ 1 1 0 8 9 1
2 ∞ 0 1 ∞ 2 ∞ 0 1 ∞
1 2 3 4 𝐴1 = 𝐴2 =
3 4 12 0 5 3 4 12 0 5
1 0 8 9 1 4 ∞ 2 9 0 4 ∞ 2 3 0
2 5 0 1 6 1 2 3 4
𝐴3 =
3 4 12 0 5 1 0 3 4 1
4 7 2 3 0 2 5 0 1 6
𝐴4 =
3 4 7 0 5
47 Mr. Atul Haribhau Kachare 5/23/2025
4 7 2 3 0
Multistage Graph
 A multistage graph G = (V, E) is a directed graph in which vertices are partitioned
into k ≥ 2 disjoint sets 𝑣𝑖 where value of i is ranging from 1to k.
 In addition, if (u, v) is an edge in a graph G then u 𝜖 𝑣𝑖 & the v 𝜖 𝑣𝑖+1 where i is
ranging from 1 to k.
 The cardinality of set 𝑣1 and 𝑣𝑘 is equal to 1.
 Let S & T be the vertices in 𝑣1 and 𝑣𝑘 respectively then S is called as Source Vertex
whereas T is called SinkVertex.
 The cost of the graph from S to T is given by sum of cost of edges on the path from
S to T.

48 Mr. Atul Haribhau Kachare 5/23/2025


Multistage Graph
 Forward Approach
In this we start from Source Vertex and reach to Sink Vertex
 Backward Approach
In this we start from Sink Vertex and reach to Source Vertex

49 Mr. Atul Haribhau Kachare 5/23/2025


4
2 5

Multistage Graph 1 11 18
9
2 7 13
1 3 6 8

3 2
4 7
Using Forward Approach: 2
d(1, 8) = min { cost(1, 2) + d(2, 8), cost(1, 3) + d(3, 8), cost(1, 4) + d(4, 8)}
d(1, 8) = min { 1 + d(2, 8), 2 + d(3, 8), 3 + d(4, 8)} ………………...(1)

d(2, 8) = min { cost(2, 5) + d(5, 8), cost(2, 6) + d(6, 8)}


d(2, 8) = min { 4 + d(5, 8), 11 + d(6, 8)}…………………………….(2)

d(3, 8) = min { cost(3, 5) + d(5, 8), cost(3, 6) + d(6, 8)}


d(3, 8) = min { 9 + d(5, 8), 7 + d(6, 8)} ……………………………..(3)

d(4, 8) = min { cost(4, 7) + d(7, 8)}


d(4, 8) = 2 + d(7, 8) ……………………………………………….…(4)

50 Mr. Atul Haribhau Kachare 5/23/2025


4
2 5

Multistage Graph 1 11 18
9
2 7 13
Using Forward Approach: 1 3 6 8
Now, we know that
d(5, 8) = cost (5, 8) = 18 3 2
d(6, 8) = cost (6, 8) = 13
d(7, 8) = cost (7, 8) = 2 4 7
2
Using above values in previous equations 2, 3, 4 we get
d(2, 8) = min { 4 + d(5, 8), 11 + d(6, 8)} = min { 4+ 18, 11+13} = min {22, 24} = 22
d(3, 8) = min { 9 + d(5, 8), 7 + d(6, 8)} = min { 9+ 18, 7+13} = min {27, 20} = 20
d(4, 8) = 2 + d(7, 8) = 2 + 2 = 4

Putting all values in equation 1 we get


d(1, 8) = min { 1 + d(2, 8), 2 + d(3, 8), 3 + d(4, 8)}
d(1, 8) = min { 1 + 22, 2 + 20, 3 + 4}
d(1, 8) = min { 23, 22, 7}= 7

PATH: 1→ 4 → 7 → 8
51 Mr. Atul Haribhau Kachare 5/23/2025
4
2 5

Multistage Graph 1 11 18
9
2 7 13
1 3 6 8

3 2
4 7
Using Backward Approach: 2
d(1, 8) = min { d(1, 5) + cost(5, 8), d(1, 6) + cost(6, 8), d(1, 7) + cost(7, 8)}
d(1, 8) = min { d(1, 5) + 18, d(1, 6) + 13, d(1, 7) + 2} ………….....(1)

d(1, 5) = min { d(1, 2) + cost(2, 5), d(1, 3) + cost(3, 5)}


d(1, 5) = min { d(1, 2) + 4, d(1, 3) + 9}…………………………….(2)

d(1, 6) = min { d(1, 2) + cost(2, 6), d(1, 3) + cost(3, 6)}


d(1, 6) = min { d(1, 2) + 11, d(1, 3) + 7}…..……………………….(2)

d(1, 7) = min { d(1, 4) + cost(4, 7)}


d(1, 7) = d(1, 4) + 2 ….………………………………………….…(4)

52 Mr. Atul Haribhau Kachare 5/23/2025


4
2 5

Multistage Graph 1 11 18
9
2 7 13
Using Backward Approach: 1 3 6 8
Now, we know that
d(1, 2) = cost (1, 2) = 1 3 2
d(1, 3) = cost (1, 3) = 2
d(1, 4) = cost (1, 4) = 3 4 7
2
Using above values in previous equations 2, 3, 4 we get
d(1, 5) = min { d(1, 2) + 4, d(1, 3) + 9} = min { 1+ 4, 2+9} = min {5, 11} = 5
d(1, 6) = min { d(1, 2) + 11, d(1, 3) + 7} = min { 1+ 11, 2+7} = min {12, 9} = 9
d(1, 7) = d(1, 4) + 2 = 3 + 2 = 5

Putting all values in equation 1 we get


d(1, 8) = min { d(1, 5) + 18, d(1, 6) + 13, d(1, 7) + 2}
d(1, 8) = min { 5 + 18, 9 + 13, 5 + 2}
d(1, 8) = min { 23, 22, 7}= 7

PATH: 1→ 4 → 7 → 8
53 Mr. Atul Haribhau Kachare 5/23/2025
2
1
Multistage Graph
7 2 4
1 3 5 7
8
6 12
4 6
4
Using Forward Approach:
d(1, 7) = min { cost(1, 2) + d(2, 7), cost(1, 3) + d(3, 7), cost(1, 4) + d(4, 7)}
d(1, 7) = min { 9 + d(2, 7), 7 + d(3, 7), 6 + d(4, 7)} ………………...(1)

d(2, 7) = min { cost(2, 5) + d(5, 7)}


d(2, 7) = 1 + d(5, 7)……………………….………………………….(2)

d(3, 7) = min { cost(3, 5) + d(5, 7), cost(3, 6) + d(6, 7)}


d(3, 7) = min { 2 + d(5, 7), 8 + d(6, 7)} ……………………………..(3)

d(4, 7) = min {cost(4, 5) + d(5, 7), cost(4, 6) + d(6, 7)}


d(4, 7) = min { 8 + d(5, 7), 4 + d(6, 7)} .………………………….…(4)
54 Mr. Atul Haribhau Kachare 5/23/2025
2
1
Multistage Graph 9

7 2 4
Using Forward Approach: 1 3 5 7
Now, we know that 8
d(5, 7) = cost (5, 7) = 4 6 12
d(6, 7) = cost (6, 7) = 12
4 6
4
Using above values in previous equations 2, 3, 4 we get
d(2, 7) = 1 + d(5, 7) = 1+4 = 5
d(3, 7) = min { 2 + d(5, 7), 8 + d(6, 7)} = min { 2+ 4, 8+12} = min {6, 20} = 6
d(4, 7) = min { 8 + d(5, 7), 4 + d(6, 7)} = min { 8+ 4, 4+12} = min {12, 16} = 12

Putting all values in equation 1 we get


d(1, 7) = min { 9 + d(2, 7), 7 + d(3, 7), 6 + d(4, 7)}
d(1, 7) = min { 9 + 5, 7 + 6, 6 + 12}
d(1, 7) = min { 14, 13, 18}= 13

PATH: 1→ 3 → 5 → 7

55 Mr. Atul Haribhau Kachare 5/23/2025


2
1
Multistage Graph 9

7 2 4
1 3 5 7
8
6 12
4 6
4
Using Backward Approach:
d(1, 7) = min { d(1, 5) + cost(5, 7), d(1, 6) + cost(6, 7)}
d(1, 7) = min { d(1, 5) + 4, d(1, 6) + 12} …………………………….…..…...(1)

d(1, 5) = min { d(1, 2) + cost(2, 5), d(1, 3) + cost(3, 5), d(1, 4) + cost(4, 5)}
d(1, 5) = min { d(1, 2) + 1, d(1, 3) + 2, d(1, 4) + 8} ………………………….(2)

d(1, 6) = min { d(1, 3) + cost(3, 6), d(1, 4) + cost(4, 6)}


d(1, 6) = min { d(1, 3) + 8, d(1, 4) + 4} ………….………………….………..(3)

56 Mr. Atul Haribhau Kachare 5/23/2025


2
1
Multistage Graph 9

7 2 4
Using Backward Approach: 1 3 5 7
Now, we know that 8
d(1, 2) = cost (1, 2) = 9 6 12
d(1, 3) = cost (1, 3) = 7
d(1, 4) = cost (1, 4) = 6 4 6
4
Using above values in previous equations 2, 3 we get
d(1,5) = min{ d(1,2)+1, d(1,3)+2, d(1,4)+8} = min {9+1,7+2,6+8} = min {10,9,14} = 9
d(1, 6) = min { d(1, 3) + 8, d(1, 4) + 4} = min { 7+ 8, 6+4} = min {15, 10} = 10

Putting all values in equation 1 we get


d(1, 7) = min { d(1, 5) + 4, d(1, 6) + 12}
d(1, 7) = min { 9 + 4, 10 + 12}
d(1, 7) = min { 13, 22}= 13

PATH: 1→ 3 → 5 → 7

57 Mr. Atul Haribhau Kachare 5/23/2025


Single Source Shortest Path
 Let G is the graph, and we want to find out the shortest path from source vertex 1 to all other vertices.
 Algorithm:
function bellmanford (v,cost,dist,n)
{
//v is source vertex; cost is cost matrix of given graph G
//dist is array to store shortest path from source to vertex
//n is number of vertices in graph
for i=1 to n do
d [i] = cost [ v, i ]//initial dist. Bet. v and vertex i
for num =2 to n-1 do
for each u such that u ≠ source and u has at least one incoming edge do
for each ( i , u ) in the graph do
if dist [u] > dist [i] + cost [i] [u] then
dist [u] = dist [i] + cost [i] [u]
}

58 Mr. Atul Haribhau Kachare 5/23/2025


Example

Distance
K
1 2 3 4 5 6 7
1 0 6 5 5 ∞ ∞ ∞

59 Mr. Atul Haribhau Kachare 5/23/2025


Example

Distance
K
1 2 3 4 5 6 7
1 0 6 5 5 ∞ ∞ ∞
2 0 3 3 5 5 4 7

60 Mr. Atul Haribhau Kachare 5/23/2025


Example

Distance
K
1 2 3 4 5 6 7
1 0 6 5 5 ∞ ∞ ∞
2 0 3 3 5 5 4 7
3 0 1 3 5 0 4 3

61 Mr. Atul Haribhau Kachare 5/23/2025


Example

Distance
K
1 2 3 4 5 6 7
1 0 6 5 5 ∞ ∞ ∞
2 0 3 3 5 5 4 7
3 0 1 3 5 0 4 3
4 0 1 3 5 0 4 3

62 Mr. Atul Haribhau Kachare 5/23/2025


Example

Distance
K
1 2 3 4 5 6 7
1 0 6 5 5 ∞ ∞ ∞
2 0 3 3 5 5 4 7
3 0 1 3 5 0 4 3
4 0 1 3 5 0 4 3
5 0 1 3 5 0 4 3
6 0 1 3 5 0 4 3

63 Mr. Atul Haribhau Kachare 5/23/2025


Example

Distance
K
0 1 2 3 4

1 0 6 ∞ 7 ∞

2 0 6 4 7 2

3 0 2 4 7 2

4 0 2 4 7 -2

64 Mr. Atul Haribhau Kachare 5/23/2025


Example

Distance
K
1 2 3 4 5

1 0 6 5 ∞ ∞

2 0 3 5 5 8

3 0 3 5 2 5

4 0 3 5 2 5

65 Mr. Atul Haribhau Kachare 5/23/2025


Principle of Optimality
The principle of optimality is the basic principle of dynamic programming, which
was developed by Richard Bellman: that an optimal path has the property that
whatever the initial conditions and control variables (choices) over some initial
period, the control (or decision variables) chosen over the remaining period must be
optimal for the remaining problem, with the state resulting from the early decisions
taken to be the initial condition.

66 Mr. Atul Haribhau Kachare 5/23/2025


Travelling Salesman Problem
 A traveler needs to visit all the cities from a list, where distances between all the
cities are known and each city should be visited just once. What is the shortest
possible route that he visits each city exactly once and returns to the origin city?
 It is an NP-hard problem in combinatorial optimization, important in theoretical
computer science and operations research.

67 Mr. Atul Haribhau Kachare 5/23/2025


Travelling Salesman Problem
 Travelling salesman problem is the most notorious computational
problem. We can use brute-force approach to evaluate every
possible tour and select the best one. For n number of vertices in
a graph, there are (n - 1)! number of possibilities.
 Instead of brute-force using dynamic programming approach, the
solution can be obtained in lesser time, though there is no
polynomial time algorithm.
 Let us consider a graph G = (V, E), where V is a set of cities and E
is a set of weighted edges. An edge e(u, v) represents that vertices
u and v are connected. Distance between vertex u and v is d(u,
v), which should be non-negative.
68 Mr. Atul Haribhau Kachare 5/23/2025
Travelling Salesman Problem
 Cost of going from city i to city 1 is given as,
𝑔 𝑖, ∅ = 𝐶𝑖1
 Cost of going from city i to another city(ies) and coming to city 1 is given as,
𝑚𝑖𝑛
𝑔 𝑖, 𝑆 = 𝑗 ∈𝑆
𝐶𝑖𝑗 + 𝑔(𝑗, 𝑆 − 𝑗 )
 Cost of going from city 1 to all other cities and coming back to city 1 is given as,
𝑚𝑖𝑛
𝑔 1, 𝑉 − {1} = 2 ≤𝑘≤𝑛 𝐶1𝑘 + 𝑔(𝑘, 𝑉 − 1, 𝑘 )

69 Mr. Atul Haribhau Kachare 5/23/2025


Travelling Salesman Problem
0 10 15 20
𝐶𝑜𝑠𝑡 = 5 0 9 10
6 13 0 12
8 8 9 0

When S = ∅,
𝑔 2, ∅ = 𝐶21 = 5
𝑔 3, ∅ = 𝐶31 = 6
𝑔 4, ∅ = 𝐶41 = 8

70 Mr. Atul Haribhau Kachare 5/23/2025


Travelling Salesman Problem
0 10 15 20
𝐶𝑜𝑠𝑡 = 5 0 9 10
6 13 0 12
8 8 9 0

When 𝑆 = 1,
𝑔 2, {3} = 𝐶23 + 𝑔 3, ∅ = 9 + 6 = 15
𝑔 2, {4} = 𝐶24 + 𝑔 4, ∅ = 10 + 8 = 18
𝑔 3, {2} = 𝐶32 + 𝑔 2, ∅ = 13 + 5 = 18
𝑔 3, {4} = 𝐶34 + 𝑔 4, ∅ = 12 + 8 = 20
𝑔 4, {2} = 𝐶42 + 𝑔 2, ∅ = 8 + 5 = 13
𝑔 4, {3} = 𝐶43 + 𝑔 3, ∅ = 9 + 6 = 15
71 Mr. Atul Haribhau Kachare 5/23/2025
Travelling Salesman Problem
0 10 15 20
𝐶𝑜𝑠𝑡 = 5 0 9 10
6 13 0 12
8 8 9 0

When 𝑆 = 2,
𝑔 2, {3,4} = min 𝐶23 + 𝑔 3, {4} , 𝐶24 + 𝑔 4, {3}
𝑔 2, {3,4} = min 9 + 20,10 + 15 = min 29,25 = 25
𝑔 3, {2,4} = min 𝐶32 + 𝑔 2, {4} , 𝐶34 + 𝑔 4, {2}
𝑔 3, {2,4} = min 13 + 18,12 + 13 = min 31,25 = 25
𝑔 4, {2,3} = min 𝐶42 + 𝑔 2, {3} , 𝐶43 + 𝑔 3, {2}
𝑔 4, {2,3} = min 8 + 15,9 + 18 = min 23,27 = 23
72 Mr. Atul Haribhau Kachare 5/23/2025
Travelling Salesman Problem
0 10 15 20
𝐶𝑜𝑠𝑡 = 5 0 9 10
6 13 0 12
8 8 9 0

When 𝑆 = 3,
𝑔 1, {2,3,4}
= min 𝐶12 + 𝑔 2, {3,4} , 𝐶13 + 𝑔 3, {2,4} , 𝐶14 + 𝑔 4, {2,3}
= min 10 + 25,15 + 25,20 + 23
= min 35,40,43
= 35
Path: 1→ 2 → 4 → 3 → 1
73 Mr. Atul Haribhau Kachare 5/23/2025
Travelling Salesman Problem
Analysis:
 Using Brute Force Method: 𝑂 𝑛!
 Using Dynamic Programming: 𝑂 2𝑛 𝑛2
 Using Branch & Bound: Worst Case - 𝑂 𝑛!

74 Mr. Atul Haribhau Kachare 5/23/2025


Travelling Salesman Problem
0 4 1 3
𝐶𝑜𝑠𝑡 = 4 0 2 1
1 2 0 5
3 1 5 0

When S = ∅,
𝑔 2, ∅ = 𝐶21 = 4
𝑔 3, ∅ = 𝐶31 = 1
𝑔 4, ∅ = 𝐶41 = 3

75 Mr. Atul Haribhau Kachare 5/23/2025


Travelling Salesman Problem
0 4 1 3
𝐶𝑜𝑠𝑡 = 4 0 2 1
1 2 0 5
3 1 5 0
When 𝑆 = 1,
𝑔 2, {3} = 𝐶23 + 𝑔 3, ∅ =2+1=3
𝑔 2, {4} = 𝐶24 + 𝑔 4, ∅ =1+3=4
𝑔 3, {2} = 𝐶32 + 𝑔 2, ∅ =2+4=6
𝑔 3, {4} = 𝐶34 + 𝑔 4, ∅ =5+3=8
𝑔 4, {2} = 𝐶42 + 𝑔 2, ∅ =1+4=5
𝑔 4, {3} = 𝐶43 + 𝑔 3, ∅ =5+1=6
76 Mr. Atul Haribhau Kachare 5/23/2025
Travelling Salesman Problem
0 4 1 3
𝐶𝑜𝑠𝑡 = 4 0 2 1
1 2 0 5
3 1 5 0
When 𝑆 = 2,
𝑔 2, {3,4} = min 𝐶23 + 𝑔 3, {4} , 𝐶24 + 𝑔 4, {3}
𝑔 2, {3,4} = min 2 + 8,1 + 6 = min 10,7 = 7
𝑔 3, {2,4} = min 𝐶32 + 𝑔 2, {4} , 𝐶34 + 𝑔 4, {2}
𝑔 3, {2,4} = min 2 + 4,5 + 5 = min 6,10 = 6
𝑔 4, {2,3} = min 𝐶42 + 𝑔 2, {3} , 𝐶43 + 𝑔 3, {2}
𝑔 4, {2,3} = min 1 + 3,5 + 6 = min 4,11 = 4
77 Mr. Atul Haribhau Kachare 5/23/2025
Travelling Salesman Problem
0 4 1 3
𝐶𝑜𝑠𝑡 = 4 0 2 1
1 2 0 5
3 1 5 0
When 𝑆 = 3,
𝑔 1, {2,3,4}
= min 𝐶12 + 𝑔 2, {3,4} , 𝐶13 + 𝑔 3, {2,4} , 𝐶14 + 𝑔 4, {2,3}
= min 4 + 7,1 + 6,3 + 4
= min 11,7,7
=7
Path: 1→ 3 → 2 → 4 → 1 OR 1→ 4 → 2 → 3 → 1
78 Mr. Atul Haribhau Kachare 5/23/2025
Assembly Line Scheduling
A car manufacturing company has two assembly lines, each with n stations. A station is
denoted by 𝑆𝑖𝑗 where i denotes the assembly line the station is on, and j denotes the
number of the station. The time taken per station is denoted by 𝑎𝑖𝑗 . Each station is
dedicated to do some sort of work in the manufacturing process. So, a car must pass
through each of the n stations in order before exiting the company. The parallel stations
of the two assembly lines perform the same task. After it passes through station 𝑆𝑖𝑗 , it
will continue to station 𝑆𝑖𝑗+1 unless it decides to transfer to the other line. Continuing the
same line incurs no extra cost but transferring from line i at station j − 1 to station j on
the other line takes time 𝑡𝑖𝑗 . Each assembly line takes an entry time 𝑒𝑖 and exit time 𝑥𝑖 .
Our task here is to develop an algorithm for computing the minimum time from
start to exit.

79 Mr. Atul Haribhau Kachare 5/23/2025


Assembly Line Scheduling
 In this problem we need to start from one of the assembly lines and then at each
point we have two options whether to switch to other assembly line or stay on the
same line.
 Thus, basically if we are at a particular station say 𝑆𝑖𝑗 then we can determine the
time taken by simply adding 𝑎𝑖𝑗 to the previous station cost.
 This approach seems like the recursive technique used in Factorial problem.
 Indeed, we shall be using recursion here and with the concept of Dynamic
Programming, we will optimize our recursive solution so that it requires lesser
time complexity.

80 Mr. Atul Haribhau Kachare 5/23/2025


Assembly Line Scheduling
 Base Case:
The entry times e1, e2 comes into picture only when the car enters the car factory. Thus,
time to leave station 1 on both lines serves as our base case:
𝑓1 1 = 𝑒1 + 𝑎11
𝑓2 1 = 𝑒2 + 𝑎21
 Recursive Subproblem
The car at station 𝑆1𝑗 can come either from station 𝑆1𝑗−1 or station 𝑆2𝑗−1 (Since, the tasks
done by both 𝑆1𝑗 and 𝑆2𝑗 are same). But if the chassis comes from 𝑆2𝑗−1 , it additionally
incurs the transfer cost for changing the assembly line. Thus, the recursion relation to reach
the station j in assembly line i can be obtained as follows:

81 Mr. Atul Haribhau Kachare 5/23/2025


Assembly Line Scheduling
𝑒1 + 𝑎11 𝑖𝑓 𝑗 = 1
𝑓1 𝑗 = ൝
𝑚𝑖𝑛 𝑓1 𝑗 − 1 + 𝑎1𝑗 , 𝑓2 𝑗 − 1 + 𝑡2𝑗−1 + 𝑎1𝑗 𝑖𝑓 𝑗 ≥ 2

𝑒2 + 𝑎21 𝑖𝑓 𝑗 = 1
𝑓2 𝑗 = ൝
𝑚𝑖𝑛 𝑓2 𝑗 − 1 + 𝑎2𝑗 , 𝑓1 𝑗 − 1 + 𝑡1𝑗−1 + 𝑎2𝑗 𝑖𝑓 𝑗 ≥ 2

𝑓𝑜𝑝𝑡𝑖𝑚𝑎𝑙 = min 𝑓1 𝑗 + 𝑥1 , 𝑓2 𝑗 + 𝑥2

82 Mr. Atul Haribhau Kachare 5/23/2025


Assembly Line Scheduling : Algo
𝒇𝟏 𝟏 = 𝒆𝟏 + 𝒂𝟏𝟏
𝒇𝟐 𝟏 = 𝒆𝟐 + 𝒂𝟐𝟏
for j = 2 to n do
if 𝒇𝟏 𝒋 − 𝟏 + 𝒂𝟏𝒋 ≤ 𝒇𝟐 𝒋 − 𝟏 + 𝒕𝟐𝒋−𝟏 + 𝒂𝟏𝒋 then
𝒇𝟏 𝒋 = 𝒇𝟏 𝒋 − 𝟏 + 𝒂𝟏𝒋 and 𝒍𝟏 𝒋 = 𝟏
else
𝒇𝟏 𝒋 =𝒇𝟐 𝒋 − 𝟏 + 𝒕𝟐𝒋−𝟏 + 𝒂𝟏𝒋 and 𝒍𝟏 𝒋 =2

if 𝒇𝟐 𝒋 − 𝟏 + 𝒂𝟐𝒋 ≤ 𝒇𝟏 𝒋 − 𝟏 + 𝒕𝟏𝒋−𝟏 + 𝒂𝟐𝒋 then


𝒇𝟐 𝒋 = 𝒇𝟐 𝒋 − 𝟏 + 𝒂𝟐𝒋 and 𝒍𝟏 𝒋 = 𝟐
else
𝒇𝟐 𝒋 =𝒇𝟏 𝒋 − 𝟏 + 𝒕𝟏𝒋−𝟏 + 𝒂𝟐𝒋 and 𝒍𝟏 𝒋 = 𝟏
if 𝒇𝟏 𝒏 + 𝒙𝟏 ≤ 𝒇𝟐 𝒏 + 𝒙𝟐 then
𝒇𝒐𝒑𝒕𝒊𝒎𝒂𝒍 = 𝒇𝟏 𝒏 + 𝒙𝟏 and 𝒍𝒐𝒑𝒕𝒊𝒎𝒂𝒍 = 𝟏
else
𝒇𝒐𝒑𝒕𝒊𝒎𝒂𝒍 = 𝒇𝟐 𝒏 + 𝒙𝟐 and 𝒍𝒐𝒑𝒕𝒊𝒎𝒂𝒍 = 𝟐

83 Mr. Atul Haribhau Kachare 5/23/2025


Assembly Line Scheduling

𝑓1 1 = 𝑒1 + 𝑎11 = 3 + 5 = 8
𝑓2 1 = 𝑒2 + 𝑎21 = 2 + 2 = 4
𝑓1 2 = min 𝑓1 1 + 𝑎12 , 𝑓2 1 + 𝑡22 + 𝑎12
𝑓1 2 = min 8 + 4, 4 + 1 + 4 = min 12, 9 = 9
𝑓2 2 = min 𝑓2 1 + 𝑎22 , 𝑓1 1 + 𝑡12 + 𝑎22
𝑓2 2 = min 4 + 3, 8 + 2 + 3 = min 7, 13 = 7
84 Mr. Atul Haribhau Kachare 5/23/2025
Assembly Line Scheduling

𝑓1 3 = min 𝑓1 2 + 𝑎13 , 𝑓2 2 + 𝑡23 + 𝑎13


𝑓1 3 = min 9 + 3, 7 + 1 + 3 = min 12, 11 = 11
𝑓2 3 = min 𝑓2 2 + 𝑎23 , 𝑓1 2 + 𝑡13 + 𝑎23
𝑓2 3 = min 7 + 7, 9 + 2 + 7 = min 14, 18 = 14
𝑓𝑜𝑝𝑡𝑖𝑚𝑎𝑙 = 𝑚𝑖𝑛 𝑓1 3 + 𝑥1 , 𝑓2 3 + 𝑥2
𝑓𝑜𝑝𝑡𝑖𝑚𝑎𝑙 = 𝑚𝑖𝑛 11 + 3, 14 + 4 = 14
PATH: 𝑒2 → 𝑎21 → 𝑎22 → 𝑎13 → 𝑥1
85 Mr. Atul Haribhau Kachare 5/23/2025
Assembly Line Scheduling

𝑓1 3 = min 𝑓1 2 + 𝑎13 , 𝑓2 2 + 𝑡23 + 𝑎13


𝑓1 3 = min 9 + 3, 7 + 1 + 3 = min 12, 11 = 11
𝑓2 3 = min 𝑓2 2 + 𝑎23 , 𝑓1 2 + 𝑡13 + 𝑎23
𝑓2 3 = min 7 + 7, 9 + 2 + 7 = min 14, 18 = 14
𝑓𝑜𝑝𝑡𝑖𝑚𝑎𝑙 = 𝑚𝑖𝑛 𝑓1 3 + 𝑥1 , 𝑓2 3 + 𝑥2
𝑓𝑜𝑝𝑡𝑖𝑚𝑎𝑙 = 𝑚𝑖𝑛 11 + 3, 14 + 4 = 14
PATH: 𝑒2 → 𝑎21 → 𝑎22 → 𝑎13 → 𝑥1
86 Mr. Atul Haribhau Kachare 5/23/2025
Assembly Line Scheduling

𝑓1 1 = 𝑒1 + 𝑎11 = 10 + 4 = 14
𝑓2 1 = 𝑒2 + 𝑎21 = 12 + 2 = 14
𝑓1 2 = min 𝑓1 1 + 𝑎12 , 𝑓2 1 + 𝑡22 + 𝑎12
𝑓1 2 = min 14 + 5, 14 + 9 + 5 = min 19, 28 = 19
𝑓2 2 = min 𝑓2 1 + 𝑎22 , 𝑓1 1 + 𝑡12 + 𝑎22
𝑓2 2 = min 14 + 10, 14 + 7 + 10 = min 24, 31 = 24
87 Mr. Atul Haribhau Kachare 5/23/2025
Assembly Line Scheduling

𝑓1 3 = min 𝑓1 2 + 𝑎13 , 𝑓2 2 + 𝑡23 + 𝑎13


𝑓1 3 = min 19 + 3, 24 + 2 + 3 = min 22, 29 = 22
𝑓2 3 = min 𝑓2 2 + 𝑎23 , 𝑓1 2 + 𝑡13 + 𝑎23
𝑓2 3 = min 24 + 1, 19 + 4 + 1 = min 25, 24 = 24

88 Mr. Atul Haribhau Kachare 5/23/2025


Assembly Line Scheduling

𝑓1 4 = min 𝑓1 3 + 𝑎14 , 𝑓2 3 + 𝑡24 + 𝑎14


𝑓1 4 = min 22 + 2, 24 + 8 + 2 = min 24, 34 = 24
𝑓2 4 = min 𝑓2 3 + 𝑎24 , 𝑓1 3 + 𝑡14 + 𝑎24
𝑓2 4 = min 24 + 4, 22 + 5 + 4 = min 28, 31 = 28
𝑓𝑜𝑝𝑡𝑖𝑚𝑎𝑙 = 𝑚𝑖𝑛 𝑓1 4 + 𝑥1 , 𝑓2 4 + 𝑥2
𝑓𝑜𝑝𝑡𝑖𝑚𝑎𝑙 = 𝑚𝑖𝑛 24 + 18, 28 + 7 = 𝑚𝑖𝑛 42, 35 = 35
PATH: 𝑒1 → 𝑎11 → 𝑎12 → 𝑎23 → 𝑎24 → 𝑥2
89 Mr. Atul Haribhau Kachare 5/23/2025
All Pair Shortest Path
B D
0 2 4 ∞ 0 2 4 ∞
𝐴0 = 2 0 1 5 𝐴𝐴 = 2 0 1 5
4 1 0 3 4 1 0 3
A C ∞ 5 3 0 ∞ 5 3 0
0 2 3 7 0 2 3 6
0 2 4 ∞ 𝐴𝐵 = 2 0 1 5 𝐴𝐶 = 2 0 1 4
3 1 0 3 3 1 0 3
2 0 1 5 7 5 3 0 6 4 3 0

4 1 0 3 0 2 3 6
𝐴𝐷 = 2 0 1 4
∞ 5 3 0 3 1 0 3
90 Mr. Atul Haribhau Kachare 6 4 3 0 5/23/2025
Assembly Line Scheduling

𝑓1 1 = 𝑒1 + 𝑎11 = 3 + 6 = 9
𝑓2 1 = 𝑒2 + 𝑎21 = 2 + 5 = 7
𝑓1 2 = min 𝑓1 1 + 𝑎12 , 𝑓2 1 + 𝑡22 + 𝑎12
𝑓1 2 = min 9 + 3, 7 + 2 + 3 = min 12, 12 = 12
𝑓2 2 = min 𝑓2 1 + 𝑎22 , 𝑓1 1 + 𝑡12 + 𝑎22
𝑓2 2 = min 7 + 8, 9 + 7 + 8 = min 15, 24 = 15
91 Mr. Atul Haribhau Kachare 5/23/2025
Assembly Line Scheduling

𝑓1 3 = min 𝑓1 2 + 𝑎13 , 𝑓2 2 + 𝑡23 + 𝑎13


𝑓1 3 = min 12 + 9, 15 + 7 + 9 = min 21, 31 = 21
𝑓2 3 = min 𝑓2 2 + 𝑎23 , 𝑓1 2 + 𝑡13 + 𝑎23
𝑓2 3 = min 15 + 2, 12 + 4 + 2 = min 17, 18 = 17
𝑓𝑜𝑝𝑡𝑖𝑚𝑎𝑙 = 𝑚𝑖𝑛 𝑓1 3 + 𝑥1 , 𝑓2 3 + 𝑥2
𝑓𝑜𝑝𝑡𝑖𝑚𝑎𝑙 = 𝑚𝑖𝑛 21 + 8, 17 + 5 = 22
PATH: 𝑒2 → 𝑎21 → 𝑎22 → 𝑎23 → 𝑥2
92 Mr. Atul Haribhau Kachare 5/23/2025
Example

Distance
K
0 1 2 3 4

1 0 ∞ ∞ 3 12

2 0 8 6 3 12

3 0 8 6 3 12

4 0 8 6 3 12

93 Mr. Atul Haribhau Kachare 5/23/2025

You might also like