Vit DP
Vit DP
Dynamic Programming
Problem and Solutions
P
S1 S2 S3 S4 S5 S6
Problem and Solutions
Buy Mobile Phone
•Feasible Solution
• Constraint (8GB RAM & 128GB Storage)
•Optimal Solution
• Objective Function (Minimum Cost)
Min Coins Problem
Notes = { 1, 2, 5, 10, 20, 50, 100, 500, 2000}
• S1 ={ 2000,500}
• S2 ={ 100,100,100,500,500,500}
Solutions • S3 ={ 50,50,50,50,50,50,50}
• S4 ={ 100,100,100,50}
• S5 ={ 100,100,50,50,20,20,10}
Greedy Approach
Notes = { 2000, 500, 100, 50, 20, 10, 5, 2, 1}
Solution = { }
Greedy Approach
Notes = { 2000, 500, 100, 50, 20, 10, 5, 2, 1}
Value = 250
Solution = { 100, }
Greedy Approach
Notes = { 2000, 500, 100, 50, 20, 10, 5, 2, 1}
Value = 150
Value = 50
Value = 0
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Capacity of Knapsack: 15
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Capacity of Knapsack: 15
Profit = 0
Knapsack Problem
Objects 1 2 3 4 5 6 7
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Pi/Wi 5 3.33 3 1.75 8 3 2
X 1
X 1 1
X 1 1 1
X 1 1 1 1
X 1 1 1 1 1
X 1 1 1 1 1 1
Profits (Pi) 1 2 5 6
Weights (Wi) 2 3 4 5
0/1 Knapsack Problem
• M=8 Capacity of Bag
Profits (Pi) 1 2 5 6
Weights (Wi) 2 3 4 5
Solution Xi = 1 0 0 1
max Σ Pi * Xi Σ Wi * Xi <= M
0/1 Knapsack Problem Possible Solutions
0 0 0 0
0 0 0 1
• M=8 Capacity of Bag 0 0 1 0
0 0 1 1
• N=4 Number of Objects 0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
Profits (Pi) 1 2 5 6 1 0 0 0
1 0 0 1
Weights (Wi) 2 3 4 5 1 0 1 0
1 0 1 1
1 1 0 0
Solution Xi = 1 0 0 1
1 1 0 1
1 1 1 0
max Σ Pi * Xi Σ Wi * Xi <= M 1 1 1 1
Total = 2^4 = 16
0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects
0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi)
0
1 2 1
2 3 2
5 4 3
6 5 4
0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects
0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi) 0 0 0 0 0 0 0 0 0
0
1 2 1 0
2 3 2 0
5 4 3 0
6 5 4 0
0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects
0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi) 0 0 0 0 0 0 0 0 0
0
1 2 1 0 0 1 1 1 1 1 1 1
2 3 2 0
5 4 3 0
6 5 4 0
0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects
0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi) 0 0 0 0 0 0 0 0 0
0
1 2 1 0 0 1 1 1 1 1 1 1
2 3 2 0 0 1 2 2 3 3 3 3
5 4 3 0
6 5 4 0
0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects
0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi) 0 0 0 0 0 0 0 0 0
0
1 2 1 0 0 1 1 1 1 1 1 1
2 3 2 0 0 1 2 2 3 3 3 3
5 4 3 0 0 1 2 5 5 6 7 7
6 5 4 0
0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects
0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi) 0 0 0 0 0 0 0 0 0
0
1 2 1 0 0 1 1 1 1 1 1 1
2 3 2 0 0 1 2 2 3 3 3 3
5 4 3 0 0 1 2 5 5 6 7 7
6 5 4 0 0 1 2 5 6 6 7 8
0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi) 0 0 0 0 0 0 0 0 0
0
1 2 1 0 0 1 1 1 1 1 1 1
2 3 2 0 0 1 2 2 3 3 3 3
5 4 3 0 0 1 2 5 5 6 7 7
6 5 4 0 0 1 2 5 6 6 7 8
Solution x1 x2 x3 x4 8-6 = 2
1
0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects
0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi) 0 0 0 0 0 0 0 0 0
0
1 2 1 0 0 1 1 1 1 1 1 1
2 3 2 0 0 1 2 2 3 3 3 3
5 4 3 0 0 1 2 5 5 6 7 7
6 5 4 0 0 1 2 5 6 6 7 8
Solution x1 x2 x3 x4 8-6 = 2
1 0 1 2-2 = 0
0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects
0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi) 0 0 0 0 0 0 0 0 0
0
1 2 1 0 0 1 1 1 1 1 1 1
2 3 2 0 0 1 2 2 3 3 3 3
5 4 3 0 0 1 2 5 5 6 7 7
6 5 4 0 0 1 2 5 6 6 7 8
Solution x1 x2 x3 x4 8-6 = 2
0 1 0 1 2-2 = 0
0/1 Knapsack Problem (Second Method)
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects
1 2
2 3
5 4
6 5
0/1 Knapsack Problem (Second Method)
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects
1 2 S01 = { ( 1 , 2 ) }
2 3
5 4 S1 = { (0 , 0) , ( 1 , 2 )}
6 5
0/1 Knapsack Problem (Second Method)
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects
S0 = { (0 , 0) }
1 2 S1 = { ( 0 , 0 ) , ( 1 , 2 ) }
2 3 S11 = { ( 2 , 3 ) , ( 3 , 5 ) }
5 4
S2 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 )}
6 5
0/1 Knapsack Problem (Second Method)
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects
S0 = { (0 , 0) }
S1 = { (0 , 0) , ( 1 , 2 )}
1 2
S11 = { ( 2 , 3 ) , ( 3 , 5 ) }
2 3
S2 = { ( 0 , 0 ) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) }
5 4 S21 = { ( 5 , 4 ) , ( 6 , 6 ) , ( 7 , 7 ) , ( 8 , 9 ) }
6 5
S3 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 5 , 4 ) , ( 6 , 6 ) , ( 7 , 7 ) , ( 8 , 9 )}
0/1 Knapsack Problem (Second Method)
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects S0 = { (0 , 0) }
S01 = { ( 1 , 2 ) }
S1 = { (0 , 0) , ( 1 , 2 )}
Profits (Pi) Weights (Wi) S11 = { ( 2 , 3 ) , ( 3 , 5 ) }
S2 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 )}
1 2 S21 = { ( 5 , 4 ) , ( 6 , 6 ) , ( 7 , 7 ) , ( 8 , 9 ) }
2 3
5 4
S3 = { ( 0 , 0 ) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 6 , 6 ) , ( 7 , 7 )}
6 5 S31 = { ( 6 , 5 ) , ( 7 , 7 ) , ( 8 , 8 ) , ( 9 , 10 ) , ( 12 , 11 ) , ( 13 , 12 ) }
S4 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 6 , 5 ) , ( 6 , 6 ) , ( 7 , 7 ) , ( 8 , 8 )}
0/1 Knapsack Problem (Second Method)
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects
S0 = { (0 , 0) }
S1 = { (0 , 0) , ( 1 , 2 )}
S2 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 )}
S3 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 6 , 6 ) , ( 7 , 7 )}
S0 = { (0 , 0) }
S1 = { (0 , 0) , ( 1 , 2 )}
S2 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 )}
( 2 , 3 ) belongs to S3 also in S2
S3 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 6 , 6 ) , ( 7 , 7 )}
x3 = 1 (2,3)
S0 = { (0 , 0) }
S1 = { (0 , 0) , ( 1 , 2 )}
( 2 , 3 ) belongs to S3 also in S2 S3 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 6 , 6 ) , ( 7 , 7 )}
x3 = 1 (2,3)
S0 = { (0 , 0) }
( 0 , 0 ) belongs to S1 also in S0
S1 = { (0 , 0) , ( 1 , 2 )}
x1 = 0 (0,0)
( 2 , 3 ) belongs to S3 also in S2 S3 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 6 , 6 ) , ( 7 , 7 )}
x3 = 0 (2,3)
1 2 1
3
0 3 ∞ 7
2
2 8 0 2 ∞
2 7 5
A0 = 3
5 ∞ 0 1
4 3
4
1 2 ∞ ∞ 0
All Pairs Shortest Path 1 2 3 4
1
8 0 3 ∞ 7
1 2 2
8 0 2 ∞
3
A0 = 3
2 7 5
2 5 ∞ 0 1
4
2 ∞ ∞ 0
4 3
1
1 2 3 4
1
0 3 ∞ 7
min { A0[2,3] , A0[2,1]+A0[1,3] } 2
8 0 2
A1 = 3
2 , 8 + ∞ 5 0
4
2 0
All Pairs Shortest Path 1 2 3 4
1
8 0 3 ∞ 7
1 2 2
8 0 2 ∞
3
A0 = 3
2 7 5
2 5 ∞ 0 1
4
2 ∞ ∞ 0
4 3
1
1 2 3 4
1
0 3 ∞ 7
min { A0[2,4] , A0[2,1]+A0[1,4] } 2
8 0 2 15
A1 = 3
∞ , 8 + 7 5 0
4
2 0
All Pairs Shortest Path 1 2 3 4
1
8 0 3 ∞ 7
1 2 2
8 0 2 ∞
3
A0 = 3
2 7 5
2 5 ∞ 0 1
4
2 ∞ ∞ 0
4 3
1
1 2 3 4
1
0 3 ∞ 7
min { A0[3,2] , A0[3,1]+A0[1,2] } 2
8 0 2 15
A1 = 3
∞ , 5 + 3 5 8 0
4
2 0
All Pairs Shortest Path 1 2 3 4
1
8 0 3 ∞ 7
1 2 2
8 0 2 ∞
3
A0 = 3
2 7 5
2 5 ∞ 0 1
4
2 ∞ ∞ 0
4 3
1
1 2 3 4
1
0 3 ∞ 7
min { A0[3,4] , A0[3,1]+A0[1,4] } 2
8 0 2 15
A1 = 3
1 , 5 + 7 5 8 0 1
4
2 0
All Pairs Shortest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[3,4] , A1[3,2]+A1[2,4] } 8 0 2 15
A2 = 3
5 8 0 1
1 , 8 + 15 4
5 0
All Pairs Shortest Path 1 2 3 4
1
8 0 3 ∞ 7
2
1 2 8 0 2 15
3 A1 = 3
2 5 8 0 1
2 7 5
4
2 5 ∞ 0
4 3
1
1 2 3 4
1
0 3 5 7
2
min { A1[4,1] , A1[4,2]+A1[2,1] } 8 0 2 15
A2 = 3
5 8 0 1
2 , 5 + 8 4
2 5 0
All Pairs Shortest Path 1 2 3 4
1
8 0 3 ∞ 7
2
1 2 8 0 2 15
3 A1 = 3
2 5 8 0 1
2 7 5
4
2 5 ∞ 0
4 3
1
1 2 3 4
1
0 3 5 7
2
min { A1[4,3] , A1[4,2]+A1[2,3] } 8 0 2 15
A2 = 3
5 8 0 1
∞ , 5 + 2 4
2 5 7 0
1 2 3 4
All Pairs Shortest Path
1
0 3 5 7
8 2
8 0 2 15
1 2 A2 = 3
3 5 8 0 1
2 4
2 7 5 2 5 7 0
4 3
1
1 2 3 4
1
0 3 5
2
0 2
min { A2[1,2] , A2[1,3]+A2[3,2] } A3 = 3
5 8 0 1
3 , 5 + 8 4
7 0
1 2 3 4
All Pairs Shortest Path
1
0 3 5 7
8 2
8 0 2 15
1 2 A2 = 3
3 5 8 0 1
2 4
2 7 5 2 5 7 0
4 3
1
1 2 3 4
1
0 3 5 6
2
0 2
min { A2[1,4] , A2[1,3]+A2[3,4] } A3 = 3
5 8 0 1
7 , 5 + 1 4
7 0
1 2 3 4
All Pairs Shortest Path
1
0 3 5 7
8 2
8 0 2 15
1 2 A2 = 3
3 5 8 0 1
2 4
2 7 5 2 5 7 0
4 3
1
1 2 3 4
1
0 3 5 6
2
7 0 2
min { A2[2,1] , A2[2,3]+A2[3,1] } A3 = 3
5 8 0 1
8 , 2 + 5 4
7 0
1 2 3 4
All Pairs Shortest Path
1
0 3 5 7
8 2
8 0 2 15
1 2 A2 = 3
3 5 8 0 1
2 4
2 7 5 2 5 7 0
4 3
1
1 2 3 4
1
0 3 5 6
2
7 0 2 3
min { A2[2,4] , A2[2,3]+A2[3,4] } A3 = 3
5 8 0 1
15 , 2 + 1 4
7 0
1 2 3 4
All Pairs Shortest Path
1
0 3 5 7
8 2
8 0 2 15
1 2 A2 = 3
3 5 8 0 1
2 4
2 7 5 2 5 7 0
4 3
1
1 2 3 4
1
0 3 5 6
2
7 0 2 3
min { A2[4,1] , A2[4,3]+A2[3,1] } A3 = 3
5 8 0 1
2 , 7 + 5 4
2 7 0
1 2 3 4
All Pairs Shortest Path
1
0 3 5 7
8 2
8 0 2 15
1 2 A2 = 3
3 5 8 0 1
2 4
2 7 5 2 5 7 0
4 3
1
1 2 3 4
1
0 3 5 6
2
7 0 2 3
min { A2[4,2] , A2[4,3]+A2[3,2] } A3 = 3
5 8 0 1
5 , 7 + 8 4
2 5 7 0
All Pairs Shortest Path 1 2 3 4
1
0 3 5 6
8
2
7 0 2 3
1 2 A3 = 3
3
5 8 0 1
2
4
2 7 5 2 5 7 0
4 3
1
1 2 3 4
1
0 3 6
2
0 3
min { A3[1,2] , A3[1,4]+A3[4,2] } A4 = 3
0 1
3 , 6 + 5 4
2 5 7 0
All Pairs Shortest Path 1 2 3 4
1
0 3 5 6
8
2
7 0 2 3
1 2 A3 = 3
3
5 8 0 1
2
4
2 7 5 2 5 7 0
4 3
1
1 2 3 4
1
0 3 5 6
2
0 3
min { A3[1,3] , A3[1,4]+A3[4,3] } A4 = 3
0 1
5 , 6 + 7 4
2 5 7 0
All Pairs Shortest Path 1 2 3 4
1
0 3 5 6
8
2
7 0 2 3
1 2 A3 = 3
3
5 8 0 1
2
4
2 7 5 2 5 7 0
4 3
1
1 2 3 4
1
0 3 5 6
2
5 0 3
min { A3[2,1] , A3[2,4]+A3[4,1] } A4 = 3
0 1
7 , 3 + 2 4
2 5 7 0
All Pairs Shortest Path 1 2 3 4
1
0 3 5 6
8
2
7 0 2 3
1 2 A3 = 3
3
5 8 0 1
2
4
2 7 5 2 5 7 0
4 3
1
1 2 3 4
1
0 3 5 6
2
5 0 2 3
min { A3[2,3] , A3[2,4]+A3[4,3] } A4 = 3
0 1
2 , 3 + 7 4
2 5 7 0
All Pairs Shortest Path 1 2 3 4
1
0 3 5 6
8
2
7 0 2 3
1 2 A3 = 3
3
5 8 0 1
2
4
2 7 5 2 5 7 0
4 3
1
1 2 3 4
1
0 3 5 6
2
5 0 2 3
min { A3[3,1] , A3[3,4]+A3[4,1] } A4 = 3
3 0 1
5 , 1 + 2 4
2 5 7 0
All Pairs Shortest Path 1 2 3 4
1
0 3 5 6
8
2
7 0 2 3
1 2 A3 = 3
3
5 8 0 1
2
4
2 7 5 2 5 7 0
4 3
1
1 2 3 4
1
0 3 5 6
2
5 0 2 3
min { A3[3,2] , A3[3,4]+A3[4,2] } A4 = 3
3 6 0 1
8 , 1 + 5 4
2 5 7 0
All Pairs Shortest Path
8
1 2 3 4
1
1
3
2
0 3 5 6
2
2 5 0 2 3
2 7 5
A4 = 3
3 6 0 1
4 3
4
1 2 5 7 0