Dynamic Programing
Dynamic Programing
Dynamic Programming
4/8/2024 1
Approaches for solving DP Problems
4
Bottom-Up vs. Top Down
• There are two versions of dynamic programming.
• Bottom-up. (tabular approaches)
• Top-down (or memorization).
• Bottom-up:
• Iterative, solves problems in sequence, from smaller to bigger.
• Top-down:
• Recursive, start from the larger problem, solve smaller problems as needed.
• For any problem that we solve, store the solution, so we never have to compute the same
solution twice.
• This approach is also called memoization.
5
Top-Down Dynamic Programming
( Memoization )
• Maintain an array/table where solutions to problems
can be saved.
• To solve a problem P:
• See if the solution has already been stored in the array.
• If yes, return the solution.
• Else:
• Issue recursive calls to solve whatever smaller problems we need
to solve.
• Using those solutions obtain the solution to problem P.
• Store the solution in the solutions array.
• Return the solution.
6
Example on Fibonacci series calculation
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,…………..
Number 0 1 1 3 5 8 13
Array index 0 1 2 3 4 5 6
{
0 if n=0
Fib(n) = 1 if n=1
Fib(5)
Fib(3) Fib(4)
Fib(2) Fib(3)
Fib(1) Fib(2)
Fib(0) Fib(1)
Example on Fibonacci series calculation
If somebody wants to find Fib(5), then we can do using the dynamic
programming
Int fib(int n)
{
If (n<=1)
return n;
Else
Return fib(n-2)+fib(n-1);
}
0/1 Knapsack problem
The 0/1 knapsack problem is a classic optimization problem in
computer science and combinatorial optimization. It's called "0/1"
because for each item, you can either include it in the knapsack
(take it) or exclude it (leave it), but you cannot take a fraction of an
item.
0 0 0 0 0 0 0 0 0 0 0
A 1 1
B 4 3
C 5 4
D 7 5
0/1 Knapsack problem solution
Given data
Total weight limit = 7
Weight
Item Price Weight 0 1 2 3 4 5 6 7
0 0 0 0 0 0 0 0 0 0 0
A 1 1 0 1 1 1 1 1 1 1
B 4 3 0 1 1 4 5 5 5 5
C 5 4 0 1 1 4 5 6 6 9
D 7 5 0 1 1 4 5 7 8 9
0/1 Knapsack problem practice problem
Given data
Total weight limit = 8
Weight
Item Price Weight 0 1 2 3 4 5 6 7 8
A 2 3
B 3 4
C 4 5
D 1 6
0/1 Knapsack problem practice problem solution
Given data, Total weight limit = 8
Weight(w)
Item Price Weight i 0 1 2 3 4 5 6 7 8
0 0 0 0
A 2 3 1
B 3 4 2
C 4 5 3
D 1 6 4
0/1 Knapsack problem practice problem solution
Given data, Total weight limit = 8
Weight(w)
Item Price Weight i 0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0 0 0 0
A 2 3 1 0 0 0 2 2 2 2 2 2
B 3 4 2 0 0 0 2 3 3 3 5 5
C 4 5 3 0 0 0 2 3 4 4 5 6
D 1 6 4 0 0 0 2 3 4 4 5 6
2 6 9
1 3 7 10 12
End
start
4
5 8 11
Multistage graph using dynamic programming
L-2 L-3 L-4
L-1 L-5
4 6
2 6 9
9 2 2 5 4
4
7 3 2
1 3 7 7 10 12
3 11 End
start
2 4 11 5
1 5
6
8
5 8 11
Multistage graph using dynamic programming
Vertex 1 2 3 4 5 6 7 8 9 10 11 12
Cost 0
d 12
9 2 2 5 4
4
1 7 3 7 3 10 2 12
7
3 11 End
start
4
2 11
1 5
6
5 8 8 11
9 2 2 5 4
4
1 7 3 7 3 10 2 12
7
3 11 End
start
4
2 11
1 5
6
Level-3 5 8 8 11
Cost of 3rd stage 6th node = C(3rd stage, 6 node)= min {[C(6,9) + C(4th,9)], [C(6,10)+C(4th ,10)] = min {6+4, 5+2} = 7
Cost of 3rd stage 7th node = C(3rd stage, 7 node)= min {[C(7,9) + C(4th,9)], [C(7,10)+C(4th ,10)] = min {4+4, 3+2} = 5
Cost of 3rd stage 8th node = C(4th stage, 8 node)= min {[C(8,10) + C(4th,10)], [C(8,11)+C(4th ,11)] = min {5+2, 6+5} =7
Multistage graph using dynamic programming
J, l belongs to vertices
Stage or level l belongs to vertice in the next level connected to j
Vertex no.
Multistage graph using dynamic programming
Vertex 1 2 3 4 5 6 7 8 9 10 11 12
Cost 7 5 7 4 2 5 0
d 10 10 10 12 12 12 12
9 2 2 5 4
4
1 7 3 7 3 10 2 12
7
3 11 End
start
4
2 11
1 5
6
Level-3 5 8 8 11
Cost of 3rd stage 6th node = C(3rd stage, 6 node)= min {[C(6,9) + C(4th,9)], [C(6,10)+C(4th ,10)] = min {6+4, 5+2} = 7
Cost of 3rd stage 7th node = C(3rd stage, 7 node)= min {[C(7,9) + C(4th,9)], [C(7,10)+C(4th ,10)] = min {4+4, 3+2} = 5
Cost of 3rd stage 8th node = C(4th stage, 8 node)= min {[C(8,10) + C(4th,10)], [C(8,11)+C(4th ,11)] = min {5+2, 6+5} =7
Multistage graph using dynamic programming
Vertex 1 2 3 4 5 6 7 8 9 10 11 12
Cost 16 7 9 18 15 7 5 7 4 2 5 0
d 2 or 3 7 6 8 8 10 10 10 12 12 12 12
9 2 2 5 4
4
1 7 3 7 3 10 2 12
7
3 11 End
start
4
2 11
1 5
6
Level-3 5 8 8 11
Cost of 3rd stage 6th node = C(3rd stage, 6 node)= min {[C(6,9) + C(4th,9)], [C(6,10)+C(4th ,10)] = min {6+4, 5+2} = 7
Cost of 3rd stage 7th node = C(3rd stage, 7 node)= min {[C(7,9) + C(4th,9)], [C(7,10)+C(4th ,10)] = min {4+4, 3+2} = 5
Cost of 3rd stage 8th node = C(4th stage, 8 node)= min {[C(8,10) + C(4th,10)], [C(8,11)+C(4th ,11)] = min {5+2, 6+5} =7