Dynamic Programming Techniques Explained
Dynamic Programming Techniques Explained
➢ Dynamic programming is applicable when the subproblems are not independent, that is,
when subproblems share subsubproblems.
➢ A dynamic-programming algorithm solves every subsubproblem just once and then saves
its answer in a table, thereby avoiding the work of recomputing the answer every time the
subsubproblem is encountered
➢ Tabulation: Bottom Up
Fib(4)
Algorithm
Fib(3) Fib(2)
Fib(n)
If n == 0 || n == 1 return n;
Fib(2) Fib(1) Fib(1) Fib(0) else return Fib(n-1) + Fib(n-2);
Fib(1) Fib(0)
General Method
Tabulation: Bottom Up
➢ Using the subproblem result solve another subproblem and finally solve the whole problem.
General Method
Fib(4)
Fib(3) Fib(2)
Fib(1) Fib(0)
General Method
Fib(4)
Fib(3) Fib(2)
-1 -1 -1 -1 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(1) Fib(0)
General Method
Fib(4)
Fib(3) Fib(2)
-1 -1 -1 -1 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(1) Fib(0)
General Method
Fib(4)
Fib(3) Fib(2)
-1 -1 -1 -1 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(4)
Fib(3) Fib(2)
-1 -1 -1 -1 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(1) Fib(0)
General Method
Fib(4)
Fib(3) Fib(2)
-1 -1 -1 -1 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(4)
Fib(3) Fib(2)
-1 -1 -1 -1 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(1) Fib(0)
General Method
Fib(4)
Fib(3) Fib(2)
-1 -1 -1 -1 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(4)
Fib(3) Fib(2)
-1 -1 -1 -1 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(1) Fib(0)
General Method
Fib(4)
Fib(3) Fib(2)
-1 1 -1 -1 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(4)
Fib(3) Fib(2)
-1 1 -1 -1 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(1) Fib(0)
General Method
Fib(4)
Fib(3) Fib(2)
0 1 -1 -1 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(4)
Fib(3) Fib(2)
0 1 1 -1 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(4)
Fib(3) Fib(2)
0 1 1 -1 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(4)
Fib(3) Fib(2)
0 1 1 -1 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(4)
Fib(3) Fib(2)
0 1 1 -1 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(4)
Fib(3) Fib(2)
0 1 1 2 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(4)
Fib(3) Fib(2)
0 1 1 2 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(4)
Fib(3) Fib(2)
0 1 1 2 -1
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
Fib(4)
Fib(3) Fib(2)
0 1 1 2 3
Fib(2) Fib(1) Fib(1) Fib(0) 0 1 2 3 4
0 1 2 3 4
Algorithm
1. set Fib[0] = 0
Fib(4) 2. set Fib[1] = 1
3. From index 2 to n compute result using the below formula
Fib[index] = Fib[index - 1] + Fib[index - 2]
4. The final result will be stored in Fib[n].
General Method
0 1 2 3 4
Algorithm
1. set Fib[0] = 0
Fib(4) 2. set Fib[1] = 1
3. From index 2 to n compute result using the below formula
Fib[index] = Fib[index - 1] + Fib[index - 2]
4. The final result will be stored in Fib[n].
General Method
0 1
0 1 2 3 4
Algorithm
1. set Fib[0] = 0
Fib(4) 2. set Fib[1] = 1
3. From index 2 to n compute result using the below formula
Fib[index] = Fib[index - 1] + Fib[index - 2]
4. The final result will be stored in Fib[n].
General Method
0 1
0 1 2 3 4
Algorithm
1. set Fib[0] = 0
Fib(4) 2. set Fib[1] = 1
3. From index 2 to n compute result using the below formula
Fib[index] = Fib[index - 1] + Fib[index - 2]
4. The final result will be stored in Fib[n].
General Method
0 1
0 1 2 3 4
Algorithm
1. set Fib[0] = 0
Fib(4) 2. set Fib[1] = 1
3. From index 2 to n compute result using the below formula
Fib[index] = Fib[index - 1] + Fib[index - 2]
4. The final result will be stored in Fib[n].
General Method
0 1 1
0 1 2 3 4
Algorithm
1. set Fib[0] = 0
Fib(4) 2. set Fib[1] = 1
3. From index 2 to n compute result using the below formula
Fib[index] = Fib[index - 1] + Fib[index - 2]
4. The final result will be stored in Fib[n].
General Method
0 1 1
0 1 2 3 4
Algorithm
1. set Fib[0] = 0
Fib(4) 2. set Fib[1] = 1
3. From index 2 to n compute result using the below formula
Fib[index] = Fib[index - 1] + Fib[index - 2]
4. The final result will be stored in Fib[n].
General Method
0 1 1 2
0 1 2 3 4
Algorithm
1. set Fib[0] = 0
Fib(4) 2. set Fib[1] = 1
3. From index 2 to n compute result using the below formula
Fib[index] = Fib[index - 1] + Fib[index - 2]
4. The final result will be stored in Fib[n].
General Method
0 1 1 2
0 1 2 3 4
Algorithm
1. set Fib[0] = 0
Fib(4) 2. set Fib[1] = 1
3. From index 2 to n compute result using the below formula
Fib[index] = Fib[index - 1] + Fib[index - 2]
4. The final result will be stored in Fib[n].
General Method
0 1 1 2 3
0 1 2 3 4
Algorithm
1. set Fib[0] = 0
Fib(4) 2. set Fib[1] = 1
3. From index 2 to n compute result using the below formula
Fib[index] = Fib[index - 1] + Fib[index - 2]
4. The final result will be stored in Fib[n].
General Method
0 1 1 2 3
0 1 2 3 4
Algorithm
1. set Fib[0] = 0
Fib(4) = 3 2. set Fib[1] = 1
3. From index 2 to n compute result using the below formula
Fib[index] = Fib[index - 1] + Fib[index - 2]
4. The final result will be stored in Fib[n].
Topics to be covered
➢ 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
• It is a classic example of single source single destination shortest path problem
• It computes shortest path from a single source to single destination in a directed graph,
where nodes are partitioned into multiple stages
2
2
C E
5
Multistage Graphs : Forward Approach
2
B D
1 4 𝐜𝐨𝐬 𝐭 ⅈ, 𝐣 = 𝐦ⅈ𝐧ሼ𝒄 𝐣, 𝐥 + 𝐜𝐨𝐬 𝐭 ⅈ + 𝟏, 𝐥 }
A 3 Where l ϵ Vi+1 and (j, l) ϵ E
F
2 Stage
2
Number
C E
5
Multistage Graphs : Forward Approach
2
B D
1 4 𝐜𝐨𝐬 𝐭 ⅈ, 𝐣 = 𝐦ⅈ𝐧ሼ𝒄 𝐣, 𝐥 + 𝐜𝐨𝐬 𝐭 ⅈ + 𝟏, 𝐥 }
A 3 Where l ϵ Vi+1 and (j, l) ϵ E
F
2 Vertex
2
C E Number
5
Multistage Graphs : Forward Approach
2
B D
1 4 𝐜𝐨𝐬 𝐭 ⅈ, 𝐣 = 𝐦ⅈ𝐧ሼ𝒄 𝐣, 𝐥 + 𝐜𝐨𝐬 𝐭 ⅈ + 𝟏, 𝐥 }
A 3 Where l ϵ Vi+1 and (j, l) ϵ E
F
2
2
C E
5
Vertex
Cost
d (target Vertex)
Multistage Graphs : Forward Approach
2
B D
1 4
A 3
F
2
2
C E
5
Vertex A B C D E F
Cost
d (target Vertex)
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
A 3
F
2
2
C E
5
Vertex A B C D E F
Cost
d (target Vertex)
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost
d (target Vertex)
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost
d (target Vertex)
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Vertex A B C D E F
Cost
d (target Vertex)
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Cost(4, F)
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost
d (target Vertex)
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Cost(4, F)
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost
d (target Vertex)
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Cost(4, F)
s A 3 t
F
2 Stage Number
2
C E
5
Vertex A B C D E F
Cost
d (target Vertex)
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Cost(4, F)
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost
d (target Vertex)
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Cost(4, F)
s A 3 t
F
2 Vertex
2
C E
5
Vertex A B C D E F
Cost
d (target Vertex)
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Cost(4, F) = 0
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost 0
d (target Vertex)
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Cost(4, F) = 0
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost 0
d (target Vertex) F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost 0
d (target Vertex) F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Vertex A B C D E F
Cost 0
d (target Vertex) F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 Cost(3, D) =
F t
2
2
C E
5
Vertex A B C D E F
Cost 0
d (target Vertex) F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 Cost(3, D)
F t
Vertex A B C D E F
Cost 0
d (target Vertex) F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 0
d (target Vertex) F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 0
d (target Vertex) F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 0
d (target Vertex) F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 0
d (target Vertex) F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 0
d (target Vertex) F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 0
d (target Vertex) F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 Cost(3, D)=min{4 + 0}
F t
2
2
C E
5
Vertex A B C D E F
Cost 0
d (target Vertex) F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 Cost(3, D)=min{4 + 0} = 4
F t
2
2
C E
5
Vertex A B C D E F
Cost 0
d (target Vertex) F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 Cost(3, D) = 4
F t
2
2
C E
5
Vertex A B C D E F
Cost 0
d (target Vertex) F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 Cost(3, D) = 4
F t
2
2
C E
5
Vertex A B C D E F
Cost 4 0
d (target Vertex) F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 Cost(3, D) = 4
F t
2
2
C E
5
Vertex A B C D E F
Cost 4 0
d (target Vertex) F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 Cost(3, E) = ?
F t
2
2
C E
5
Vertex A B C D E F
Cost 4 0
d (target Vertex) F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 0
d (target Vertex) F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 Cost(3, E) = 2
F t
2
2
C E
5
Vertex A B C D E F
Cost 4 0
d (target Vertex) F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 Cost(3, E) = 2
F t
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 Cost(3, E) = 2
F t
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Now we have go to stage 2
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Now we have go to stage 2
s A 3 t
F
Stage 2 has 2 vertices B, C
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Cost(2, B) = ?
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Cost(2, B) = ?
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Cost(2, B) = ?
s A 3 t
F
From B vertex there are two
2 paths to reach to Stage 3. So we
2
C E have to select minimum
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 Cost(2, B) = min{2+ 4 , 3+ 2}
F t
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Vertex A B C D E F
Cost 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Vertex A B C D E F
Cost 5 4 2 0
d (target Vertex) F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Vertex A B C D E F
Cost 5 4 2 0
d (target Vertex) E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 Cost(2, C) = ?
F t
2
2
C E
5
Vertex A B C D E F
Cost 5 4 2 0
d (target Vertex) E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 5 4 2 0
d (target Vertex) E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Vertex A B C D E F
Cost 5 4 2 0
d (target Vertex) E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 Cost(2, C) = 7
F t
2
2
C E
5
Vertex A B C D E F
Cost 5 4 2 0
d (target Vertex) E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 Cost(2, C) = 7
F t
2
2
C E
5
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
s A 3 Cost(1, A) = min{1+ 5 , 2+ 5}
F t
2
2
C E
5
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D
1 4
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) =
1 4
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) =
1 4
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) =
1 4
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4
d(2, B) =
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4
d(2, B) =
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4
d(2, B) =
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4
d(2, B) = E
s A 3 t
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4
d(2, B) = E
s A 3 t d(3, E) =
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4
d(2, B) = E
s A 3 t d(3, E) =
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4
d(2, B) = E
s A 3 t d(3, E) =
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4
d(2, B) = E
s A 3 t d(3, E) = F
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4
d(2, B) = E
s A 3 t d(3, E) = F
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4 Path is
d(2, B) = E
s A 3 t d(3, E) = F
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4 Path is
d(2, B) = E
s A 3 t d(3, E) = F
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4 Path is
d(2, B) = E
s A 3 t d(3, E) = F
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4 Path is
d(2, B) = E
s A 3 t d(3, E) = F
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4 Path is
d(2, B) = E
s A 3 t d(3, E) = F
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4 Path is
d(2, B) = E
s A 3 t d(3, E) = F
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4 Path is
d(2, B) = E
s A 3 t d(3, E) = F
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4 Path is
d(2, B) = E
s A 3 t d(3, E) = F
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Forward Approach
V1 V2 V3 V4
2
B D d(1, A) = B
1 4 Path is :
d(2, B) = E
A→B→E→F
s A 3 t d(3, E) = F
F
2
2
C E
5
Vertex A B C D E F
Cost 6 5 7 4 2 0
d (target Vertex) B E E F F F
Multistage Graphs : Backward Approach
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2
C E
5
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2
C E
5
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2
C E
5
Vertex A B C D E F
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2
C E
5
Vertex A B C D E F
Cost
d
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2
C E Starting with Stage-1
5
Cost(1, A) = 0
Vertex A B C D E F
Cost
d
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2
C E Starting with Stage-1
5
Cost(1, A) = 0
Vertex A B C D E F
Cost 0
d
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2
C E Starting with Stage-1
5
Cost(1, A) = 0
Vertex A B C D E F
Cost 0
d A
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2
C E Stage-2
5 Cost(2, B) = min{cost(1,A) + c(A, B)
Vertex A B C D E F
Cost 0
d A
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-2
C E Cost(2, B) = min{cost(1,A) + c(A, B) }
5
Cost(2, B) = min{0+1} = 1
Vertex A B C D E F
Cost 0
d A
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-2
C E Cost(2, B) = min{cost(1,A) + c(A, B) }
5
Cost(2, B) = min{0+1} = 1
Vertex A B C D E F
Cost 0 1
d A
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-2
C E Cost(2, B) = min{cost(1,A) + c(A, B) }
5
Cost(2, B) = min{0+1} = 1
Vertex A B C D E F
Cost 0 1
d A A
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-2
C E Cost(2, C) = min{cost(1,A) + c(A, C)}
5
Cost(2, C) = min{0+2} = 2
Vertex A B C D E F
Cost 0 1
d A A
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2
C E Stage-2
5 Cost(2, C) = min{cost(1,A) + c(A, C) }
Cost(2, C) = min{0+2} = 2
Vertex A B C D E F
Cost 0 1 2
d A A
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-2
C E Cost(2, C) = min{cost(1,A) + c(A, C)}
5
Cost(2, C) = min{0+2} = 2
Vertex A B C D E F
Cost 0 1 2
d A A A
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-3
C E Cost(3, D) = min{cost(2,B) + c(B, D)}
5
Cost(3, D) = min{1+2 } = 3
Vertex A B C D E F
Cost 0 1 2
d A A A
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-3
C E Cost(3, D) = min{cost(2,B) + c(B, D)}
5
Cost(3, D) = min{1+2} = 3
Vertex A B C D E F
Cost 0 1 2 3
d A A A
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-3
C E Cost(3, D) = min{cost(2,B) + c(B, D)}
5
Cost(3, D) = min{1+2} = 3
Vertex A B C D E F
Cost 0 1 2 3
d A A A B
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-3
C E Cost(3, E) = min{cost(2,B) + c(B, E) , cost(2,C) + c(C, E) }
5
Vertex A B C D E F
Cost 0 1 2 3
d A A A B
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-3
C E Cost(3, E) = min{cost(2,B) + c(B, E) , cost(2,C) + c(C, E) }
5
Cost(3, E) = {1 + 3 , 2 + 5}
Vertex A B C D E F
Cost 0 1 2 3
d A A A B
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-3
C E Cost(3, E) = min{cost(2,B) + c(B, E) , cost(2,C) + c(C, E) }
5
Cost(3, E) = {1 + 3 , 2 + 5} = 4
Vertex A B C D E F
Cost 0 1 2 3
d A A A B
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-3
C E Cost(3, E) = min{cost(2,B) + c(B, E) , cost(2,C) + c(C, E) }
5
Cost(3, E) = {1 + 3 , 2 + 5} = 4
Vertex A B C D E F
Cost 0 1 2 3 4
d A A A B
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-3
C E Cost(3, E) = min{cost(2,B) + c(B, E) , cost(2,C) + c(C, E) }
5
Cost(3, E) = {1 + 3 , 2 + 5} = 4
Vertex A B C D E F
Cost 0 1 2 3 4
d A A A B B
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-4
C E
5
Vertex A B C D E F
Cost 0 1 2 3 4
d A A A B B
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-4
C E Cost(4, F) = min{cost(3, D) + c(D, F) , cost(3, E) + c(E, F) }
5
Vertex A B C D E F
Cost 0 1 2 3 4
d A A A B B
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-4
C E Cost(4, F) = min{cost(3, D) + c(D, F) , cost(3, E) + c(E, F) }
5
Cost(4, F) = min(3 + 4 , 4 + 2}
Vertex A B C D E F
Cost 0 1 2 3 4
d A A A B B
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-4
C E Cost(4, F) = min{cost(3, D) + c(D, F) , cost(3, E) + c(E, F) }
5
Cost(4, F) = min(3 + 4 , 4 + 2} = 6
Vertex A B C D E F
Cost 0 1 2 3 4
d A A A B B
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-4
C E Cost(4, F) = min{cost(3, D) + c(D, F) , cost(3, E) + c(E, F) }
5
Cost(4, F) = min(3 + 4 , 4 + 2} = 6
Vertex A B C D E F
Cost 0 1 2 3 4 6
d A A A B B
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1
3
F (l,j)ϵE
2
2 Stage-4
C E Cost(4, F) = min{cost(3, D) + c(D, F) , cost(3, E) + c(E, F) }
5
Cost(4, F) = min(3 + 4 , 4 + 2} = 6
Vertex A B C D E F
Cost 0 1 2 3 4 6
d A A A B B E
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1 and (l,j)ϵE
3
F
2 d(4, F) =
2
C E
5
Vertex A B C D E F
Cost 0 1 2 3 4 6
d A A A B B E
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1 and (l,j)ϵE
3
F
2 d(4, F) =
2
C E
5
Vertex A B C D E F
Cost 0 1 2 3 4 6
d A A A B B E
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1 and (l,j)ϵE
3
F
2 d(4, F) = E
2
C E
5
Vertex A B C D E F
Cost 0 1 2 3 4 6
d A A A B B E
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1 and (l,j)ϵE
3
F
2 d(4, F) = E
2
C E d(3, E) = B
5
Vertex A B C D E F
Cost 0 1 2 3 4 6
d A A A B B E
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1 and (l,j)ϵE
3
F
2 d(4, F) = E
2
C E d(3, E) = B
5
Vertex A B C D E F
Cost 0 1 2 3 4 6
d A A A B B E
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1 and (l,j)ϵE
3
F
2 d(4, F) = E
2
C E d(3, E) = B
5 d(2, B) = A
Vertex A B C D E F
Cost 0 1 2 3 4 6
d A A A B B E
Multistage Graphs : Backward Approach
V1 V2 V3 V4
2
B D
1 4 BCOST(i, j) = min { BCOST(i - 1, l) + c(l, j)}
A
l ϵVi- 1 and (l,j)ϵE
3
F
2 d(4, F) = E
2
C E d(3, E) = B
5 d(2, B) = A
Vertex A B C D E F
Cost 0 1 2 3 4 6
d A A A B B E
Multistage Graphs : Forward Approach Algorithm
procedure FGRAPH(E, k, n, P)
/* The input is a k stage graph with n vertices indexed in order of stages. E is a set of edges and
c(i, j) is the cost of (i, j) P(l:k) is a minimum cost path */
/* The input is a k stage graph with n vertices indexed in order of stages. E is a set of edges and
c(i, j) is the cost of (i, j) P(l:k) is a minimum cost path */
If G is represented by its adjacency lists, then r in line 4 may be found in time proportional to the degree
of vertex j. Hence, if G has E edges then the time for the for loop of lines 3 to 7 is O(V +E).
Multistage Graphs : Time Complexity
procedure FGRAPH(E, k, n, P)
/* The input is a k stage graph with n vertices indexed in order of stages. E is a set of edges and
c(i, j) is the cost of (i, j) P(l:k) is a minimum cost path */
/* The input is a k stage graph with n vertices indexed in order of stages. E is a set of edges and
c(i, j) is the cost of (i, j) P(l:k) is a minimum cost path */
/* The input is a k stage graph with n vertices indexed in order of stages. E is a set of edges and
c(i, j) is the cost of (i, j) P(l:k) is a minimum cost path */
/* The input is a k stage graph with n vertices indexed in order of stages. E is a set of edges and
c(i, j) is the cost of (i, j) P(l:k) is a minimum cost path */
/* The input is a k stage graph with n vertices indexed in order of stages. E is a set of edges and
c(i, j) is the cost of (i, j) P(l:k) is a minimum cost path */
/* The input is a k stage graph with n vertices indexed in order of stages. E is a set of edges and
c(i, j) is the cost of (i, j) P(l:k) is a minimum cost path */
/* The input is a k stage graph with n vertices indexed in order of stages. E is a set of edges and
c(i, j) is the cost of (i, j) P(l:k) is a minimum cost path */
/* The input is a k stage graph with n vertices indexed in order of stages. E is a set of edges and
c(i, j) is the cost of (i, j) P(l:k) is a minimum cost path */
In addition to the space needed for the input, space is needed for COST, D and P which is O(V).
Multistage Graphs : Backward Approach Algorithm
procedure BGRAPH(E, k, n, P)
/* The input is a k stage graph with n vertices indexed in order of stages. E is a set of edges and
c(i, j) is the cost of (i, j) P(l:k) is a minimum cost path */
• A single source shortest path problem is problem of finding shortest path from source node to all
other nodes in a weighted connected graph G = (V, E)
• The Bellman-Ford algorithm solves the single-source shortest-paths problem in the general case
in which edge weights may be negative.
• Given a weighted, directed graph G = (V, E) with source s and weight function w : E → R, the
Bellman-Ford algorithm returns a Boolean value indicating whether or not there is a negative-
weight cycle that is reachable from the source.
• If there is such a cycle, the algorithm indicates that no solution exists. If there is no such cycle, the
algorithm produces the shortest paths and their weights.
Single Source Shortest Path: Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G, s)
2. for i ← 1 to |V[G]| - 1
3. do for each edge (u, v) ϵ E[G]
4. do RELAX(u, v, w)
5. for each edge (u, v) ϵ E[G]
6. do if d[v] > d[u] + w(u, v)
7. then return FALSE
8. return TRUE
Single Source Shortest Path: Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ INITALISE-SINGLE-SOURCE (G,s)
3. π[v] ← NIL
4. d[s] = 0
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G]
7. do RELAX(u, v, w)
8. for each edge (u, v) ϵ E[G]
9. do if d[v] > d[u] + w(u, v)
10. then return FALSE
11. return TRUE
Single Source Shortest Path: Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞
3. π[v] ← NIL INITALISE-SINGLE-SOURCE (G,s)
4. d[s] = 0
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G]
7. if d[v] > d[u] + w(u, v)
8. then d[v] ← d[u] + w(u, v) RELAX (u, v, w)
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE
13. return TRUE
Single Source Shortest Path: Bellman-Ford Algorithm
-1
B E
BELLMAN-FORD(G, w, s)
1. for each vertex v ϵ V[G] 6 -2 1 3
2. do d[v] ← ∞
3. π[v] ← NIL 5
A C G
4. d[s] = 0
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v)
D F
8. then d[v] ← d[u] + w(u, v) -1
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE
13. return TRUE
Single Source Shortest Path: Bellman-Ford Algorithm
-1
BELLMAN-FORD(G, w, s) B E
1. for each vertex v ϵ V[G] 6 -2
2. do d[v] ← ∞ 1 3
3. π[v] ← NIL 5
4. d[s] = 0 A C G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v)
8. then d[v] ← d[u] + w(u, v) D F
-1
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE
13. return TRUE
Single Source Shortest Path: Bellman-Ford Algorithm
-1
BELLMAN-FORD(G, w, s) B E
1. for each vertex v ϵ V[G] 6 -2
2. do d[v] ← ∞ 1 3
3. π[v] ← NIL 5
4. d[s] = 0 A C G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v)
8. then d[v] ← d[u] + w(u, v) D F
-1
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE V A B C D E F G
13. return TRUE
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ ꝏ
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ
4. d[s] = 0 5
A C ꝏ G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G]
7. if d[v] > d[u] + w(u, v) 5 -2 3
8. then d[v] ← d[u] + w(u, v)
D F
9. π[v] ← u -1
10. for each edge (u, v) ϵ E[G] ꝏ ꝏ
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE V A B C D E F G
13. return TRUE
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ ꝏ
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ
5
4. d[s] = 0 A C ꝏ G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v) V A B C D E F G
12. then return FALSE
d[v] ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ
13. return TRUE
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ ꝏ
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ
5
4. d[s] = 0 A C ꝏ G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v) V A B C D E F G
12. then return FALSE
d[v] ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ
13. return TRUE
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ ꝏ
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ
5
4. d[s] = 0 A C ꝏ G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v) V A B C D E F G
12. then return FALSE
d[v] ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ
13. return TRUE
π[v] Nil Nil Nil Nil Nil Nil Nil
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ ꝏ
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ
5
4. d[s] = 0 A C ꝏ G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v) V A B C D E F G
12. then return FALSE
d[v] ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ
13. return TRUE
π[v] Nil Nil Nil Nil Nil Nil Nil
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ ꝏ
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ
5
4. d[s] = 0 A C ꝏ G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v) V A B C D E F G
12. then return FALSE
d[v] ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ
13. return TRUE
π[v] Nil Nil Nil Nil Nil Nil Nil
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ ꝏ
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v) V A B C D E F G
12. then return FALSE
d[v] ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ
13. return TRUE
π[v] Nil Nil Nil Nil Nil Nil Nil
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ ꝏ
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v) V A B C D E F G
12. then return FALSE
d[v] ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ
13. return TRUE
π[v] Nil Nil Nil Nil Nil Nil Nil
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ ꝏ
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v) V A B C D E F G
12. then return FALSE
d[v] 0 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ
13. return TRUE
π[v] Nil Nil Nil Nil Nil Nil Nil
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ ꝏ
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v) V A B C D E F G
12. then return FALSE
d[v] 0 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ
13. return TRUE
π[v] Nil Nil Nil Nil Nil Nil Nil
Vertex(G) = 7
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ ꝏ
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v) V A B C D E F G
12. then return FALSE
d[v] 0 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ
13. return TRUE
π[v] Nil Nil Nil Nil Nil Nil Nil
Vertex(G) = 7 i=1
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ ꝏ
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v) V A B C D E F G
12. then return FALSE
d[v] 0 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ
13. return TRUE
π[v] Nil Nil Nil Nil Nil Nil Nil
Vertex(G) = 7 i=1
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ ꝏ
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v) V A B C D E F G
12. then return FALSE
d[v] 0 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ
13. return TRUE
π[v] Nil Nil Nil Nil Nil Nil Nil
Vertex(G) = 7 i=1
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ ꝏ
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 ꝏ ꝏ ꝏ ꝏ ꝏ ꝏ
13. return TRUE π[v] Nil Nil Nil Nil Nil Nil Nil
Vertex(G) = 7 i=1 (u, v) = (B, E) d(E) > d(B) + w(u, v) = ꝏ > 6+ (-1) = ꝏ > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 ꝏ
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 6 5 5 ꝏ ꝏ ꝏ
13. return TRUE π[v] Nil A A A Nil Nil Nil
Vertex(G) = 7 i=1 (u, v) = (B, E) d(E) > d(B) + w(u, v) = ꝏ > 6+ (-1) = ꝏ > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 ꝏ
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 6 5 5 ꝏ ꝏ ꝏ
13. return TRUE π[v] Nil A A A Nil Nil Nil
Vertex(G) = 7 i=1 (u, v) = (B, E) d(E) > d(B) + w(u, v) = ꝏ > 6+ (-1) = ꝏ > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 6 5 5 ꝏ ꝏ ꝏ
13. return TRUE π[v] Nil A A A Nil Nil Nil
Vertex(G) = 7 i=1 (u, v) = (B, E) d(E) > d(B) + w(u, v) = ꝏ > 6+ (-1) = ꝏ > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 6 5 5 ꝏ ꝏ ꝏ
13. return TRUE π[v] Nil A A A Nil Nil Nil
Vertex(G) = 7 i=1 (u, v) = (B, E) d(E) > d(B) + w(u, v) = ꝏ > 6+ (-1) = ꝏ > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 6 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil A A A Nil Nil Nil
Vertex(G) = 7 i=1 (u, v) = (B, E) d(E) > d(B) + w(u, v) = ꝏ > 6+ (-1) = ꝏ > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 6 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil A A A Nil Nil Nil
Vertex(G) = 7 i=1 (u, v) = (B, E) d(E) > d(B) + w(u, v) = ꝏ > 6+ (-1) = ꝏ > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 6 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil A A A Nil Nil Nil
Vertex(G) = 7 i=1 (u, v) = (B, E) d(E) > d(B) + w(u, v) = ꝏ > 6+ (-1) = ꝏ > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 6 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil A A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (B, E) d(E) > d(B) + w(u, v) = ꝏ > 6+ (-1) = ꝏ > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 6 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil A A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (C, B) d(B) > d(C) + w(u, v) = 6 > 5 + (-2) = 6 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 6 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil A A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (C, B) d(B) > d(C) + w(u, v) = 6 > 5 + (-2) = 6 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 6 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil A A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (C, B) d(B) > d(C) + w(u, v) = 6 > 5 + (-2) = 6 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 6 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil A A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (C, B) d(B) > d(C) + w(u, v) = 6 > 5 + (-2) = 6 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 6 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil A A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (C, B) d(B) > d(C) + w(u, v) = 6 > 5 + (-2) = 6 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil A A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (C, B) d(B) > d(C) + w(u, v) = 6 > 5 + (-2) = 6 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil A A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (C, B) d(B) > d(C) + w(u, v) = 6 > 5 + (-2) = 6 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil A A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (C, B) d(B) > d(C) + w(u, v) = 6 > 5 + (-2) = 6 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil C A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (C, B) d(B) > d(C) + w(u, v) = 6 > 5 + (-2) = 6 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil C A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (D, C) d(C) > d(D) + w(u, v) = 5 > 5+(-2) = 5 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ5 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil C A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (D, C) d(C) > d(D) + w(u, v) = 5 > 5+(-2) = 5 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil C A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (D, C) d(C) > d(D) + w(u, v) = 5 > 5+(-2) = 5 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 5 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil C A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (D, C) d(C) > d(D) + w(u, v) = 5 > 5+(-2) = 5 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil C A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (D, C) d(C) > d(D) + w(u, v) = 5 > 5+(-2) = 5 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil C A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (D, C) d(C) > d(D) + w(u, v) = 5 > 5+(-2) = 5 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil C A A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (D, C) d(C) > d(D) + w(u, v) = 5 > 5+(-2) = 5 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil C D A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (D, C) d(C) > d(D) + w(u, v) = 5 > 5+(-2) = 5 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil C D A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (D, F) d(F) > d(D) + w(u, v) = ꝏ > 5 + (-1) = ꝏ > 4
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil C D A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (D, F) d(F) > d(D) + w(u, v) = ꝏ > 5 + (-1) = ꝏ > 4
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil C D A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (D, F) d(F) > d(D) + w(u, v) = ꝏ > 5 + (-1) = ꝏ > 4
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil C D A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (D, F) d(F) > d(D) + w(u, v) = ꝏ > 5 + (-1) = ꝏ > 4
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 ꝏ ꝏ
13. return TRUE π[v] Nil C D A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (D, F) d(F) > d(D) + w(u, v) = ꝏ > 5 + (-1) = ꝏ > 4
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 ꝏ
13. return TRUE π[v] Nil C D A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (D, F) d(F) > d(D) + w(u, v) = ꝏ > 5 + (-1) = ꝏ > 4
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 ꝏ
13. return TRUE π[v] Nil C D A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (D, F) d(F) > d(D) + w(u, v) = ꝏ > 5 + (-1) = ꝏ > 4
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 ꝏ
13. return TRUE π[v] Nil C D A B Nil Nil
Vertex(G) = 7 i=1 (u, v) = (D, F) d(F) > d(D) + w(u, v) = ꝏ > 5 + (-1) = ꝏ > 4
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 ꝏ
13. return TRUE π[v] Nil C D A B D Nil
Vertex(G) = 7 i=1 (u, v) = (D, F) d(F) > d(D) + w(u, v) = ꝏ > 5 + (-1) = ꝏ > 4
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 ꝏ
13. return TRUE π[v] Nil C D A B D Nil
Vertex(G) = 7 i=1 (u, v) = (E, G) d(G) > d(E) + w(u, v) = ꝏ > 5 + 3 = ꝏ > 8
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 ꝏ
13. return TRUE π[v] Nil C D A B D Nil
Vertex(G) = 7 i=1 (u, v) = (E, G) d(G) > d(E) + w(u, v) = ꝏ > 5 + 3 = ꝏ > 8
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5
4. d[s] = 0 A C ꝏ53 G ꝏ
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 ꝏ
13. return TRUE π[v] Nil C D A B D Nil
Vertex(G) = 7 i=1 (u, v) = (E, G) d(G) > d(E) + w(u, v) = ꝏ > 5 + 3 = ꝏ > 8
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 ꝏ
13. return TRUE π[v] Nil C D A B D Nil
Vertex(G) = 7 i=1 (u, v) = (E, G) d(G) > d(E) + w(u, v) = ꝏ > 5 + 3 = ꝏ > 8
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 ꝏ
13. return TRUE π[v] Nil C D A B D Nil
Vertex(G) = 7 i=1 (u, v) = (E, G) d(G) > d(E) + w(u, v) = ꝏ > 5 + 3 = ꝏ > 8
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 8
13. return TRUE π[v] Nil C D A B D Nil
Vertex(G) = 7 i=1 (u, v) = (E, G) d(G) > d(E) + w(u, v) = ꝏ > 5 + 3 = ꝏ > 8
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 8
13. return TRUE π[v] Nil C D A B D Nil
Vertex(G) = 7 i=1 (u, v) = (E, G) d(G) > d(E) + w(u, v) = ꝏ > 5 + 3 = ꝏ > 8
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 8
13. return TRUE π[v] Nil C D A B D Nil
Vertex(G) = 7 i=1 (u, v) = (E, G) d(G) > d(E) + w(u, v) = ꝏ > 5 + 3 = ꝏ > 8
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 8
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=1 (u, v) = (E, G) d(G) > d(E) + w(u, v) = ꝏ > 5 + 3 = ꝏ > 8
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 8
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=1 (u, v) = (F, G) d(G) > d(F) + w(u, v) = 8 > 4 + 3 = 8 > 7
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 8
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=1 (u, v) = (F, G) d(G) > d(F) + w(u, v) = 8 > 4 + 3 = 8 > 7
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 8
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=1 (u, v) = (F, G) d(G) > d(F) + w(u, v) = 8 > 4 + 3 = 8 > 7
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 8
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=1 (u, v) = (F, G) d(G) > d(F) + w(u, v) = 8 > 4 + 3 = 8 > 7
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 8
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=1 (u, v) = (F, G) d(G) > d(F) + w(u, v) = 8 > 4 + 3 = 8 > 7
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 7
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=1 (u, v) = (F, G) d(G) > d(F) + w(u, v) = 8 > 4 + 3 = 8 > 7
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 7
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=1 (u, v) = (F, G) d(G) > d(F) + w(u, v) = 8 > 4 + 3 = 8 > 7
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 7
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=1 (u, v) = (F, G) d(G) > d(F) + w(u, v) = 8 > 4 + 3 = 8 > 7
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=1 (u, v) = (F, G) d(G) > d(F) + w(u, v) = 8 > 4 + 3 = 8 > 7
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (B,E) d(E) > d(B) + w(u, v) = 5 > 3 + (-1) = 5 > 2
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (B,E) d(E) > d(B) + w(u, v) = 5 > 3 + (-1) = 5 > 2
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (B,E) d(E) > d(B) + w(u, v) = 5 > 3 + (-1) = 5 > 2
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (B,E) d(E) > d(B) + w(u, v) = 5 > 3 + (-1) = 5 > 2
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 5 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (B,E) d(E) > d(B) + w(u, v) = 5 > 3 + (-1) = 5 > 2
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (B,E) d(E) > d(B) + w(u, v) = 5 > 3 + (-1) = 5 > 2
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (B,E) d(E) > d(B) + w(u, v) = 5 > 3 + (-1) = 5 > 2
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (B,E) d(E) > d(B) + w(u, v) = 5 > 3 + (-1) = 5 > 2
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (B,E) d(E) > d(B) + w(u, v) = 5 > 3 + (-1) = 5 > 2
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (C,B) d(B) > d(C) + w(u, v) = 3 > 3 + (-2) = 3 > 1
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (C,B) d(B) > d(C) + w(u, v) = 3 > 3 + (-2) = 3 > 1
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (C,B) d(B) > d(C) + w(u, v) = 3 > 3 + (-2) = 3 > 1
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (C,B) d(B) > d(C) + w(u, v) = 3 > 3 + (-2) = 3 > 1
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 3 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (C,B) d(B) > d(C) + w(u, v) = 3 > 3 + (-2) = 3 > 1
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (C,B) d(B) > d(C) + w(u, v) = 3 > 3 + (-2) = 3 > 1
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (C,B) d(B) > d(C) + w(u, v) = 3 > 3 + (-2) = 3 > 1
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (C,B) d(B) > d(C) + w(u, v) = 3 > 3 + (-2) = 3 > 1
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (C,B) d(B) > d(C) + w(u, v) = 3 > 3 + (-2) = 3 > 1
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (C, E) d(E) > d(C) + w(u, v) = 2 > 3 + 1 = 2 > 4
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (D, E) d(C) > d(D) + w(u, v) = 3 > 5 + (-2) = 3 ≯ 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (D, F ) d(F) > d(D) + w(u, v) = 4 > 5 + (-1) = 4 ≯ 4
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (E, G) d(G) > d(E) + w(u, v) = 7 > 2+3 = 7 > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (E, G) d(G) > d(E) + w(u, v) = 7 > 2+3 = 7 > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (E, G) d(G) > d(E) + w(u, v) = 7 > 2+3 = 7 > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 7
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (E, G) d(G) > d(E) + w(u, v) = 7 > 2+3 = 7 > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 5
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (E, G) d(G) > d(E) + w(u, v) = 7 > 2+3 = 7 > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 5
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (E, G) d(G) > d(E) + w(u, v) = 7 > 2+3 = 7 > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 5
13. return TRUE π[v] Nil C D A B D F
Vertex(G) = 7 i=2 (u, v) = (E, G) d(G) > d(E) + w(u, v) = 7 > 2+3 = 7 > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 5
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=2 (u, v) = (E, G) d(G) > d(E) + w(u, v) = 7 > 2+3 = 7 > 5
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 5
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (B, E) d(E) > d(B) + w(u, v) = 2 > 1 + (-1) = 2 > 0
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 5
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (B, E) d(E) > d(B) + w(u, v) = 2 > 1 + (-1) = 2 > 0
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 5
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (B, E) d(E) > d(B) + w(u, v) = 2 > 1 + (-1) = 2 > 0
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 2 4 5
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (B, E) d(E) > d(B) + w(u, v) = 2 > 1 + (-1) = 2 > 0
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 5
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (B, E) d(E) > d(B) + w(u, v) = 2 > 1 + (-1) = 2 > 0
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 5
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (B, E) d(E) > d(B) + w(u, v) = 2 > 1 + (-1) = 2 > 0
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 5
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (B, E) d(E) > d(B) + w(u, v) = 2 > 1 + (-1) = 2 > 0
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 5
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (C,B) d(B) > d(C) + w(u, v) = 1 > 3 + (-2) = 1 ≯ 1
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 5
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (D, C) d(C) > d(D) + w(u, v) = 3 > 5 + (-2) = 3 ≯ 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 5
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (D,F ) d(F) > d(D) + w(u, v) = 4 > 5 + (-1) = 4 ≯ 4
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 5
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (E, G) d(G) > d(E) + w(u, v) = 5 > 0 + 3 = 5 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 5
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (E, G) d(G) > d(E) + w(u, v) = 5 > 0 + 3 = 5 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 5
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (E, G) d(G) > d(E) + w(u, v) = 5 > 0 + 3 = 5 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5 3
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 5
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (E, G) d(G) > d(E) + w(u, v) = 5 > 0 + 3 = 5 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5 3
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 5
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (E, G) d(G) > d(E) + w(u, v) = 5 > 0 + 3 = 5 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5 3
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 3
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (E, G) d(G) > d(E) + w(u, v) = 5 > 0 + 3 = 5 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5 3
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 3
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (E, G) d(G) > d(E) + w(u, v) = 5 > 0 + 3 = 5 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5 3
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 3
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=3 (u, v) = (E, G) d(G) > d(E) + w(u, v) = 5 > 0 + 3 = 5 > 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5 3
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 3
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=4 (u, v) = (B, E) d(E) > d(B) + w(u, v) = 0 > 1 + (-1) = 0 ≯ 0
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5 3
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 3
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=4 (u, v) = (C,B) d(B) > d(C) + w(u, v) =1 > 3 + (-2) = 1 ≯ 1
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5 3
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 3
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=4 (u, v) = (D, C) d(C) > d(D) + w(u, v) = 3 > 5 + (-2) = 3 ≯ 3
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5 3
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 3
13. return TRUE π[v] Nil C D A B D E
Vertex(G) = 7 i=4 (u, v) = (D, F) d(F) > d(D) + w(u, v) = 4 > 5 + (-1) = 4 ≯ 4
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
BELLMAN-FORD(G, w, s) -1
B E
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ 6 -2 1 3
3. π[v] ← NIL ꝏ 0
5 ꝏ8 7
4. d[s] = 0 A C ꝏ53 G
5. for i ← 1 to |V[G]| - 1 5 3
6. do for each edge (u, v) ϵ E[G] 5 -2 3
7. if d[v] > d[u] + w(u, v) D F
8. then d[v] ← d[u] + w(u, v) -1
ꝏ5 ꝏ 4
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
V A B C D E F G
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE d[v] 0 1 3 5 0 4 3
13. return TRUE π[v] Nil C D A B D E
5 3
5 -2 3
D F
-1
ꝏ5 ꝏ 4
V A B C D E F G
d[v] 0 1 3 5 0 4 3
π[v] Nil C D A B D E
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
-1
B E
Pair Paths Shortest Distance
6 -2 1 3
ꝏ 0
A→B
5 ꝏ8 7
A C ꝏ53 G
5 3
5 -2 3
D F
-1
ꝏ5 ꝏ 4
V A B C D E F G
d[v] 0 1 3 5 0 4 3
π[v] Nil C D A B D E
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
-1
B E
Pair Paths Shortest Distance
6 -2 1 3
ꝏ 0
A→B A→
5 ꝏ8 7
A C ꝏ53 G
5 3
5 -2 3
D F
-1
ꝏ5 ꝏ 4
V A B C D E F G
d[v] 0 1 3 5 0 4 3
π[v] Nil C D A B D E
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
-1
B E
Pair Paths Shortest Distance
6 -2 1 3
ꝏ 0
A→B A→D
5 ꝏ8 7
A C ꝏ53 G
5 3
5 -2 3
D F
-1
ꝏ5 ꝏ 4
V A B C D E F G
d[v] 0 1 3 5 0 4 3
π[v] Nil C D A B D E
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
-1
B E
Pair Paths Shortest Distance
6 -2 1 3
ꝏ 0
A→B A→D
5 ꝏ8 7
A C ꝏ53 G
5 3
5 -2 3
D F
-1
ꝏ5 ꝏ 4
V A B C D E F G
d[v] 0 1 3 5 0 4 3
π[v] Nil C D A B D E
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
-1
B E
Pair Paths Shortest Distance
6 -2 1 3
ꝏ 0
A→B A→D→C
5 ꝏ8 7
A C ꝏ53 G
5 3
5 -2 3
D F
-1
ꝏ5 ꝏ 4
V A B C D E F G
d[v] 0 1 3 5 0 4 3
π[v] Nil C D A B D E
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
-1
B E
Pair Paths Shortest Distance
6 -2 1 3
ꝏ 0
A→B A→D→C
5 ꝏ8 7
A C ꝏ53 G
5 3
5 -2 3
D F
-1
ꝏ5 ꝏ 4
V A B C D E F G
d[v] 0 1 3 5 0 4 3
π[v] Nil C D A B D E
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
-1
B E
Shortest
Pair Paths 6 -2
Distance 1 3
ꝏ 0
A→B A→D→C→B 5 ꝏ8 7
A C ꝏ53 G
5 3
5 -2 3
D F
-1
ꝏ5 ꝏ 4
V A B C D E F G
d[v] 0 1 3 5 0 4 3
π[v] Nil C D A B D E
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
-1
B E
Pair Paths Shortest Distance
6 -2 1 3
ꝏ 0
A→B A→D→C→B 5+(-2)+(-2) = 1
5 ꝏ8 7
A C ꝏ53 G
5 3
5 -2 3
D F
-1
ꝏ5 ꝏ 4
V A B C D E F G
d[v] 0 1 3 5 0 4 3
π[v] Nil C D A B D E
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
-1
B E
Pair Paths Shortest Distance
6 -2 1 3
ꝏ 0
A→B A→D→C→B 5+(-2)+(-2) = 1
5 ꝏ8 7
A C ꝏ53 G
A→C
5 3
5 -2 3
D F
-1
ꝏ5 ꝏ 4
V A B C D E F G
d[v] 0 1 3 5 0 4 3
π[v] Nil C D A B D E
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
-1
B E
Pair Paths Shortest Distance
6 -2 1 3
ꝏ 0
A→B A→D→C→B 5+(-2)+(-2) = 1
5 ꝏ8 7
A C ꝏ53 G
A→C A→D→C 5-2 = 3
5 3
5 -2 3
D F
-1
ꝏ5 ꝏ 4
V A B C D E F G
d[v] 0 1 3 5 0 4 3
π[v] Nil C D A B D E
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
-1
B E
Pair Paths Shortest Distance
6 -2 1 3
ꝏ 0
A→B A→D→C→B 5+(-2)+(-2) = 1
5 ꝏ8 7
A C ꝏ53 G
A→C A→D→C 5-2 = 3
5 3
5 -2 3
A→D
D F
-1
ꝏ5 ꝏ 4
V A B C D E F G
d[v] 0 1 3 5 0 4 3
π[v] Nil C D A B D E
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
-1
B E
Pair Paths Shortest Distance
6 -2 1 3
ꝏ 0
A→B A→D→C→B 5+(-2)+(-2) = 1
5 ꝏ8 7
A C ꝏ53 G
A→C A→D→C 5-2 = 3
5 3
5 -2 3
A→D A→D 5
D F
-1
ꝏ5 ꝏ 4
V A B C D E F G
d[v] 0 1 3 5 0 4 3
π[v] Nil C D A B D E
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
-1
B E
Shortest
Pair Paths 6 -2
Distance 1 3
ꝏ 0
A→B A→D→C→B 5+(-2)+(-2) = 1 5 ꝏ8 7
A C ꝏ53 G
A→C A→D→C 5-2 = 3 5 3
5 -2 3
A→D A→D 5
D F
-1
A→E ꝏ5 ꝏ 4
V A B C D E F G
d[v] 0 1 3 5 0 4 3
π[v] Nil C D A B D E
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
-1
B E
Shortest
Pair Paths 6 -2
Distance 1 3
ꝏ 0
A→B A→D→C→B 5+(-2)+(-2) = 1 5 ꝏ8 7
A C ꝏ53 G
A→C A→D→C 5-2 = 3 5 3
5 -2 3
A→D A→D 5
D F
-1
A→E A→D→C→B→E 5-2-2-1 = 0 ꝏ5 ꝏ 4
V A B C D E F G
d[v] 0 1 3 5 0 4 3
π[v] Nil C D A B D E
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
-1
B E
Shortest
Pair Paths 6 -2
Distance 1 3
ꝏ 0
A→B A→D→C→B 5+(-2)+(-2) = 1 5 ꝏ8 7
A C ꝏ53 G
A→C A→D→C 5-2 = 3 5 3
5 -2 3
A→D A→D 5
D F
-1
A→E A→D→C→B→E 5-2-2-1 = 0 ꝏ5 ꝏ 4
A→F V A B C D E F G
d[v] 0 1 3 5 0 4 3
π[v] Nil C D A B D E
Single Source Shortest Path: Bellman-Ford Algorithm
ꝏ6 3 1 ꝏ 5 2 0
-1
B E
Shortest
Pair Paths 6 -2
Distance 1 3
ꝏ 0
A→B A→D→C→B 5+(-2)+(-2) = 1 5 ꝏ8 7
A C ꝏ53 G
A→C A→D→C 5-2 = 3 5 3
5 -2 3
A→D A→D 5
D F
-1
A→E A→D→C→B→E 5-2-2-1 = 0 ꝏ5 ꝏ 4
A→D→C→B→E d[v] 0 1 3 5 0 4 3
A→G 5-2-2-1+3 = 3
→G π[v] Nil C D A B D E
Bellman-Ford Algorithm : Time Complexity
BELLMAN-FORD(G, w, s)
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞
3. π[v] ← NIL
4. d[s] = 0
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G]
7. if d[v] > d[u] + w(u, v)
8. then d[v] ← d[u] + w(u, v)
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE
13. return TRUE
Bellman-Ford Algorithm : Time Complexity
BELLMAN-FORD(G, w, s)
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞
3. π[v] ← NIL
4. d[s] = 0
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G]
7. if d[v] > d[u] + w(u, v)
8. then d[v] ← d[u] + w(u, v)
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE
13. return TRUE
Bellman-Ford Algorithm : Time Complexity
BELLMAN-FORD(G, w, s)
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ Executes V times so time
complexity is O(V)
3. π[v] ← NIL
4. d[s] = 0
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G]
7. if d[v] > d[u] + w(u, v)
8. then d[v] ← d[u] + w(u, v)
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE
13. return TRUE
Bellman-Ford Algorithm : Time Complexity
BELLMAN-FORD(G, w, s)
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ Executes V times so time
complexity is O(V)
3. π[v] ← NIL
4. d[s] = 0
5. for i ← 1 to |V[G]| - 1
6. do for each edge (u, v) ϵ E[G]
7. if d[v] > d[u] + w(u, v) Executes V-1 times so time
8. then d[v] ← d[u] + w(u, v) complexity is O(V-1)
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE
13. return TRUE
Bellman-Ford Algorithm : Time Complexity
BELLMAN-FORD(G, w, s)
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ Executes V times so time
complexity is O(V)
3. π[v] ← NIL
4. d[s] = 0
5. for i ← 1 to |V[G]| - 1 Executes V-1 times so time
6. do for each edge (u, v) ϵ E[G] complexity is O(V-1)
7. if d[v] > d[u] + w(u, v)
8. then d[v] ← d[u] + w(u, v) Executes E times so time
9. π[v] ← u complexity is O(E)
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE
13. return TRUE
Bellman-Ford Algorithm : Time Complexity
BELLMAN-FORD(G, w, s)
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ Executes V times so time
complexity is O(V)
3. π[v] ← NIL
4. d[s] = 0
5. for i ← 1 to |V[G]| - 1 Executes V-1 times so time
6. do for each edge (u, v) ϵ E[G] complexity is O(V-1)
7. if d[v] > d[u] + w(u, v)
Executes E times so time O(V.E)
8. then d[v] ← d[u] + w(u, v)
9. π[v] ← u complexity is O(E)
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE
13. return TRUE
Bellman-Ford Algorithm : Time Complexity
BELLMAN-FORD(G, w, s)
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ Executes V times so time
complexity is O(V)
3. π[v] ← NIL
4. d[s] = 0
5. for i ← 1 to |V[G]| - 1 Executes V-1 times so time
6. do for each edge (u, v) ϵ E[G] complexity is O(V-1)
7. if d[v] > d[u] + w(u, v)
Executes E times so time O(V.E)
8. then d[v] ← d[u] + w(u, v)
9. π[v] ← u complexity is O(E)
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v) Executes E times so time
complexity is O(E)
12. then return FALSE
13. return TRUE
Bellman-Ford Algorithm : Time Complexity
BELLMAN-FORD(G, w, s)
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ Executes V times so time
complexity is O(V)
3. π[v] ← NIL
4. d[s] = 0
5. for i ← 1 to |V[G]| - 1 Executes V-1 times so time
6. do for each edge (u, v) ϵ E[G] complexity is O(V-1)
O(V) +
7. if d[v] > d[u] + w(u, v) O(V.E)
Executes E times so time O(V. E) +
8. then d[v] ← d[u] + w(u, v) O(V)
complexity is O(E)
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v) Executes E times so time
complexity is O(E)
12. then return FALSE
13. return TRUE
Bellman-Ford Algorithm : Time Complexity
BELLMAN-FORD(G, w, s)
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ Executes V times so time
complexity is O(V)
3. π[v] ← NIL
4. d[s] = 0
5. for i ← 1 to |V[G]| - 1 Executes V-1 times so time
6. do for each edge (u, v) ϵ E[G] complexity is O(V-1)
O(V) +
7. if d[v] > d[u] + w(u, v) O(V.E)
Executes E times so time O(V. E) +
8. then d[v] ← d[u] + w(u, v) O(V)
complexity is O(E)
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v) Executes E times so time
complexity is O(E)
12. then return FALSE
13. return TRUE
Bellman-Ford Algorithm : Time Complexity
BELLMAN-FORD(G, w, s)
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞ Executes V times so time
complexity is O(V)
3. π[v] ← NIL
4. d[s] = 0
5. for i ← 1 to |V[G]| - 1 Executes V-1 times so time
6. do for each edge (u, v) ϵ E[G] complexity is O(V-1)
7. if d[v] > d[u] + w(u, v) O(V.E) O(V. E)
Executes E times so time
8. then d[v] ← d[u] + w(u, v)
complexity is O(E)
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v) Executes E times so time
complexity is O(E)
12. then return FALSE
13. return TRUE
Bellman-Ford Algorithm : Space Complexity
BELLMAN-FORD(G, w, s)
1. for each vertex v ϵ V[G]
2. do d[v] ← ∞
3. π[v] ← NIL
4. d[s] = 0
5. for i ← 1 to |V[G]| - 1 O(V) =for an array of distances.
6. do for each edge (u, v) ϵ E[G]
7. if d[v] > d[u] + w(u, v)
8. then d[v] ← d[u] + w(u, v)
9. π[v] ← u
10. for each edge (u, v) ϵ E[G]
11. do if d[v] > d[u] + w(u, v)
12. then return FALSE
13. return TRUE
Topics to be covered
➢ 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
All pair shortest path: Floyd Warshall Algorithm
➢ All pairs shortest path problem is to find the shortest paths between each pair of nodes in a
given weighted directed graph
8 2 1
1 3 2
1 3 3 2
2 5 2
7 4 4
3 4
1 1 1
2 3 4 2
4 3
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W)
1. n ← rows[W]
2. D(0) ← W
3. for k ← 1 to n
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix
5. do for i ← 1 to n
6. do for j ← 1 to n
𝑘 𝑘−1 𝑘−1 𝑘−1
7. do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗 , 𝑑ⅈ𝑘 + 𝑑𝑘𝑗
8. return D(n)
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W) 3
A B
1. n ← rows[W]
2. D(0) ← W 7
3. for k ← 1 to n -4
8
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix E
2 1
5. do for i ← 1 to n 4
6. do for j ← 1 to n 6
C D
7.
𝑘
do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗
𝑘−1 𝑘−1
, 𝑑ⅈ𝑘
𝑘−1
+ 𝑑𝑘𝑗 -5
8. return D(n)
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W) 3
A B
1. n ← rows[W]
2. D(0) ← W 7
3. for k ← 1 to n -4
8
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix E
2 1
5. do for i ← 1 to n 4
6. do for j ← 1 to n 6
C D
7.
𝑘
do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗
𝑘−1 𝑘−1
, 𝑑ⅈ𝑘
𝑘−1
+ 𝑑𝑘𝑗 -5
8. return D(n)
A B C D E
A
B
C
D
E
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W) 3
A B
1. n ← rows[W]
2. D(0) ← W 7
3. for k ← 1 to n -4
8
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix E
2 1
5. do for i ← 1 to n 4
6. do for j ← 1 to n 6
C D
7.
𝑘
do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗
𝑘−1 𝑘−1
, 𝑑ⅈ𝑘
𝑘−1
+ 𝑑𝑘𝑗 -5
8. return D(n)
A B C D E
A 0
B 0
C 0
D 0
E 0
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W) 3
A B
1. n ← rows[W]
2. D(0) ← W 7
3. for k ← 1 to n -4
8
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix E
4 2 1
5. do for i ← 1 to n
6. do for j ← 1 to n 6
C D
7.
𝑘
do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗
𝑘−1 𝑘−1
, 𝑑ⅈ𝑘
𝑘−1
+ 𝑑𝑘𝑗 -5
8. return D(n)
A B C D E
A 0 3 8 -4
B 0 1 7
C 4 0
D 2 -5 0
E 6 0
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W) 3
A B
1. n ← rows[W]
2. D(0) ← W 7
3. for k ← 1 to n -4
8
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix E
4 2 1
5. do for i ← 1 to n
6. do for j ← 1 to n 6
C D
7.
𝑘
do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗
𝑘−1 𝑘−1
, 𝑑ⅈ𝑘
𝑘−1
+ 𝑑𝑘𝑗 -5
8. return D(n)
A B C D E
A 0 3 8 ꝏ -4
B ꝏ 0 ꝏ 1 7
C ꝏ 4 0 ꝏ ꝏ
D 2 ꝏ -5 0 ꝏ
E ꝏ ꝏ ꝏ 6 0
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W) 3
A B
1. n ← rows[W]
2. D(0) ← W 7
3. for k ← 1 to n -4
8
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix E
4 2 1
5. do for i ← 1 to n
6. do for j ← 1 to n 6
C D
7.
𝑘
do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗
𝑘−1 𝑘−1
, 𝑑ⅈ𝑘
𝑘−1
+ 𝑑𝑘𝑗 -5
8. return D(n)
A B C D E A B C D E
A 0 3 8 ꝏ -4 A
B ꝏ 0 ꝏ 1 7 B
D0 𝝅0
C ꝏ 4 0 ꝏ ꝏ C
D 2 ꝏ -5 0 ꝏ D
E ꝏ ꝏ ꝏ 6 0 E
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W) 3
A B
1. n ← rows[W]
2. D(0) ← W 7
3. for k ← 1 to n -4
8
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix E
4 2 1
5. do for i ← 1 to n
6. do for j ← 1 to n 6
C D
7.
𝑘
do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗
𝑘−1 𝑘−1
, 𝑑ⅈ𝑘
𝑘−1
+ 𝑑𝑘𝑗 -5
8. return D(n)
A B C D E A B C D E
A 0 3 8 ꝏ -4 A Nil A A Nil A
B ꝏ 0 ꝏ 1 7 B
D0 𝝅0
C ꝏ 4 0 ꝏ ꝏ C
D 2 ꝏ -5 0 ꝏ D
E ꝏ ꝏ ꝏ 6 0 E
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W) 3
A B
1. n ← rows[W]
2. D(0) ← W 7
3. for k ← 1 to n -4
8
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix E
4 2 1
5. do for i ← 1 to n
6. do for j ← 1 to n 6
C D
7.
𝑘
do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗
𝑘−1 𝑘−1
, 𝑑ⅈ𝑘
𝑘−1
+ 𝑑𝑘𝑗 -5
8. return D(n)
A B C D E A B C D E
A 0 3 8 ꝏ -4 A Nil A A Nil A
B ꝏ 0 ꝏ 1 7 B Nil Nil Nil B B
D0 𝝅0
C ꝏ 4 0 ꝏ ꝏ C
D 2 ꝏ -5 0 ꝏ D
E ꝏ ꝏ ꝏ 6 0 E
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W) 3
A B
1. n ← rows[W]
2. D(0) ← W 7
3. for k ← 1 to n -4
8
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix E
4 2 1
5. do for i ← 1 to n
6. do for j ← 1 to n 6
C D
7.
𝑘
do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗
𝑘−1 𝑘−1
, 𝑑ⅈ𝑘
𝑘−1
+ 𝑑𝑘𝑗 -5
8. return D(n)
A B C D E A B C D E
A 0 3 8 ꝏ -4 A Nil A A Nil A
B ꝏ 0 ꝏ 1 7 B Nil Nil Nil B B
D0 𝝅0
C ꝏ 4 0 ꝏ ꝏ C Nil C Nil Nil Nil
D 2 ꝏ -5 0 ꝏ D
E ꝏ ꝏ ꝏ 6 0 E
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W) 3
A B
1. n ← rows[W]
2. D(0) ← W 7
3. for k ← 1 to n -4
8
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix E
4 2 1
5. do for i ← 1 to n
6. do for j ← 1 to n 6
C D
7.
𝑘
do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗
𝑘−1 𝑘−1
, 𝑑ⅈ𝑘
𝑘−1
+ 𝑑𝑘𝑗 -5
8. return D(n)
A B C D E A B C D E
A 0 3 8 ꝏ -4 A Nil A A Nil A
B ꝏ 0 ꝏ 1 7 B Nil Nil Nil B B
D0 𝝅0
C ꝏ 4 0 ꝏ ꝏ C Nil C Nil Nil Nil
D 2 ꝏ -5 0 ꝏ D D Nil D Nil Nil
E ꝏ ꝏ ꝏ 6 0 E
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W) 3
A B
1. n ← rows[W]
2. D(0) ← W 7
3. for k ← 1 to n -4
8
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix E
4 2 1
5. do for i ← 1 to n
6. do for j ← 1 to n 6
C D
7.
𝑘
do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗
𝑘−1 𝑘−1
, 𝑑ⅈ𝑘
𝑘−1
+ 𝑑𝑘𝑗 -5
8. return D(n)
A B C D E A B C D E
A 0 3 8 ꝏ -4 A Nil A A Nil A
B ꝏ 0 ꝏ 1 7 B Nil Nil Nil B B
D0 𝝅0
C ꝏ 4 0 ꝏ ꝏ C Nil C Nil Nil Nil
D 2 ꝏ -5 0 ꝏ D D Nil D Nil Nil
E ꝏ ꝏ ꝏ 6 0 E Nil Nil Nil E Nil
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W) 3
A B
1. n ← rows[W]
2. D(0) ← W 7
3. for k ← 1 to n -4
8
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix E
4 2 1
5. do for i ← 1 to n
6. do for j ← 1 to n 6
C D
7.
𝑘
do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗
𝑘−1 𝑘−1
, 𝑑ⅈ𝑘
𝑘−1
+ 𝑑𝑘𝑗 -5
8. return D(n)
A B C D E
A 0 3 8 ꝏ -4
Considering each node is as an
intermediate node, we have to find B ꝏ 0 ꝏ 1 7
the is any shorter path through that C ꝏ 4 0 ꝏ ꝏ
intermediate node
D 2 ꝏ -5 0 ꝏ
E ꝏ ꝏ ꝏ 6 0
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W) A B C D E
1. n ← rows[W] A Nil A A Nil A
2. D(0) ← W B Nil Nil Nil B B
3. for k ← 1 to n 𝝅0
C Nil C Nil Nil Nil
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix D D Nil D Nil Nil
5. do for i ← 1 to n E Nil Nil Nil E Nil
6. do for j ← 1 to n
𝑘 𝑘−1 𝑘−1 𝑘−1
7. do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗 , 𝑑ⅈ𝑘 + 𝑑𝑘𝑗
8. return D(n)
A B C D E
A 0 3 8 ꝏ -4
B ꝏ 0 ꝏ 1 7
D0
C ꝏ 4 0 ꝏ ꝏ
D 2 ꝏ -5 0 ꝏ
E ꝏ ꝏ ꝏ 6 0
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W) A B C D E
1. n ← rows[W] A Nil A A Nil A
2. D(0) ← W B Nil Nil Nil B B
3. for k ← 1 to n 𝝅0
C Nil C Nil Nil Nil
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix D D Nil D Nil Nil
5. do for i ← 1 to n E Nil Nil Nil E Nil
6. do for j ← 1 to n
𝑘 𝑘−1 𝑘−1 𝑘−1
7. do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗 , 𝑑ⅈ𝑘 + 𝑑𝑘𝑗
8. return D(n)
A B C D E
A 0 3 8 ꝏ -4 Consider vertex A as intermediate node, so
B ꝏ 0 ꝏ 1 7 we have find whether there is shortest path
D0 through vertex A
C ꝏ 4 0 ꝏ ꝏ
D 2 ꝏ -5 0 ꝏ
E ꝏ ꝏ ꝏ 6 0
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W) A B C D E k=1
1. n ← rows[W] A Nil A A Nil A
2. D(0) ← W B Nil Nil Nil B B
3. for k ← 1 to n 𝝅A
C Nil C Nil Nil Nil
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix D D Nil D Nil Nil
5. do for i ← 1 to n E Nil Nil Nil E Nil
6. do for j ← 1 to n
𝑘 𝑘−1 𝑘−1 𝑘−1
7. do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗 , 𝑑ⅈ𝑘 + 𝑑𝑘𝑗
8. return D(n)
A B C D E A B C D E
A 0 1 -3 2 -4 A Nil C D E A
B 3 0 -4 1 -1 B D Nil D B A
DE 𝝅E
C 7 4 0 5 3 C D C Nil B A
D 2 -1 -5 0 -2 D D C D Nil A
E 8 5 1 6 0 E D C D E Nil
All pair shortest path: Floyd Warshall Algorithm
FLOYD-WARSHALL(W) 3
A B
1. n ← rows[W] 7
2. D(0) ← W
3. for k ← 1 to n -4
8
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix E
4 2 1
5. do for i ← 1 to n
6. do for j ← 1 to n C D 6
7.
𝑘
do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗
𝑘−1 𝑘−1
, 𝑑ⅈ𝑘
𝑘−1
+ 𝑑𝑘𝑗 -5
8. return D(n)
FLOYD-WARSHALL(W)
1. n ← rows[W]
2. D(0) ← W
3. for k ← 1 to n
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix
5. do for i ← 1 to n
6. do for j ← 1 to n
𝑘 𝑘−1 𝑘−1 𝑘−1
7. do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗 , 𝑑ⅈ𝑘 + 𝑑𝑘𝑗
8. return D(n)
Floyd Warshall Algorithm: Time Complexity
FLOYD-WARSHALL(W)
1. n ← rows[W]
2. D(0) ← W
n is number of vertex present in a graph
3. for k ← 1 to n So time complexity is O(V)/ O(N)
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix
5. do for i ← 1 to n
6. do for j ← 1 to n
𝑘 𝑘−1 𝑘−1 𝑘−1
7. do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗 , 𝑑ⅈ𝑘 + 𝑑𝑘𝑗
8. return D(n)
Floyd Warshall Algorithm: Time Complexity
FLOYD-WARSHALL(W)
1. n ← rows[W]
2. D(0) ← W
n is number of vertex present in a graph
3. for k ← 1 to n So time complexity is O(V)/ O(N)
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix
5. do for i ← 1 to n
6. do for j ← 1 to n
𝑘 𝑘−1 𝑘−1 𝑘−1
7. do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗 , 𝑑ⅈ𝑘 + 𝑑𝑘𝑗
8. return D(n)
Floyd Warshall Algorithm: Time Complexity
FLOYD-WARSHALL(W)
1. n ← rows[W]
2. D(0) ← W
n is number of vertex present in a graph
3. for k ← 1 to n So time complexity is O(V)/ O(N)
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix
n is number of vertex present in a graph
5. do for i ← 1 to n So time complexity is O(V)/ O(N)
6. do for j ← 1 to n
𝑘 𝑘−1 𝑘−1 𝑘−1
7. do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗 , 𝑑ⅈ𝑘 + 𝑑𝑘𝑗
8. return D(n)
Floyd Warshall Algorithm: Time Complexity
FLOYD-WARSHALL(W)
1. n ← rows[W]
2. D(0) ← W
n is number of vertex present in a graph
3. for k ← 1 to n So time complexity is O(V)/ O(N)
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix
n is number of vertex present in a graph
5. do for i ← 1 to n So time complexity is O(V)/ O(N)
6. do for j ← 1 to n
𝑘 𝑘−1 𝑘−1 𝑘−1
7. do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗 , 𝑑ⅈ𝑘 + 𝑑𝑘𝑗
8. return D(n)
Floyd Warshall Algorithm: Time Complexity
FLOYD-WARSHALL(W)
1. n ← rows[W]
2. D(0) ← W
n is number of vertex present in a graph
3. for k ← 1 to n So time complexity is O(V)/ O(N)
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix
n is number of vertex present in a graph
5. do for i ← 1 to n So time complexity is O(V)/ O(N)
6. do for j ← 1 to n
𝑘 𝑘−1 𝑘−1 𝑘−1
7. do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗 , 𝑑ⅈ𝑘 + 𝑑𝑘𝑗
8. return D(n)
n is number of vertex present in a graph
So time complexity is O(V)/ O(N)
O(V3)
Floyd Warshall Algorithm: Space Complexity
FLOYD-WARSHALL(W)
1. n ← rows[W] O(V2) : v x v matrix is used
2. D(0) ← W where v is equal to number
3. for k ← 1 to n of vertex
𝑘
4. Let Dk = (𝑑ⅈ𝑗 ) be a new n x n matrix
5. do for i ← 1 to n
6. do for j ← 1 to n
𝑘 𝑘−1 𝑘−1 𝑘−1
7. do 𝑑ⅈ𝑗 = min 𝑑ⅈ𝑗 , 𝑑ⅈ𝑘 + 𝑑𝑘𝑗
8. return D(n)
Topics to be covered
➢ 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
Assembly-line scheduling Problem
➢ In the manufacturing industry, the usage of multiple assembly lines speeds up the manufacturing
process. It is achieved through efficient assembly line scheduling
fi[j] : the fastest time to get from the starting point through station Si,j
e1 + a1,1 if j = 1
f1[j] =
min(f1[j - 1] + a1,j ,f2[j -1] + t2,j-1 + a1,j) if j ≥ 2
e2 + a2,1 if j = 1
f2[j] =
min(f2[j - 1] + a2,j ,f1[j -1] + t1,j-1 + a2,j) if j ≥ 2
Assembly-line scheduling Problem
ASSEMBLY-LINE-SCHEDULING(a, t, e, x, n )
1.f1[1] ← e1 + a1,1
2. f2[1] ← e2 + a2,1
3. for j ← 2 to n
4. do if f1[j - 1] + a1,j ≤ f2[j - 1] + t2, j-1 + a1, j
5. then f1[j] ← f1[j - 1] + a1, j PRINT-STATIONS(l, n)
6. l1[j] ← 1 i ← l*
7. else f1[j] ← f2[j - 1] + t2, j-1 + a1, j print “line ” i “, station ” n
8. l1[j] ← 2 for j ← n down to 2
9. if f2[j - 1] + a2, j ≤ f1[j - 1] + t1, j-1 + a2, j do i ←li[j]
10. then f2[j] ← f2[j - 1] + a2, j print “line ” i “, station ” j - 1
11. l2[j] ← 2
12. else f2[j] ← f1[j - 1] + t1, j-1 + a2, j
13. l2[j] ← 1
14. if f1[n] + x1 ≤ f2[n] + x2
15. then f* = f1[n] + x1
16. l* = 1
17. else f* = f2[n] + x2
18. l* = 2
Assembly-line scheduling
a
Problem
a a a1,6
a1,1 1,2 a1,3 1,4 1,5
2 8 9 3 4 1
e1 x1
t1,1 3 t1,3 2 3
4 t1,2 1 t1,4 1 t15 3
e2 + a2,1 if j = 1
f2[j] =
min(f2[j - 1] + a2,j ,f1[j -1] + t1,j-1 + a2,j) if j ≥ 2
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
f1[j]
f2[j]
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
f1[j]
f2[j]
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
f1[1] = e1 + a1,1
f1[1]
f1[j]
f2[j]
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
f1[1] = e1 + a1,1
f1[1]
f1[j]
f2[j]
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
f1[1] = e1 + a1,1
f1[1] = 4 + 2 = 6
f1[j]
f2[j]
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
f1[1] = e1 + a1,1
f1[1] = 4 + 2 = 6
f1[j] 6
f2[j]
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
f1[1] = e1 + a1,1
f1[1] = 4 + 2 = 6
f2[1] = e2 + a2,1
j=1 j=2 j=3 j=4 j=5 j=6
f2[1] =
f1[j] 6
f2[j]
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
f1[1] = e1 + a1,1
f1[1] = 4 + 2 = 6
f2[1] = e2 + a2,1
j=1 j=2 j=3 j=4 j=5 j=6
f2[1] =
f1[j] 6
f2[j]
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
f1[1] = e1 + a1,1
f1[1] = 4 + 2 = 6
f2[1] = e2 + a2,1
j=1 j=2 j=3 j=4 j=5 j=6
f2[1] = 2 + 6 = 8
f1[j] 6
f2[j]
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
f1[1] = e1 + a1,1
f1[1] = 4 + 2 = 6
f2[1] = e2 + a2,1
j=1 j=2 j=3 j=4 j=5 j=6
f2[1] = 2 + 6 = 8
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
=min(6+
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
=min(6+
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
=min(6+
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
=min(6+8
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
=min(6+8 ,
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
=min(6+8 ,
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
=min(6+8 , 8+
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
=min(6+8 , 8+
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
=min(6+8 , 8+
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
=min(6+8 , 8+3
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
=min(6+8 , 8+3
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
=min(6+8 , 8+3
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
=min(6+8 , 8+3+8)
= min(14, 19) = 14
j=1 j=2 j=3 j=4 j=5 j=6
f1[j] 6
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j]
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
=min(6+8 , 8+3+8)
= min(14, 19) = 14
j=1 j=2 j=3 j=4 j=5 j=6
f1[j] 6 14
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
=min(6+8 , 8+3+8)
= min(14, 19) = 14
j=1 j=2 j=3 j=4 j=5 j=6
f1[j] 6 14
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[2] =min(f2[1] + a2,2 , f1[1] + t1, 1 + a2,2)
f1[j] 6 14
f2[j] 8
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[2] =min(f2[1] + a2,2 , f1[1] + t1, 1 + a2,2)
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1
2 3 4 1 1 3 l2[j]
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[2] =min(f2[1] + a2,2 , f1[1] + t1, 1 + a2,2)
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1
2 3 4 1 1 3 l2[j] 2
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[2] =min(f2[1] + a2,2 , f1[1] + t1, 1 + a2,2)
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1
2 3 4 1 1 3 l2[j] 2
7
6 1 2 2 7 3
f1[3] =
f1[j] 6 14
f2[j] 8 9
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1
2 3 4 1 1 3 l2[j] 2
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[3] =min(f2[2] + a2,3 , f1[2] + t1, 2 + a2,3)
f1[j] 6 14
f2[j] 8 9
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1
2 3 4 1 1 3 l2[j] 2
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[3] =min(f2[2] + a2,3 , f1[2] + t1, 2 + a2,3)
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1
2 3 4 1 1 3 l2[j] 2
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[3] =min(f2[2] + a2,3 , f1[2] + t1, 2 + a2,3)
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2
2 3 4 1 1 3 l2[j] 2 2
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[3] =min(f2[2] + a2,3 , f1[2] + t1, 2 + a2,3)
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2
2 3 4 1 1 3 l2[j] 2 2
7
6 1 2 2 7 3
f1[4] =
f1[j] 6 14 22
f2[j] 8 9 11
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2
2 3 4 1 1 3 l2[j] 2 2
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[4] =min(f2[3] + a2,4 , f1[3] + t1, 3 + a2,4)
f1[j] 6 14 22
f2[j] 8 9 11
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2
2 3 4 1 1 3 l2[j] 2 2
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[4] =min(f2[3] + a2,4 , f1[3] + t1, 3 + a2,4)
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2
2 3 4 1 1 3 l2[j] 2 2
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[4] =min(f2[3] + a2,4 , f1[3] + t1, 3 + a2,4)
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2
2 3 4 1 1 3 l2[j] 2 2 2
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[4] =min(f2[3] + a2,4 , f1[3] + t1, 3 + a2,4)
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2
2 3 4 1 1 3 l2[j] 2 2 2
7
6 1 2 2 7 3
f1[5] =
f1[j] 6 14 22 15
f2[j] 8 9 11 13
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2
2 3 4 1 1 3 l2[j] 2 2 2
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[5] =min(f2[4] + a2,5 , f1[4] + t1, 4 + a2,5)
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2
2 3 4 1 1 3 l2[j] 2 2 2
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[5] =min(f2[4] + a2,5 , f1[4] + t1, 4 + a2,5)
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2
2 3 4 1 1 3 l2[j] 2 2 2 2
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[5] =min(f2[4] + a2,5 , f1[4] + t1, 4 + a2,5)
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2
2 3 4 1 1 3 l2[j] 2 2 2 2
7
6 1 2 2 7 3
f1[6] =
f1[j] 6 14 22 15 18
f2[j] 8 9 11 13 20
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2
2 3 4 1 1 3 l2[j] 2 2 2 2
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[6] =min(f2[5] + a2,6 , f1[5] + t1, 5 + a2,6)
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2
2 3 4 1 1 3 l2[j] 2 2 2 2
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[6] =min(f2[5] + a2,6 , f1[5] + t1, 5 + a2,6)
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
j=1 j=2 j=3 j=4 j=5 j=6 f2[6] =min(f2[5] + a2,6 , f1[5] + t1, 5 + a2,6)
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
n=6
f1[j] 6 14 22 15 18 19
f2[j] 8 9 11 13 20 23
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
n=6
f1[j] 6 14 22 15 18 19
f2[j] 8 9 11 13 20 23
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
n=6
f1[j] 6 14 22 15 18 19
f2[j] 8 9 11 13 20 23
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
n=6
f1[j] 6 14 22 15 18 19
f2[j] 8 9 11 13 20 23
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
n=6
f1[j] 6 14 22 15 18 19
f2[j] 8 9 11 13 20 23
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
n=6
f1[j] 6 14 22 15 18 19
f2[j] 8 9 11 13 20 23
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
n=6
f1[j] 6 14 22 15 18 19
f2[j] 8 9 11 13 20 23
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
n=6
f1[j] 6 14 22 15 18 19
f2[j] 8 9 11 13 20 23
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
n=6
f1[j] 6 14 22 15 18 19
f2[j] 8 9 11 13 20 23
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
n=6
f1[j] 6 14 22 15 18 19
f2[j] 8 9 11 13 20 23
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
n=6
f1[j] 6 14 22 15 18 19
f2[j] 8 9 11 13 20 23
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
n=6
f1[j] 6 14 22 15 18 19
f2[j] 8 9 11 13 20 23
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
n=6
f1[j] 6 14 22 15 18 19
f2[j] 8 9 11 13 20 23
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
n=6
f2[j] 8 9 11 13 20 23
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
n=6
f2[j] 8 9 11 13 20 23
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
n=6
f2[j] 8 9 11 13 20 23 l* = 1
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
f1[j] 6 14 22 15 18 19
f2[j] 8 9 11 13 20 23
f * = 22 l* = 1
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
PRINT-STATIONS(l, n)
i ← l*
j=1 j=2 j=3 j=4 j=5 j=6 print “line ” i “, station ” n
6 14 22 15 18 19 for j ← n down to 2
f1[j] do i ←li[j]
f2[j] 8 9 11 13 20 23 print “line ” i “, station ” j - 1
f * = 22 l* = 1
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
PRINT-STATIONS(l, n)
i ← l*
j=1 j=2 j=3 j=4 j=5 j=6 print “line ” i “, station ” n
6 14 22 15 18 19 for j ← n down to 2
f1[j] do i ←li[j]
f2[j] 8 9 11 13 20 23 print “line ” i “, station ” j - 1
f * = 22 l* = 1 i=1
Assembly-line scheduling Problem
2 8 9 3 4 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
PRINT-STATIONS(l, n)
i ← l*
j=1 j=2 j=3 j=4 j=5 j=6 print “line ” i “, station ” n
6 14 22 15 18 19 for j ← n down to 2
f1[j] do i ←li[j]
f2[j] 8 9 11 13 20 23 print “line ” i “, station ” j - 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
PRINT-STATIONS(l, n)
i ← l*
j=1 j=2 j=3 j=4 j=5 j=6 print “line ” i “, station ” n
6 14 22 15 18 19 for j ← n down to 2
f1[j] do i ←li[j]
f2[j] 8 9 11 13 20 23 print “line ” i “, station ” j - 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
PRINT-STATIONS(l, n)
i ← l*
j=1 j=2 j=3 j=4 j=5 j=6 print “line ” i “, station ” n
6 14 22 15 18 19 for j ← n down to 2
f1[j] do i ←li[j]
f2[j] 8 9 11 13 20 23 print “line ” i “, station ” j - 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
PRINT-STATIONS(l, n)
i ← l*
j=1 j=2 j=3 j=4 j=5 j=6 print “line ” i “, station ” n
6 14 22 15 18 19 for j ← n down to 2
f1[j] do i ←li[j]
f2[j] 8 9 11 13 20 23 print “line ” i “, station ” j - 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
PRINT-STATIONS(l, n)
i ← l*
j=1 j=2 j=3 j=4 j=5 j=6 print “line ” i “, station ” n
6 14 22 15 18 19 for j ← n down to 2
f1[j] do i ←li[j]
f2[j] 8 9 11 13 20 23 print “line ” i “, station ” j - 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
PRINT-STATIONS(l, n)
i ← l*
j=1 j=2 j=3 j=4 j=5 j=6 print “line ” i “, station ” n
6 14 22 15 18 19 for j ← n down to 2
f1[j] do i ←li[j]
f2[j] 8 9 11 13 20 23 print “line ” i “, station ” j - 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
PRINT-STATIONS(l, n)
i ← l*
j=1 j=2 j=3 j=4 j=5 j=6 print “line ” i “, station ” n
6 14 22 15 18 19 for j ← n down to 2
f1[j] do i ←li[j]
f2[j] 8 9 11 13 20 23 print “line ” i “, station ” j - 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
PRINT-STATIONS(l, n)
i ← l*
j=1 j=2 j=3 j=4 j=5 j=6 print “line ” i “, station ” n
6 14 22 15 18 19 for j ← n down to 2
f1[j] do i ←li[j]
f2[j] 8 9 11 13 20 23 print “line ” i “, station ” j - 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
PRINT-STATIONS(l, n)
i ← l*
j=1 j=2 j=3 j=4 j=5 j=6 print “line ” i “, station ” n
6 14 22 15 18 19 for j ← n down to 2
f1[j] do i ←li[j]
f2[j] 8 9 11 13 20 23 print “line ” i “, station ” j - 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
PRINT-STATIONS(l, n)
i ← l*
j=1 j=2 j=3 j=4 j=5 j=6 print “line ” i “, station ” n
6 14 22 15 18 19 for j ← n down to 2
f1[j] do i ←li[j]
f2[j] 8 9 11 13 20 23 print “line ” i “, station ” j - 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
PRINT-STATIONS(l, n)
i ← l*
j=1 j=2 j=3 j=4 j=5 j=6 print “line ” i “, station ” n
6 14 22 15 18 19 for j ← n down to 2
f1[j] do i ←li[j]
f2[j] 8 9 11 13 20 23 print “line ” i “, station ” j - 1
3 2 3 3 J 2 3 4 5 6
4 1 1
l1[j] 1 2 2 2 1
2 3 4 1 1 3 l2[j] 2 2 2 2 2
7
6 1 2 2 7 3
PRINT-STATIONS(l, n)
i ← l*
j=1 j=2 j=3 j=4 j=5 j=6 print “line ” i “, station ” n
6 14 22 15 18 19 for j ← n down to 2
f1[j] do i ←li[j]
f2[j] 8 9 11 13 20 23 print “line ” i “, station ” j - 1
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j]
4 2 1 2 2 1 l2[j]
2
8 5 6 4 5 7
f1[1] = e1 + a1,1
f1[1] =
f2[1] = e2 + a2,1
j=1 j=2 j=3 j=4 j=5 j=6
f2[1] =
f1[j]
f2[j]
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j]
4 2 1 2 2 1 l2[j]
2
8 5 6 4 5 7
f1[1] = e1 + a1,1
f1[1] = 2 + 7 =9
f2[1] = e2 + a2,1
j=1 j=2 j=3 j=4 j=5 j=6
f2[1] = 4 + 8 =12
f1[j]
f2[j]
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j]
4 2 1 2 2 1 l2[j]
2
8 5 6 4 5 7
f1[1] = e1 + a1,1
f1[1] = 2 + 7 =9
f2[1] = e2 + a2,1
j=1 j=2 j=3 j=4 j=5 j=6
f2[1] = 4 + 8 =12
f1[j] 9
f2[j] 12
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j]
4 2 1 2 2 1 l2[j]
2
8 5 6 4 5 7
j=1 j=2 j=3 j=4 j=5 j=6 f2[2] =min(f2[1] + a2,2 , f1[1] + t1, 1 + a2,2)
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j]
4 2 1 2 2 1 l2[j]
2
8 5 6 4 5 7
j=1 j=2 j=3 j=4 j=5 j=6 f2[2] =min(f2[1] + a2,2 , f1[1] + t1, 1 + a2,2)
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1
4 2 1 2 2 1 l2[j] 1
2
8 5 6 4 5 7
j=1 j=2 j=3 j=4 j=5 j=6 f2[2] =min(f2[1] + a2,2 , f1[1] + t1, 1 + a2,2)
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1
4 2 1 2 2 1 l2[j] 1
2
8 5 6 4 5 7
j=1 j=2 j=3 j=4 j=5 j=6 f2[3] =min(f2[2] + a2,3 , f1[2] + t1, 2 + a2,3)
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1
4 2 1 2 2 1 l2[j] 1
2
8 5 6 4 5 7
j=1 j=2 j=3 j=4 j=5 j=6 f2[3] =min(f2[2] + a2,3 , f1[2] + t1, 2 + a2,3)
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2
4 2 1 2 2 1 l2[j] 1 2
2
8 5 6 4 5 7
j=1 j=2 j=3 j=4 j=5 j=6 f2[3] =min(f2[2] + a2,3 , f1[2] + t1, 2 + a2,3)
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2
4 2 1 2 2 1 l2[j] 1 2
2
8 5 6 4 5 7
j=1 j=2 j=3 j=4 j=5 j=6 f2[4] =min(f2[3] + a2,4 , f1[3] + t1, 3 + a2,4)
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2
4 2 1 2 2 1 l2[j] 1 2
2
8 5 6 4 5 7
j=1 j=2 j=3 j=4 j=5 j=6 f2[4] =min(f2[3] + a2,4 , f1[3] + t1, 3 + a2,4)
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1
4 2 1 2 2 1 l2[j] 1 2 1
2
8 5 6 4 5 7
j=1 j=2 j=3 j=4 j=5 j=6 f2[4] =min(f2[3] + a2,4 , f1[3] + t1, 3 + a2,4)
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1
4 2 1 2 2 1 l2[j] 1 2 1
2
8 5 6 4 5 7
j=1 j=2 j=3 j=4 j=5 j=6 f2[5] =min(f2[4] + a2,5 , f1[4] + t1, 4 + a2,5)
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1
4 2 1 2 2 1 l2[j] 1 2 1
2
8 5 6 4 5 7
j=1 j=2 j=3 j=4 j=5 j=6 f2[5] =min(f2[4] + a2,5 , f1[4] + t1, 4 + a2,5)
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1
4 2 1 2 2 1 l2[j] 1 2 1 2
2
8 5 6 4 5 7
j=1 j=2 j=3 j=4 j=5 j=6 f2[5] =min(f2[4] + a2,5 , f1[4] + t1, 4 + a2,5)
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1
4 2 1 2 2 1 l2[j] 1 2 1 2
2
8 5 6 4 5 7
j=1 j=2 j=3 j=4 j=5 j=6 f2[5] =min(f2[5] + a2,6 , f1[5] + t1, 5 + a2,6)
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1
4 2 1 2 2 1 l2[j] 1 2 1 2
2
8 5 6 4 5 7
j=1 j=2 j=3 j=4 j=5 j=6 f2[5] =min(f2[5] + a2,6 , f1[5] + t1, 5 + a2,6)
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
j=1 j=2 j=3 j=4 j=5 j=6 f2[5] =min(f2[5] + a2,6 , f1[5] + t1, 5 + a2,6)
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
f2[j] 12 16 22 25 30 37
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
l* = 1
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
f1[j] 9 18 20 24 32 35
f2[j] 12 16 22 25 30 37
PRINT-STATIONS(l, n)
i ← l*
print “line ” i “, station ” n
for j ← n down to 2
l* = 1
do i ←li[j]
print “line ” i “, station ” j - 1
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
f1[j] 9 18 20 24 32 35
f2[j] 12 16 22 25 30 37
PRINT-STATIONS(l, n)
i ← l*
l* = 1 = i
print “line ” i “, station ” n
for j ← n down to 2
do i ←li[j]
print “line ” i “, station ” j - 1
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
f1[j] 9 18 20 24 32 35
f2[j] 12 16 22 25 30 37
PRINT-STATIONS(l, n)
i ← l*
l* = 1 = i
print “line ” i “, station ” n
for j ← n down to 2
do i ←li[j]
print “line ” i “, station ” j - 1
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
f1[j] 9 18 20 24 32 35
f2[j] 12 16 22 25 30 37
PRINT-STATIONS(l, n)
i ← l*
l* = 1 = i
print “line ” i “, station ” n
for j ← n down to 2 li[j] = l1[6]
do i ←li[j]
print “line ” i “, station ” j - 1
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
f1[j] 9 18 20 24 32 35
f2[j] 12 16 22 25 30 37
PRINT-STATIONS(l, n)
i ← l*
l* = 1 = i
print “line ” i “, station ” n
for j ← n down to 2 li[j] = l1[6]
do i ←li[j]
print “line ” i “, station ” j - 1
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
PRINT-STATIONS(l, n)
i ← l*
l* = 1
print “line ” i “, station ” n
for j ← n down to 2 li[j] = l1[6]
do i ←li[j]
print “line ” i “, station ” j - 1 i=2
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
PRINT-STATIONS(l, n)
i ← l*
l* = 1
print “line ” i “, station ” n
for j ← n down to 2 li[j] = l1[6]
do i ←li[j]
print “line ” i “, station ” j - 1 i=2
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
PRINT-STATIONS(l, n)
i ← l*
l* = 1
print “line ” i “, station ” n
for j ← n down to 2 li[j] = l2[5]
do i ←li[j]
print “line ” i “, station ” j - 1 i=
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
PRINT-STATIONS(l, n)
i ← l*
l* = 1
print “line ” i “, station ” n
for j ← n down to 2 li[j] = l2[5]
do i ←li[j]
print “line ” i “, station ” j - 1 i=
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
PRINT-STATIONS(l, n)
i ← l*
l* = 1
print “line ” i “, station ” n
for j ← n down to 2 li[j] = l2[5]
do i ←li[j]
print “line ” i “, station ” j - 1 i=2
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
PRINT-STATIONS(l, n)
i ← l*
l* = 1
print “line ” i “, station ” n
for j ← n down to 2 li[j] = l2[5]
do i ←li[j]
print “line ” i “, station ” j - 1 i=2
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
PRINT-STATIONS(l, n)
i ← l*
l* = 1
print “line ” i “, station ” n
for j ← n down to 2 li[j] = l2[4]
do i ←li[j]
print “line ” i “, station ” j - 1 i=
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
PRINT-STATIONS(l, n)
i ← l*
l* = 1
print “line ” i “, station ” n
for j ← n down to 2 li[j] = l2[4]
do i ←li[j]
print “line ” i “, station ” j - 1 i=
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
PRINT-STATIONS(l, n)
i ← l*
l* = 1
print “line ” i “, station ” n
for j ← n down to 2 li[j] = l2[4]
do i ←li[j]
print “line ” i “, station ” j - 1 i=1
Assembly-line scheduling Problem : Example 2
7 9 3 4 8 4
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
2 1 4 J 2 3 4 5 6
2 3 3 3
l1[j] 1 2 1 1 2
4 2 1 2 2 1 l2[j] 1 2 1 2 2
2
8 5 6 4 5 7
➢ S0 = {(0,0)} Initially
M= 8
0/1 knapsack Problem : Sets representation
M= 8
M= 8
Initially Compute
Set S0
0/1 knapsack Problem : Sets representation
M= 8
Initially Compute
Set S0
0/1 knapsack Problem : Sets representation
M= 8
M= 8
M= 8
M= 8
M= 8
M= 8
Merge 𝑺𝟎 , 𝑺𝟎𝟏
𝑺𝟏 = 𝑺𝟎 ∪ 𝑺𝟎𝟏
0/1 knapsack Problem : Sets representation
M= 8
Merge 𝑺𝟎 , 𝑺𝟎𝟏
𝑺𝟏 = 𝑺𝟎 ∪ 𝑺𝟎𝟏
0/1 knapsack Problem : Sets representation
M= 8
M= 8
M= 8
M= 8
M= 8
M= 8
Merge 𝑺𝟏 , 𝑺𝟏𝟏
𝑺𝟐 = 𝑺𝟏 ∪ 𝑺𝟏𝟏
0/1 knapsack Problem : Sets representation
Merge 𝑺𝟏 , 𝑺𝟏𝟏
𝑺𝟐 = 𝑺𝟏 ∪ 𝑺𝟏𝟏
0/1 knapsack Problem : Sets representation
Merge 𝑺𝟐 , 𝑺𝟐𝟏
𝑺𝟑 = 𝑺𝟐 ∪ 𝑺𝟐𝟏
0/1 knapsack Problem : Sets representation
𝐒 𝟑 = ሼ 𝟎 , 𝟎 , 𝟏, 𝟐 , 𝟐, 𝟑 , 𝟑, 𝟓 ,
Merge 𝑺𝟐 , 𝑺𝟐𝟏 𝟓, 𝟒 , 𝟔, 𝟔 , 𝟕, 𝟕 }
𝑺𝟑 = 𝑺𝟐 ∪ 𝑺𝟐𝟏
0/1 knapsack Problem : Sets representation
𝐒 𝟑 = ሼ 𝟎 , 𝟎 , 𝟏, 𝟐 , 𝟐, 𝟑 , 𝟑, 𝟓 ,
Merge 𝑺𝟐 , 𝑺𝟐𝟏 This set is to be
𝟓, 𝟒 , 𝟔, 𝟔 , 𝟕, 𝟕 }discarded because 9 >
𝑺𝟑 = 𝑺𝟐 ∪ 𝑺𝟐𝟏 8 (Capacity of
knapsack)
0/1 knapsack Problem : Sets representation
𝐒 𝟑 = ሼ 𝟎 , 𝟎 , 𝟏, 𝟐 , 𝟐, 𝟑 , 𝟑, 𝟓 ,
Merge 𝑺𝟐 , 𝑺𝟐𝟏 𝟓, 𝟒 , 𝟔, 𝟔 , 𝟕, 𝟕 }
𝑺𝟑 = 𝑺𝟐 ∪ 𝑺𝟐𝟏
0/1 knapsack Problem : Sets representation
𝑺𝟒 = 𝑺𝟑 ∪ 𝑺𝟑𝟏
0/1 knapsack Problem : Sets representation
𝑺𝟒 = 𝑺𝟑 ∪ 𝑺𝟑𝟏
0/1 knapsack Problem : Sets representation
check (8, 8) ∈ 𝑺𝟒 𝐒𝟒 = ሼ 𝟎 , 𝟎 , 𝟏, 𝟐 , 𝟐, 𝟑 , 𝟑, 𝟓 , 𝟓, 𝟒 , 𝟔, 𝟓 , 𝟕, 𝟕 , 𝟖, 𝟖 , }
0/1 knapsack Problem : Sets representation
check (8, 8) ∈ 𝑺𝟒 𝐒𝟒 = ሼ 𝟎 , 𝟎 , 𝟏, 𝟐 , 𝟐, 𝟑 , 𝟑, 𝟓 , 𝟓, 𝟒 , 𝟔, 𝟓 , 𝟕, 𝟕 , 𝟖, 𝟖 , }
check (8, 8) ∉ 𝑺𝟑
0/1 knapsack Problem : Sets representation
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0/1 knapsack Problem : Tabular representation
M= 5 n= 4
Step-01:
Item(i) Weight(Wi) Profit(Pi)
➢ Draw a table say ‘T’ with (n+1) number of rows and
1 2 3 (w+1) number of columns.
➢ Fill all the boxes of 0th row and 0th column with zeroes
2 3 4
3 4 5
4 5 6
0/1 knapsack Problem : Tabular representation
M= 5 n= 4
Step-01:
Item(i) Weight(Wi) Profit(Pi)
➢ Draw a table say ‘T’ with (n+1) number of rows and
1 2 3 (M+1) number of columns.
➢ Fill all the boxes of 0th row and 0th column with zeroes
2 3 4
3 4 5
4 5 6
0 1 2 -- --- M
0
1
---
n
0/1 knapsack Problem : Tabular representation
M= 5 n= 4
Step-01:
Item(i) Weight(Wi) Profit(Pi)
➢ Draw a table say ‘T’ with (n+1) number of rows and
1 2 3 (M+1) number of columns.
2 3 4 ➢ Fill all the boxes of 0th row and 0th column with zeroes
3 4 5
4 5 6
0 1 2 3 4 5
0
1
2
3
4
0/1 knapsack Problem : Tabular representation
M= 5 n= 4
Step-01:
Item(i) Weight(Wi) Profit(Pi)
➢ Draw a table say ‘T’ with (n+1) number of rows and
1 2 3 (M+1) number of columns.
2 3 4 ➢ Fill all the boxes of 0th row and 0th column with zeroes
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
M= 5 n= 4
Step-02:
Item(i) Weight(Wi) Profit(Pi)
➢ Start filling the table row wise top to bottom from left
1 2 3 to right. Use the following formula-
2 3 4 T (i , j) = max { T ( i-1 , j ) , profiti + T( i-1 , j – weighti ) }
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
M= 5 n= 4
Step-02: Finding T(1,1)
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(1,1)
M= 5 n= 4 We have,
i = 1, j = 1
Item(i) Weight(Wi) Profit(Pi) (profit)i = (profit)1 = 3
(weight)i = (weight)1 = 2
1 2 3
2 3 4 T(1,1) = max { T(1-1 , 1) , 3 + T(1-1 , 1-2) }
T(1,1) = max { T(0,1) , 3 + T(0,-1) }
3 4 5 T(1,1) = T(0,1) { Ignore T(0,-1) }
4 5 6 T(1,1) = 0
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(1,1)
M= 5 n= 4 We have,
i = 1, j = 1
Item(i) Weight(Wi) Profit(Pi) (profit)i = (profit)1 = 3
(weight)i = (weight)1 = 2
1 2 3
2 3 4 T(1,1) = max { T(1-1 , 1) , 3 + T(1-1 , 1-2) }
T(1,1) = max { T(0,1) , 3 + T(0,-1) }
3 4 5 T(1,1) = T(0,1) { Ignore T(0,-1) }
4 5 6 T(1,1) = 0
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(1,2)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(1,2)
We have,
M= 5 n= 4 i = 1, j = 2
(profit)i = (profit)1 = 3
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)1 = 2
1 2 3
Substituting the values, we get-
2 3 4
T(1,2) = max { T(1-1 , 2) , 3 + T(1-1 , 2-2) }
3 4 5 T(1,2) = max { T(0,2) , 3 + T(0,0) }
4 5 6 T(1,2) = max {0 , 3+0}
T(1,2) = 3
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(1,2)
We have,
M= 5 n= 4 i = 1, j = 2
(profit)i = (profit)1 = 3
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)1 = 2
1 2 3
Substituting the values, we get-
2 3 4
T(1,2) = max { T(1-1 , 2) , 3 + T(1-1 , 2-2) }
3 4 5 T(1,2) = max { T(0,2) , 3 + T(0,0) }
4 5 6 T(1,2) = max {0 , 3+0}
T(1,2) = 3
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(1,3)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(1,3)
We have,
M= 5 n= 4 i = 1, j = 3
(profit)i = (profit)1 = 3
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)1 = 2
1 2 3
Substituting the values, we get-
2 3 4
T(1,3) = max { T(1-1 , 3) , 3 + T(1-1 , 3-2) }
3 4 5 T(1,3) = max { T(0,3) , 3 + T(0,1) }
4 5 6 T(1,3) = max {0 , 3+0}
T(1,3) = 3
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(1,3)
We have,
M= 5 n= 4 i = 1, j = 3
(profit)i = (profit)1 = 3
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)1 = 2
1 2 3
Substituting the values, we get-
2 3 4
T(1,3) = max { T(1-1 , 3) , 3 + T(1-1 , 3-2) }
3 4 5 T(1,3) = max { T(0,3) , 3 + T(0,1) }
4 5 6 T(1,3) = max {0 , 3+0}
T(1,3) = 3
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(1,4)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(1,4)
We have,
M= 5 n= 4 i = 1, j = 4
(profit)i = (profit)1 = 3
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)1 = 2
1 2 3
Substituting the values, we get-
2 3 4
T(1,4) = max { T(1-1 , 4) , 3 + T(1-1 , 4-2) }
3 4 5 T(1,4) = max { T(0,4) , 3 + T(0,2) }
4 5 6 T(1,4) = max {0 , 3+0}
T(1,4) = 3
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(1,4)
We have,
M= 5 n= 4 i = 1, j = 4
(profit)i = (profit)1 = 3
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)1 = 2
1 2 3
Substituting the values, we get-
2 3 4
T(1,4) = max { T(1-1 , 4) , 3 + T(1-1 , 4-2) }
3 4 5 T(1,4) = max { T(0,4) , 3 + T(0,2) }
4 5 6 T(1,4) = max {0 , 3+0}
T(1,4) = 3
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(1,5)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(1,5)
We have,
M= 5 n= 4 i = 1, j = 5
(profit)i = (profit)1 = 3
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)1 = 2
1 2 3
Substituting the values, we get-
2 3 4
T(1,5) = max { T(1-1 , 5) , 3 + T(1-1 , 5-2) }
3 4 5 T(1,5) = max { T(0,5) , 3 + T(0,3) }
4 5 6 T(1,5) = max {0 , 3+0}
T(1,5) = 3
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(1,5)
We have,
M= 5 n= 4 i = 1, j = 5
(profit)i = (profit)1 = 3
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)1 = 2
1 2 3
Substituting the values, we get-
2 3 4
T(1,5) = max { T(1-1 , 5) , 3 + T(1-1 , 5-2) }
3 4 5 T(1,5) = max { T(0,5) , 3 + T(0,3) }
4 5 6 T(1,5) = max {0 , 3+0}
T(1,5) = 3
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(2, 1)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(2, 1)
We have,
M= 5 n= 4 i = 2, j = 1
(profit)i = (profit)2 = 4
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)2 = 3
1 2 3
Substituting the values, we get-
2 3 4
T(2,1) = max { T(2-1 , 1) , 4 + T(2-1 , 1-3) }
3 4 5 T(2,1) = max { T(1,1) , 4 + T(1,-2) }
4 5 6 T(2,1) = T(1,1) { Ignore T(1,-2) }
T(2,1) = 0
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(2, 1)
We have,
M= 5 n= 4 i = 2, j = 1
(profit)i = (profit)2 = 4
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)2 = 3
1 2 3
Substituting the values, we get-
2 3 4
T(2,1) = max { T(2-1 , 1) , 4 + T(2-1 , 1-3) }
3 4 5 T(2,1) = max { T(1,1) , 4 + T(1,-2) }
4 5 6 T(2,1) = T(1,1) { Ignore T(1,-2) }
T(2,1) = 0
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(2, 2)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(2, 2)
We have,
M= 5 n= 4 i = 2, j = 2
(profit)i = (profit)2 = 4
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)2 = 3
1 2 3
Substituting the values, we get-
2 3 4
T(2,2) = max { T(2-1 , 2) , 4 + T(2-1 , 2-3) }
3 4 5 T(2,2) = max { T(1,2) , 4 + T(1,-1) }
4 5 6 T(2,2) = T(1,2) { Ignore T(1,-1) }
T(2,2) = 3
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(2, 2)
We have,
M= 5 n= 4 i = 2, j = 2
(profit)i = (profit)2 = 4
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)2 = 3
1 2 3
Substituting the values, we get-
2 3 4
T(2,2) = max { T(2-1 , 2) , 4 + T(2-1 , 2-3) }
3 4 5 T(2,2) = max { T(1,2) , 4 + T(1,-1) }
4 5 6 T(2,2) = T(1,2) { Ignore T(1,-1) }
T(2,2) = 3
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(2, 3)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(2, 3)
We have,
M= 5 n= 4 i = 2, j = 3
(profit)i = (profit)2 = 4
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)2 = 3
1 2 3
Substituting the values, we get-
2 3 4
T(2,3) = max { T(2-1 , 3) , 4 + T(2-1 , 3-3) }
3 4 5 T(2,3) = max { T(1,3) , 4 + T(1,0) }
4 5 6 T(2,3) = max { 3 , 4+0 }
T(2,3) = 4
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(2, 3)
We have,
M= 5 n= 4 i = 2, j = 3
(profit)i = (profit)2 = 4
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)2 = 3
1 2 3
Substituting the values, we get-
2 3 4
T(2,3) = max { T(2-1 , 3) , 4 + T(2-1 , 3-3) }
3 4 5 T(2,3) = max { T(1,3) , 4 + T(1,0) }
4 5 6 T(2,3) = max { 3 , 4+0 }
T(2,3) = 4
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(2, 4)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(2, 4)
We have,
M= 5 n= 4 i = 2, j = 4
(profit)i = (profit)2 = 4
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)2 = 3
1 2 3
Substituting the values, we get-
2 3 4
T(2,4) = max { T(2-1 , 4) , 4 + T(2-1 , 4-3) }
3 4 5 T(2,4) = max { T(1,4) , 4 + T(1,1) }
4 5 6 T(2,4) = max { 3 , 4+0 }
T(2,4) = 4
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(2, 4)
We have,
M= 5 n= 4 i = 2, j = 4
(profit)i = (profit)2 = 4
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)2 = 3
1 2 3
Substituting the values, we get-
2 3 4
T(2,4) = max { T(2-1 , 4) , 4 + T(2-1 , 4-3) }
3 4 5 T(2,4) = max { T(1,4) , 4 + T(1,1) }
4 5 6 T(2,4) = max { 3 , 4+0 }
T(2,4) = 4
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(2, 5)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(2, 5)
We have,
M= 5 n= 4 i = 2, j = 5
(profit)i = (profit)2 = 4
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)2 = 3
1 2 3
Substituting the values, we get-
2 3 4
T(2,5) = max { T(2-1 , 5) , 4 + T(2-1 , 5-3) }
3 4 5 T(2,5) = max { T(1,5) , 4 + T(1,2) }
4 5 6 T(2,5) = max { 3 , 4+3 }
T(2,5) = 7
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(2, 5)
We have,
M= 5 n= 4 i = 2, j = 5
(profit)i = (profit)2 = 4
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)2 = 3
1 2 3
Substituting the values, we get-
2 3 4
T(2,5) = max { T(2-1 , 5) , 4 + T(2-1 , 5-3) }
3 4 5 T(2,5) = max { T(1,5) , 4 + T(1,2) }
4 5 6 T(2,5) = max { 3 , 4+3 }
T(2,5) = 7
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(3, 1)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(3, 1)
We have,
M= 5 n= 4 i = 3, j = 1
(profit)i = (profit)3 = 5
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)3 = 4
1 2 3
Substituting the values, we get-
2 3 4
T(3,1) = max { T(3-1 , 1) , 5 + T(3-1 , 1-4) }
3 4 5 T(3,1) = max { T(2,1) , 5 + T(2,-3) }
4 5 6 T(3,1) = max { 0 } { Ignore T(1,-3) }
T(3,1) = 0
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(3, 1)
We have,
M= 5 n= 4 i = 3, j = 1
(profit)i = (profit)3 = 5
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)3 = 4
1 2 3
Substituting the values, we get-
2 3 4
T(3,1) = max { T(3-1 , 1) , 5 + T(3-1 , 1-4) }
3 4 5 T(3,1) = max { T(2,1) , 5 + T(2,-3) }
4 5 6 T(3,1) = max { 0 } { Ignore T(1,-3) }
T(3,1) = 0
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(3, 2)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(3, 2)
We have,
M= 5 n= 4 i = 3, j = 2
(profit)i = (profit)3 = 5
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)3 = 4
1 2 3
Substituting the values, we get-
2 3 4
T(3,2) = max { T(3-1 , 2) , 5 + T(3-1 , 2-4) }
3 4 5 T(3,2) = max { T(2,2) , 5 + T(2,-2) }
4 5 6 T(3,2) = max { 3 } { Ignore T(2,-2) }
T(3,2) = 3
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(3, 2)
We have,
M= 5 n= 4 i = 3, j = 2
(profit)i = (profit)3 = 5
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)3 = 4
1 2 3
Substituting the values, we get-
2 3 4
T(3,2) = max { T(3-1 , 2) , 5 + T(3-1 , 2-4) }
3 4 5 T(3,2) = max { T(2,2) , 5 + T(2,-2) }
4 5 6 T(3,2) = max { 3 } { Ignore T(2,-2) }
T(3,2) = 3
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(3, 3)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(3, 3)
We have,
M= 5 n= 4 i = 3, j = 3
(profit)i = (profit)3 = 5
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)3 = 4
1 2 3
Substituting the values, we get-
2 3 4
T(3,3) = max { T(3-1 , 3) , 5 + T(3-1 , 3-4) }
3 4 5 T(3,3) = max { T(2,3) , 5 + T(2,-1) }
4 5 6 T(3,3) = max { 4 } { Ignore T(2,-1) }
T(3,3) = 4
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(3, 3)
We have,
M= 5 n= 4 i = 3, j = 3
(profit)i = (profit)3 = 5
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)3 = 4
1 2 3
Substituting the values, we get-
2 3 4
T(3,3) = max { T(3-1 , 3) , 5 + T(3-1 , 3-4) }
3 4 5 T(3,3) = max { T(2,3) , 5 + T(2,-1) }
4 5 6 T(3,3) = max { 4 } { Ignore T(2,-1) }
T(3,3) = 4
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(3, 4)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(3, 4)
We have,
M= 5 n= 4 i = 3, j = 4
(profit)i = (profit)3 = 5
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)3 = 4
1 2 3
Substituting the values, we get-
2 3 4
T(3,4) = max { T(3-1 , 4) , 5 + T(3-1 , 4-4) }
3 4 5 T(3,4) = max { T(2,4) , 5 + T(2,0) }
4 5 6 T(3,4) = max { 4, 5 + 0 }
T(3,4) = 5
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(3, 4)
We have,
M= 5 n= 4 i = 3, j = 4
(profit)i = (profit)3 = 5
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)3 = 4
1 2 3
Substituting the values, we get-
2 3 4
T(3,4) = max { T(3-1 , 4) , 5 + T(3-1 , 4-4) }
3 4 5 T(3,4) = max { T(2,4) , 5 + T(2,0) }
4 5 6 T(3,4) = max { 4, 5 + 0 }
T(3,4) = 5
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(3, 5)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(3, 5)
We have,
M= 5 n= 4 i = 3, j = 5
(profit)i = (profit)3 = 5
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)3 = 4
1 2 3
Substituting the values, we get-
2 3 4
T(3,5) = max { T(3-1 , 5) , 5 + T(3-1 , 5-4) }
3 4 5 T(3,5) = max { T(2,5) , 5 + T(2,1) }
4 5 6 T(3,5) = max { 7, 5 + 0 }
T(3,5) = 7
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(3, 5)
We have,
M= 5 n= 4 i = 3, j = 5
(profit)i = (profit)3 = 5
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)3 = 4
1 2 3
Substituting the values, we get-
2 3 4
T(3,5) = max { T(3-1 , 5) , 5 + T(3-1 , 5-4) }
3 4 5 T(3,5) = max { T(2,5) , 5 + T(2,1) }
4 5 6 T(3,5) = max { 7, 5 + 0 }
T(3,5) = 7
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(4, 1)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(4, 1)
We have,
M= 5 n= 4 i = 4, j = 1
(profit)i = (profit)4 = 6
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)4 = 5
1 2 3
Substituting the values, we get-
2 3 4
T(4,1) = max { T(4-1 , 1) , 6 + T(4-1 , 1-5) }
3 4 5 T(4,1) = max { T(3,1) , 6 + T(2,-4) }
4 5 6 T(4,1) = max { 0 } { Ignore T(2,-4) }
T(4,1) = 0
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(4, 1)
We have,
M= 5 n= 4 i = 4, j = 1
(profit)i = (profit)4 = 6
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)4 = 5
1 2 3
Substituting the values, we get-
2 3 4
T(4,1) = max { T(4-1 , 1) , 6 + T(4-1 , 1-5) }
3 4 5 T(4,1) = max { T(3,1) , 6 + T(2,-4) }
4 5 6 T(4,1) = max { 0 } { Ignore T(2,-4) }
T(4,1) = 0
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(4, 2)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(4, 2)
We have,
M= 5 n= 4 i = 4, j = 2
(profit)i = (profit)4 = 6
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)4 = 5
1 2 3
Substituting the values, we get-
2 3 4
T(4,1) = max { T(4-1 , 2) , 6 + T(4-1 , 2-5) }
3 4 5 T(4,1) = max { T(3,2) , 6 + T(2,-3) }
4 5 6 T(4,1) = max { 3 } { Ignore T(2,-3) }
T(4,1) = 3
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(4, 2)
We have,
M= 5 n= 4 i = 4, j = 2
(profit)i = (profit)4 = 6
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)4 = 5
1 2 3
Substituting the values, we get-
2 3 4
T(4,1) = max { T(4-1 , 2) , 6 + T(4-1 , 2-5) }
3 4 5 T(4,1) = max { T(3,2) , 6 + T(2,-3) }
4 5 6 T(4,1) = max { 3 } { Ignore T(2,-3) }
T(4,1) = 3
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(4, 3)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(4, 3)
We have,
M= 5 n= 4 i = 4, j = 3
(profit)i = (profit)4 = 6
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)4 = 5
1 2 3
Substituting the values, we get-
2 3 4
T(4,1) = max { T(4-1 , 3) , 6 + T(4-1 , 3-5) }
3 4 5 T(4,1) = max { T(3, 3) , 6 + T(2,-2) }
4 5 6 T(4,1) = max { 4 } { Ignore T(2,-2) }
T(4,1) = 4
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(4, 3)
We have,
M= 5 n= 4 i = 4, j = 3
(profit)i = (profit)4 = 6
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)4 = 5
1 2 3
Substituting the values, we get-
2 3 4
T(4,1) = max { T(4-1 , 3) , 6 + T(4-1 , 3-5) }
3 4 5 T(4,1) = max { T(3, 3) , 6 + T(2,-2) }
4 5 6 T(4,1) = max { 4 } { Ignore T(2,-2) }
T(4,1) = 4
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(4, 4)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(4, 4)
We have,
M= 5 n= 4 i = 4, j = 4
(profit)i = (profit)4 = 6
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)4 = 5
1 2 3
Substituting the values, we get-
2 3 4
T(4,1) = max { T(4-1 , 4) , 6 + T(4-1 , 4-5) }
3 4 5 T(4,1) = max { T(3, 4) , 6 + T(2,-1) }
4 5 6 T(4,1) = max { 5 } { Ignore T(2,-1) }
T(4,1) = 5
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(4, 4)
We have,
M= 5 n= 4 i = 4, j = 4
(profit)i = (profit)4 = 6
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)4 = 5
1 2 3
Substituting the values, we get-
2 3 4
T(4,1) = max { T(4-1 , 4) , 6 + T(4-1 , 4-5) }
3 4 5 T(4,1) = max { T(3, 4) , 6 + T(2,-1) }
4 5 6 T(4,1) = max { 5 } { Ignore T(2,-1) }
T(4,1) = 5
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(4, 5)
M= 5 n= 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(4, 5)
We have,
M= 5 n= 4 i = 4, j = 5
(profit)i = (profit)4 = 6
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)4 = 5
1 2 3
Substituting the values, we get-
2 3 4
T(4,1) = max { T(4-1 , 5) , 6 + T(4-1 , 5-5) }
3 4 5 T(4,1) = max { T(3, 5) , 6 + T(2, 0) }
4 5 6 T(4,1) = max { 7, 6 + 0 }
T(4,1) = 7
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5
0/1 knapsack Problem : Tabular representation
Step-02: Finding T(4, 5)
We have,
M= 5 n= 4 i = 4, j = 5
(profit)i = (profit)4 = 6
Item(i) Weight(Wi) Profit(Pi) (weight)i = (weight)4 = 5
1 2 3
Substituting the values, we get-
2 3 4
T(4,1) = max { T(4-1 , 5) , 6 + T(4-1 , 5-5) }
3 4 5 T(4,1) = max { T(3, 5) , 6 + T(2, 0) }
4 5 6 T(4,1) = max { 7, 6 + 0 }
T(4,1) = 7
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 Consider last tuple of table
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 Consider last tuple of table . This is maximum profit
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 7 belongs to column 4
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 7 belongs to column 4
Item(i) Weight(Wi) Profit(Pi) Check whether 7 belongs to column 3
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 7 belongs to column 4
Item(i) Weight(Wi) Profit(Pi) 7 belongs to column 3
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 7 belongs to column 4
Item(i) Weight(Wi) Profit(Pi) 7 belongs to column 3
1 2 3
Check whether 7 belongs to column 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 7 belongs to column 4
Item(i) Weight(Wi) Profit(Pi) 7 belongs to column 3
1 2 3
7 belongs to column 2
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 7 belongs to column 4
Item(i) Weight(Wi) Profit(Pi) 7 belongs to column 3
1 2 3
7 belongs to column 2
2 3 4
3 4 5 Check whether 7 belongs to column 1
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 7 belongs to column 4
Item(i) Weight(Wi) Profit(Pi) 7 belongs to column 3
1 2 3
7 belongs to column 2
2 3 4
3 4 5 7 does not belongs to column 1
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 7 belongs to column 4
Item(i) Weight(Wi) Profit(Pi) 7 belongs to column 3
1 2 3
7 belongs to column 2
2 3 4
3 4 5 So this profit is due to item2
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 So this profit 7 is due to item2
Item(i) Weight(Wi) Profit(Pi) Subtract profit of item2 from 7 = 7 – 4 = 3
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 Item2
Item(i) Weight(Wi) Profit(Pi) Check profit 3 in column 2
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 Item2
Item(i) Weight(Wi) Profit(Pi) Profit 3 belongs to column 2
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 Item2
Item(i) Weight(Wi) Profit(Pi) Profit 3 belongs to column 2
1 2 3
Check Profit 3 belongs to column 1
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 Item2
Item(i) Weight(Wi) Profit(Pi) Profit 3 belongs to column 2
1 2 3
Profit 3 does not belongs to column 1
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 Item2
Item(i) Weight(Wi) Profit(Pi) So Profit 3 belongs to item 1
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Tabular representation
Step-03: Tracing item
M= 5 n= 4 Item2, Item1 = Profit is 7
Item(i) Weight(Wi) Profit(Pi)
1 2 3
2 3 4
3 4 5
4 5 6
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
0/1 knapsack Problem : Time Complexity
Given a set of cities and distance between every pair of cities, the problem is to find the shortest
possible route that visits every city exactly once and returns to the starting point.
𝑔 ⅈ, 𝑠 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑠 − 𝑗
𝑗∈𝑠
Travelling Salesperson problem
1 2
Source vertex is 1. We have to find the shortest possible route that visits
every vertex exactly once and returns to the source vertex.
4 3
𝑔 ⅈ, 𝑠 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑠 − 𝑗
𝑗∈𝑠
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1
4 3
2 3 4
1 2 3 4 3 4 2 4 3 2
1 0 10 15 20
2 5 0 9 10 4 3 4 2 2 3
3 6 13 0 12
4 8 8 9 0 1 1 1 1 1 1
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1 g(1,g{2,3,4})
4 3
2 g(C12,g{3,4}) 3 g(C13,g{2,4}) 4 g(C14,g{2,3})
g(C23,g{4}) g(C24,g{3}) g(C34,g{2}) g(C42,g{3})
1 2 3 4 3 4 2
g(C32,g{4})
4 3
g(C43,g{2})
2
1 0 10 15 20
2 5 0 9 10 4g(ϕ)) g(C43,
g(C34, 3g(ϕ)) 4 2 2 3
g(C24,g(ϕ)) g(C42,g(ϕ)) g(C34,g(ϕ)) g(C24,g(ϕ))
3 6 13 0 12
4 8 8 9 0 1 1 1 1 1 1
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
|S| = ϕ
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1
4 3
2 3 4
1 2 3 4 3 4 2 4 3 2
1 0 10 15 20
2 5 0 9 10 4 3 4 2 2 3
3 6 13 0 12
4 8 8 9 0 1 1 1 1 1 1
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
Step 1: |S| = ϕ
4 3
g(2, ϕ) = C21 = 5
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1
4 3
2 3 4
1 2 3 4 3 4 2 4 3 2
1 0 10 15 20
2 5 0 9 10 4 3 4 2 2 3
3 6 13 0 12
4 8 8 9 0 1 1 1 1 1 1
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
Step 1: |S| = ϕ
4 3
g(2, ϕ) = C21 = 5 g(3 ϕ) = C31 = 6
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1
4 3
2 3 4
1 2 3 4 3 4 2 4 3 2
1 0 10 15 20
2 5 0 9 10 4 3 4 2 2 3
3 6 13 0 12
4 8 8 9 0 1 1 1 1 1 1
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
Step 1: |S| = ϕ
4 3
g(2, ϕ) = C21 = 5 g(3 ϕ) = C31 = 6 g(4 ϕ) = C41 = 8
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1
4 3
2 3 4
1 2 3 4 3 4 2 4 3 2
1 0 10 15 20
2 5 0 9 10 4 3 4 2 2 3
3 6 13 0 12
4 8 8 9 0 1 1 1 1 1 1
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(2, {3}) = min {C23 + g(3, ϕ)
3 6 13 0 12
Step 2: |S| = 3 = min {9 + 6 }
4 8 8 9 0
=15
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
4 3 g(2,{3}) = 15
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(2, {3}) = min {C23 + g(3, ϕ)
3 6 13 0 12
Step 2: |S| = 3 = min {9 + 6 }
4 8 8 9 0
=15
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1
4 3
2 3 4
1 2 3 4 3 4 2 4 3 2
1 0 10 15 20
2 5 0 9 10 4 3 4 2 2 3
3 6 13 0 12
4 8 8 9 0 1 1 1 1 1 1
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
4 3 g(2,{3}) = 15
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
Step 2: |S| = 4
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
4 3 g(2,{3}) = 15
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(2, {4}) = min {C24 + g(4, ϕ)
3 6 13 0 12
Step 2: |S| = 4
4 8 8 9 0 = min {10 + 8 }
=18
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
4 3 g(2,{3}) = 15
g(2,{4}) = 18
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(2, {4}) = min {C24 + g(4, ϕ)
3 6 13 0 12
Step 2: |S| = 4
4 8 8 9 0 = min {10 + 8 }
=18
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
4 3 g(2,{3}) = 15
g(2,{4}) = 18
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1
4 3
2 3 4
1 2 3 4 3 4 2 4 3 2
1 0 10 15 20
2 5 0 9 10 4 3 4 2 2 3
3 6 13 0 12
4 8 8 9 0 1 1 1 1 1 1
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
4 3 g(2,{3}) = 15
g(2,{4}) = 18
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
Step 2: |S| = 2
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
4 3 g(2,{3}) = 15
g(2,{4}) = 18
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(3, {2}) = min {C32 + g(2, ϕ)
3 6 13 0 12
Step 2: |S| = 2
4 8 8 9 0 = min {13 + 7}
=18
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{3}) = 15 g(3,{2}) = 18
4 3
g(2,{4}) = 18
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(3, {2}) = min {C32 + g(2, ϕ)
3 6 13 0 12
Step 2: |S| = 2
4 8 8 9 0 = min {13 + 5 }
=18
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1
4 3
2 3 4
1 2 3 4 3 4 2 4 3 2
1 0 10 15 20
2 5 0 9 10 4 3 4 2 2 3
3 6 13 0 12
4 8 8 9 0 1 1 1 1 1 1
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{3}) = 15 g(3,{2}) = 18
4 3
g(2,{4}) = 18
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
Step 2: |S| = 4
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{3}) = 15 g(3,{2}) = 18
4 3
g(2,{4}) = 18
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(3, {4}) = min {C34 + g(4, ϕ)
3 6 13 0 12
Step 2: |S| = 4
4 8 8 9 0 = min {12 + 8 }
=20
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{3}) = 15 g(3,{2}) = 18
4 3
g(2,{4}) = 18 g(3,{4}) = 20
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(3, {4}) = min {C34 + g(4, ϕ)
3 6 13 0 12
Step 2: |S| = 4
4 8 8 9 0 = min {12 + 8 }
=20
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{3}) = 15 g(3,{2}) = 18
4 3
g(2,{4}) = 18 g(3,{4}) = 20
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1
4 3
2 3 4
1 2 3 4 3 4 2 4 3 2
1 0 10 15 20
2 5 0 9 10 4 3 4 2 2 3
3 6 13 0 12
4 8 8 9 0 1 1 1 1 1 1
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{3}) = 15 g(3,{2}) = 18
4 3
g(2,{4}) = 18 g(3,{4}) = 20
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
Step 2: |S| = 2
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{3}) = 15 g(3,{2}) = 18
4 3
g(2,{4}) = 18 g(3,{4}) = 20
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(4, {2}) = min {C42 + g(2, ϕ)
3 6 13 0 12
Step 2: |S| = 2
4 8 8 9 0 = min {8 + 5 }
=13
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{4}) = 18 g(3,{4}) = 20
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(4, {2}) = min {C42 + g(2, ϕ)
3 6 13 0 12
Step 2: |S| = 2
4 8 8 9 0 = min {8 + 5 }
=13
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1
4 3
2 3 4
1 2 3 4 3 4 2 4 3 2
1 0 10 15 20
2 5 0 9 10 4 3 4 2 2 3
3 6 13 0 12
4 8 8 9 0 1 1 1 1 1 1
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{4}) = 18 g(3,{4}) = 20
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
Step 2: |S| = 2
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{4}) = 18 g(3,{4}) = 20
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(4, {3}) = min {C43 + g(3, ϕ)
3 6 13 0 12
Step 2: |S| = 2
4 8 8 9 0 = min {9 + 6 }
=15
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(4, {3}) = min {C43 + g(3, ϕ)
3 6 13 0 12
Step 2: |S| = 2
4 8 8 9 0 = min {9 + 6 }
=15
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1 2 3 4
Step 2: we have to find out the distance
1 0 10 15 20 between vertex 1 and vertex { 2, 3, 4} with two
intermediate vertex
2 5 0 9 10
3 6 13 0 12
Step 2: |S| = 2
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
Step 2: |S| =
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1
4 3
2 3 4
1 2 3 4 3 4 2 4 3 2
1 0 10 15 20
2 5 0 9 10 4 3 4 2 2 3
3 6 13 0 12
4 8 8 9 0 1 1 1 1 1 1
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
Step 2: |S| = 3,4
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(2, {3,4}) = min {C23 + g(3, {4}) , C24 + g(4, {3})
3 6 13 0 12
Step 2: |S| = 3,4 = min{9 + 20 , 10+15}
4 8 8 9 0
= min{29, 25} = 25
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{3,4}) = 25
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(2, {3,4}) = min {C23 + g(3, {4}) , C24 + g(4, {3})
3 6 13 0 12
Step 2: |S| = 3,4 = min{9 + 20 , 10+15}
4 8 8 9 0
= min{29, 25} = 25
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1
4 3
2 3 4
1 2 3 4 3 4 2 4 3 2
1 0 10 15 20
2 5 0 9 10 4 3 4 2 2 3
3 6 13 0 12
4 8 8 9 0 1 1 1 1 1 1
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{3,4}) = 25
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
Step 2: |S| = 2,4
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{3,4}) = 25
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(3, {2,4}) = min {C32 + g(2, {4}) , C34 + g(4, {2})
3 6 13 0 12
Step 2: |S| = 2,4 = min{13 + 18 , 12+13}
4 8 8 9 0
= min{31, 25} = 25
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{3,4}) = 25 g(3,{2,4}) = 25
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(3, {2,4}) = min {C32 + g(2, {4}) , C34 + g(4, {2})
3 6 13 0 12
Step 2: |S| = 2,4 = min{13 + 18 , 12+13}
4 8 8 9 0
= min{31, 25} = 25
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{3,4}) = 25 g(3,{2,4}) = 25
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
Step 2: |S| = 2,3
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1
4 3
2 3 4
1 2 3 4 3 4 2 4 3 2
1 0 10 15 20
2 5 0 9 10 4 3 4 2 2 3
3 6 13 0 12
4 8 8 9 0 1 1 1 1 1 1
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{3,4}) = 25 g(3,{2,4}) = 25
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
Step 2: |S| = 2,3
4 8 8 9 0
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
g(2,{3,4}) = 25 g(3,{2,4}) = 25
1 2 3 4
1 0 10 15 20
2 5 0 9 10 g(4, {2,3}) = min {C42 + g(2, {3}) , C43 + g(3, {2})
3 6 13 0 12
Step 2: |S| = 2,3 = min{8 + 15 , 9+18}
4 8 8 9 0
= min{23, 27} = 23
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
1
4 3
2 3 4
1 2 3 4 3 4 2 4 3 2
1 0 10 15 20
2 5 0 9 10 4 3 4 2 2 3
3 6 13 0 12
4 8 8 9 0 1 1 1 1 1 1
Travelling Salesperson problem
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
10
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
10
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
10
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
10
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
10
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
10
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
10
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
10
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
10
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
10
𝑔 ⅈ, 𝑆 = min 𝑐𝑖𝑗 + 𝑔 𝑗, 𝑆 − 𝑗
1 2 𝑗∈𝑆
Common Subsequence
➢ Suppose, X and Y are two sequences over a finite set of elements. We can say that Z is a
common subsequence of X and Y, if Z is a subsequence of both X and Y.
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi
1 A
2 B
3 C
4 B
5 D
6 A
7 B
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi
1 A
2 B
c[i, 0] = 0
3 C
4 B
5 D
6 A
7 B
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0
1 A 0
2 B 0
c[i, 0] = 0
3 C 0
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0
1 A 0
2 B 0
c[0, j] = 0
3 C 0
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
2 B 0
c[0, j] = 0
3 C 0
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
2 B 0
Check xi and yj
3 C 0
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
2 B 0
Check xi and yj
3 C 0
A≠B
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
2 B 0
Check xi and yj
3 C 0
A≠B
4 B 0
c[i -1, j] ≥ c[i, j - 1]
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0
2 B 0
Check xi and yj
3 C 0
A≠B
4 B 0
c[i -1, j] ≥ c[i, j - 1]
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0
2 B 0
Check xi and yj
3 C 0
A≠B
4 B 0
c[i -1, j] ≥ c[i, j - 1]
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0
2 B 0
Check xi and yj
3 C 0
A≠D
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0
2 B 0
Check xi and yj
3 C 0
A≠D
4 B 0
c[i -1, j] ≥ c[i, j - 1]
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0
2 B 0
Check xi and yj
3 C 0
A≠D
4 B 0
c[i -1, j] ≥ c[i, j - 1]
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0
2 B 0
Check xi and yj
3 C 0
A≠C
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0
2 B 0
Check xi and yj
3 C 0
A≠C
4 B 0
c[i -1, j] ≥ c[i, j - 1]
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0
2 B 0
Check xi and yj
3 C 0
A≠C
4 B 0
c[i -1, j] ≥ c[i, j - 1]
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0
2 B 0
Check xi and yj
3 C 0
A≠C
4 B 0
c[i -1, j] ≥ c[i, j - 1]
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0
2 B 0
Check xi and yj
3 C 0
A=A
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0
2 B 0
Check xi and yj
3 C 0
A=A
4 B 0
c[i, j] = c[i -1, j -1] +1
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1
2 B 0
Check xi and yj
3 C 0
A=A
4 B 0
c[i, j] = c[i -1, j -1] +1
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1
2 B 0
Check xi and yj
3 C 0
A=A
4 B 0
c[i, j] = c[i -1, j -1] +1
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1
2 B 0
Check xi and yj
3 C 0
A≠B
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1
2 B 0
Check xi and yj
3 C 0
A≠B
4 B 0
c[i -1, j] < c[i, j - 1]
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1
2 B 0
Check xi and yj
3 C 0
A≠B
4 B 0
c[i -1, j] < c[i, j - 1]
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1
2 B 0
Check xi and yj
3 C 0
A≠B
4 B 0
c[i -1, j] < c[i, j - 1]
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1
2 B 0
Check xi and yj
3 C 0
A=A
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1
2 B 0
Check xi and yj
3 C 0
A=A
4 B 0
c[i -1, j - 1] +1
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
Check xi and yj
3 C 0
A=A
4 B 0
c[i -1, j - 1] +1
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
Check xi and yj
3 C 0
A=A
4 B 0
c[i -1, j - 1] +1
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1
3 C 0
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1
3 C 0
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1
3 C 0
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1
3 C 0
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2
3 C 0
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2 3
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2 3 4
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2 3 4 4
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
Consider Last
2 B 0 Cell It is size of
1 1 1 1 2 2
longest
3 C 0 common
1 1 2 2 2 2
subsequence
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2 3 4 4
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
Follow the
2 B 0 arrow direction
1 1 1 1 2 2
in bottom to
3 C 0 top direction
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2 3 4 4
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0
0 0 0 1 1 1
Follow the
2 B 0 arrow direction
1 1 1 1 2 2
in bottom to
3 C 0 top direction
1 1 2 2 2 2
(one cell)
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2 3 4 4
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0 Each “ ” on the
0 0 0 1 1 1
shaded sequence
2 B 0 corresponds
1 1 1 1 2 2
to an entry
3 C 0 (highlighted) for which
1 1 2 2 2 2
xi D yj is a member of
4 B 0 an LCS.
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2 3 4 4
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0 Each “ ” on the
0 0 0 1 1 1
shaded sequence
2 B 0 corresponds
1 1 1 1 2 2
to an entry
3 C 0 (highlighted) for which
1 1 2 2 2 2
xi D yj is a member of
4 B 0 an LCS.
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2 3 4 4
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0 Each “ ” on the
0 0 0 1 1 1
shaded sequence
2 B 0 corresponds
1 1 1 1 2 2
to an entry
3 C 0 (highlighted) for which
1 1 2 2 2 2
xi D yj is a member of
4 B 0 an LCS.
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2 3 4 4
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0 Each “ ” on the
0 0 0 1 1 1
shaded sequence
2 B 0 corresponds
1 1 1 1 2 2
to an entry
3 C 0 (highlighted) for which
1 1 2 2 2 2
xi D yj is a member of
4 B 0 an LCS.
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2 3 4 4
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0 Each “ ” on the
0 0 0 1 1 1
shaded sequence
2 B 0 corresponds
1 1 1 1 2 2
to an entry
3 C 0 (highlighted) for which
1 1 2 2 2 2
xi D yj is a member of
4 B 0 an LCS.
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2 3 4 4
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0 Each “ ” on the
0 0 0 1 1 1
shaded sequence
2 B 0 corresponds
1 1 1 1 2 2
to an entry
3 C 0 (highlighted) for which
1 1 2 2 2 2
xi D yj is a member of
4 B 0 an LCS.
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2 3 4 4
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0 Each “ ” on the
0 0 0 1 1 1
shaded sequence
2 B 0 corresponds
1 1 1 1 2 2
to an entry
3 C 0 (highlighted) for which
1 1 2 2 2 2
xi D yj is a member of
4 B 0 an LCS.
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2 3 4 4
Longest common subsequence
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0 Longest common
0 0 0 1 1 1
subsequence is “ BCBA”
2 B 0
1 1 1 1 2 2
3 C 0
1 1 2 2 2 2
4 B 0
1 1 2 2 3 3
5 D 0
1 2 2 2 3 3
6 A 0
1 2 2 3 3 4
7 B 0
1 2 2 3 4 4
Longest common subsequence : Time Complexity
LCS-LENGTH(X, Y ) PRINT-LCS(b,X, i, j )
1. m = [Link] 1. if i == 0 or j == 0
2. n = [Link] 2. return
3. let b[1 .. m , 1 .. n] and c[0 .. m , 0 .. n ]be new tables 3. if b[i, j] == “ ”
4. for i = 1 to m 4. PRINT-LCS(b, X, i -1, j – 1)
5. c[i, 0] = 0 5. print xi
6. for j = 0 to n 6. elseif b[i, j ] == “ ”
7. c[0, j] = 0 7. PRINT-LCS(b, X, i -1, j )
8. for i = 1 to m 8. else PRINT-LCS(b, X, I, j -1)
9. for j = 1 to n
10. if xi == yj
11. c[i; j] = c[i -1, j -1] +1
12. b[I, j] = “ “
13. elseif c[i -1, j] ≥ c[i, j - 1] Time Complexity : O(mn)
14. c[i, j ] = c[i-1, j]
15. b[i, j] D “ “
16. else c[i, j] = c[i, j- 1]
17. b[i, j] “ ”
18. return c and b
Longest common subsequence : Space Complexity
LCS-LENGTH(X, Y ) PRINT-LCS(b,X, i, j )
1. m = [Link] 1. if i == 0 or j == 0
2. n = [Link] 2. return
3. let b[1 .. m , 1 .. n] and c[0 .. m , 0 .. n ]be new tables 3. if b[i, j] == “ ”
4. for i = 1 to m 4. PRINT-LCS(b, X, i -1, j – 1)
5. c[i, 0] = 0 5. print xi
6. for j = 0 to n 6. elseif b[i, j ] == “ ”
7. c[0, j] = 0 7. PRINT-LCS(b, X, i -1, j )
8. for i = 1 to m 8. else PRINT-LCS(b, X, I, j -1)
9. for j = 1 to n
10. if xi == yj
11. c[i; j] = c[i -1, j -1] +1
12. b[I, j] = “ “
13. elseif c[i -1, j] ≥ c[i, j - 1] Time Complexity : O(mn)
14. c[i, j ] = c[i-1, j]
15. b[i, j] D “ “
16. else c[i, j] = c[i, j- 1]
17. b[i, j] “ ”
18. return c and b