Professional Documents
Culture Documents
Dynamic Programming
Dynamic Programming
Introduction to
The Design and Analysis of Algorithms
Muhamad Fathurahman
24/03/2020
FIB(9) FIB(8)
FIB(6) FIB(5)
Fibonacci Problem With Simple Solution (Recursive)
Solution Ilustration
¿ 𝑁 , 𝑖𝑓 𝑁 =0 𝑜𝑟 𝑁 =1
𝐹 ( 𝑁 )=
{
¿ 𝐹 ( 𝑁 − 2 ) + 𝐹 ( 𝑁 − 1 ) , 𝑖𝑓 𝑁 ≥2
5
Java Code 4 3
public static int F(int n){
if(n == 1 || n== 0)
return 0; 3 2 2 1
else
return F(n-1)+F(n-2);
} 2 1 1 0 1 0
Example
𝐹 ( 5 ) =? 1 0
Fibonacci Problem With Dynamic Programming
Dynamic Programming : “Solve the smaller subproblems once, and record the result in a
table”
𝐹 ( 5 ) =?
5
Index 0 1 2 3 4 5
4 3 Return 2
Values -1
0 -1
1 -1
1 -1
2 3
-1 5
-1
𝐹 ( 5 ) =5 3 2 Return 1
2 1 Return 1
1 0
Comparison of Fibonacci Approach
5
5
4 3
4 3 Return 2
3 2 2 1
3 2 Return 1
2 1 1 0 1 0
2 1 Return 1
1 0
1 0
Bottom-Up Dynamic Programming
Solusi Bottom-Up Solution
3
W = 15.2, V = $180
W = 8.5, V = $120 4
Knapsack Problem Solution With Exhaustive Search
Capacity = 20 W
2
1 W = 4.5, V = $20
W = 8.5, V = $100
Find all subsets and Subset Total Weight Total Value
{} 0 $0
evaluate them
{1} 8,5 $ 100
{2} 4,5 $ 20
{3} 8,5 $ 120
{4} 15,2 $ 180
3
W = 15.2, V = $180 {1,2} 13 $ 120
{1,3} 17 $ 220
W = 8.5, V = $120 4 {1,4} 23,7 Not Feasible
{2,3} 13 $ 140
{2,4} 19,7 $ 200
{3,4} 23,7 Not Feasible
{1,2,3} 21,5 Not Feasible
{1,2,4} 28,2 Not Feasible
{1,3,4} 32,2 Not Feasible
{2,3,4} 28,2 Not Feasible
{1,2,3,4} 36,7 Not Feasible
Knapsack Problem Solution With Dynamic Programming
2 Capacity = 8 W
1 W = 3, V = $200
W = 2, V = $100
3
W = 5, V = $600
W = 4, V = $500 4
Knapsack Problem Solution With Dynamic Programming
Capacity = 8 W
Sack Capacity
0 1 2 3 4 5 6 7 8
Values W 0
100 2 1
Items
200 3 2
500 4 3
600 5 4
Knapsack Problem Solution With Dynamic Programming
Capacity = 8 W
Sack Capacity
0 1 2 3 4 5 6 7 8
Values W 0
0 0 0 0 0 0 0 0 0
1
100 2 0
Items
200 3 2 0
3
500 4 0
600 5 4 0
Knapsack Problem Solution With Dynamic Programming
Capacity = 8 W
Sack Capacity
0 1 2 3 4 5 6 7 8
Values W 0
0 0 0 0 0 0 0 0 0
1 0
100 2 0 100
Items
200 3 2 0
3
500 4 0
600 5 4 0
Knapsack Problem Solution With Dynamic Programming
Capacity = 8 W
Sack Capacity
0 1 2 3 4 5 6 7 8
Values W 0
0 0 0 0 0 0 0 0 0
1 0
100 2 0 100 100 100 100 100 100 100
Items
200 3 2 0
3
500 4 0
600 5 4 0
Knapsack Problem Solution With Dynamic Programming
Capacity = 8 W
Sack Capacity
0 1 2 3 4 5 6 7 8
Values W 0 0 0 0 0 0 0 0 0 0
1 0
100 2 0 100 100 100 100 100 100 100
Items
600 5 4 0
Knapsack Problem Solution With Dynamic Programming
Capacity = 8 W
Sack Capacity
0 1 2 3 4 5 6 7 8
Values W 0 0 0 0 0 0 0 0 0 0
1 0
100 2 0 100 100 100 100 100 100 100
Items
600 5 4 0
Knapsack Problem Solution With Dynamic Programming
Capacity = 8 W
Sack Capacity
0 1 2 3 4 5 6 7 8
Values W 0 0 0 0 0 0 0 0 0 0
1 0
100 2 0 100 100 100 100 100 100 100
Items
600 5 4 0
Knapsack Problem Solution With Dynamic Programming
Capacity = 8 W
Sack Capacity
0 1 2 3 4 5 6 7 8
Values W 0 0 0 0 0 0 0 0 0 0
1 0
100 2 0 100 100 100 100 100 100 100
Items
600 5 4 0
Knapsack Problem Solution With Dynamic Programming
Capacity = 8 W
Sack Capacity
0 1 2 3 4 5 6 7 8
Values W 0 0 0 0 0 0 0 0 0 0
0
100 2 1 0 100 100 100 100 100 100 100
Items
Sack Capacity
0 1 2 3 4 5 6 7 8
Values W 0 0 0 0 0 0 0 0 0 0
0
100 2 1 0 100 100 100 100 100 100 100
Items
Sack Capacity
0 1 2 3 4 5 6 7 8
Values W 0 0 0 0 0 0 0 0 0 0
0
100 2 1 0 100 100 100 100 100 100 100
Items
Sack Capacity
0 1 2 3 4 5 6 7 8
Values W 0 0 0 0 0 0 0 0 0 0
0
100 2 1 0 100 100 100 100 100 100 100
Items
Sack Capacity
0 1 2 3 4 5 6 7 8
Values W 0 0 0 0 0 0 0 0 0 0
0
100 2 1 0 100 100 100 100 100 100 100
Items
Sack Capacity
0 1 2 3 4 5 6 7 8
Values W 0 0 0 0 0 0 0 0 0 0
0
100 2 1 0 100 100 100 100 100 100 100
Items
Sack Capacity
0 1 2 3 4 5 6 7 8
Values W 0 0 0 0 0 0 0 0 0 0
0
100 2 1 0 100 100 100 100 100 100 100
Items
Sack Capacity
0 1 2 3 4 5 6 7 8
Values W 0 0 0 0 0 0 0 0 0 0
0
100 2 1 0 100 100 100 100 100 100 100
Items