Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Dynamic Programming

Introduction to
The Design and Analysis of Algorithms

Muhamad Fathurahman
24/03/2020

Disclaimer : Some contents in this slide are obtained from The


Book Introduction to The Design and Analysis of Algorithm 3rd
by Anany Levitin
Outline

• Introduction to Dynamic Programming


• Case Example: Fibonacci by Dynamic Programming
• Solve the Knapsack Problem by Dynamic Programming
Dynamic Programming
What is Dynamic Programming?

• An Algorithm Design Technique


• The term “Programming” stands for “Planning”
• Dynamic Programming is used especially to solve
problems with overlapping subs problems (e.g
Fibonacci, Knapsack Problem, BST, etc )
• Rather than solving overlapping subproblems many
times, dynamic programming suggest solving
overlapping each of the smaller subsproblems only
once and recording the result in a table form.
Fibonacci Problem (Overlapping Overview)
FIB(10)

FIB(9) FIB(8)

FIB(8) FIB(7) FIB(7) FIB(6)

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

Recursive Fibonacci with Dynamic Programming Recursive Fibonacci non-Dynamic Programming

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

public static int Fdbt(int n){ 0 1 2 3 4 5


int array[] = new int [n+1]; -1
0 -1
1 -1
1 -1
2 -1
3 -1
5
array[0] =0;
array[1] =1;
for (int i = 2; i < array.length; i++){
array[i] = array[i-1]+array[i-2];
}
return array[n];
}
Knapsack Problem
Knapsack Problem
Capacity = 20 W 2
1 W = 4.5, V = $20
W = 8.5, V = $100

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

200 3 2 0 0 100 300


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 0 100 200 200 300 300 300 300


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 0 100 200 200 300 300 300 300


3
500 4 0 0 500 600 700

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 0 100 200 200 300 300 300 300


3
500 4 0 0 100 200 500 500 600 700 700

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

200 3 2 0 0 100 200 200 300 300 300 300

500 4 3 0 0 100 200 500 500 600 700 700

600 5 4 0 0 600 700 800


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

200 3 2 0 0 100 200 200 300 300 300 300

500 4 3 0 0 100 200 500 500 600 700 700

600 5 4 0 0 100 200 500 600 600 700 800


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

200 3 2 0 0 100 200 200 300 300 300 300

500 4 3 0 0 100 200 500 500 600 700 700

600 5 4 0 0 100 200 500 600 600 700 800

Generate Items from the table :


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

200 3 2 0 0 100 200 200 300 300 300 300

500 4 3 0 0 100 200 500 500 600 700 700

600 5 4 0 0 100 200 500 600 600 700 800

Generate Items from the table : D Remain = 800-600 = 200


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

200 3 2 0 0 100 200 200 300 300 300 300

500 4 3 0 0 100 200 500 500 600 700 700

600 5 4 0 0 100 200 500 600 600 700 800

Generate Items from the table : D Remain = 800-600 = 200


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

200 3 2 0 0 100 200 200 300 300 300 300

500 4 3 0 0 100 200 500 500 600 700 700

600 5 4 0 0 100 200 500 600 600 700 800

Generate Items from the table : D Remain = 800-600 = 200


Remain = 200
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

200 3 2 0 0 100 200 200 300 300 300 300

500 4 3 0 0 100 200 500 500 600 700 700

600 5 4 0 0 100 200 500 600 600 700 800

Generate Items from the table : D Remain = 200 – 200 = 0


Remain = 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

200 3 2 0 0 100 200 200 300 300 300 300

500 4 3 0 0 100 200 500 500 600 700 700

600 5 4 0 0 100 200 500 600 600 700 800

Generate Items from the table : D,B Remain = 200 – 200 = 0


Remain = 0

You might also like