C10 - Dynamic Programming
C10 - Dynamic Programming
DYNAMIC PROGRAMMING
24/05/2023
24/05/2023
1 4
S A B 5
T
5 6
5
C 2
F
Dynamic Programming
• Dynamic Programming is a technique that can
be used to solve many optimization problems. In
most applications, dynamic programming
obtains solutions by working backward from the
end of problem toward the beginning, the
breaking up a large, unwieldy problem into a
series of smaller, more tractable problems.
5
Compare DP & LP
24/05/2023
LP DP
Algorithm Can programming Cannot programming.
(Simplex)
TYPES OF DP PROBLEMS
2 types of DP:
- Network:
Ex: Shortest path problem
- Non – network:
Ex: Knapsack problem.
8
24/05/2023
• d(J, H) = 3
• d(J, I) = 4
24/05/2023
24/05/2023
24/05/2023
11 4
11 7 7 0
8 6
KNAPSACK PROBLEM
15
16
24/05/2023
24/05/2023
24/05/2023
Example
Let’s run our algorithm on the following data:
n = 4 (# of elements)
W = 5 (max weight)
Elements (weight, benefit):
(2,3),
(3,4),
(4,5),
(5,6)
How to pack the knapsack to achieve maximum total value of packed items?
20
24/05/2023
Example (3)
i\W 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
21
24/05/2023
Items:
Example (4) 1: (2,3)
i\W 2: (3,4)
0 1 2 3 4 5
0 0 0 0 0 0 0
3: (4,5)
i=1 4: (5,6)
1 0 0 bi=3
2 0
wi=2
3 0
w=1
4 0
w-wi =-1
V [i 1, w] if wi w
V [i, w]
max{V [i 1, w], V [i 1, w wi ] bi } else
V [1,1] V [1 1,1] 0
22
Items:
24/05/2023
1: (2,3)
2: (3,4)
Example (5) 3: (4,5)
i\W 0 1 2 3 4 5 i=1 4: (5,6)
0 0 0 0 0 0 0
bi=3
1 0 0 3
wi=2
2 0
3 0 w=2
4 0 w-wi =0
V [i 1, w] if wi w
V [i, w]
max{V [i 1, w], V [i 1, w wi ] bi } else
V [1, 2] max{V [1 1, 2], V [1 1, 2 2] 3}
3
23
24/05/2023
Items:
1: (2,3)
Example (6) 2: (3,4)
i\W 0 1 2 3 4 5 3: (4,5)
0 0 0 0 0 0 0 i=1 4: (5,6)
1 0 0 3 3 bi=3
2 0 wi=2
3 0 w=3
4 0 w-wi =1
V [i 1, w] if wi w
V [i, w]
max{V [i 1, w], V [i 1, w wi ] bi } else
V [1,3] max{V [1 1,3], V [1 1,3 2] 3}
max{0,3} 3
24
Items:
24/05/2023
1: (2,3)
Example (7) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=1 4: (5,6)
0 0 0 0 0 0 0
bi=3
1 0 0 3 3 3
wi=2
2 0
3 0 w=4
4 0 w-wi =2
V [i 1, w] if wi w
V [i, w]
max{V [i 1, w], V [i 1, w wi ] bi } else
V [1, 4] max{V [1 1, 4], V [1 1, 4 2] 3}
max{0,3} 3
25
Items:
24/05/2023
V [i 1, w] if wi w
V [i, w]
max{V [i 1, w], V [i 1, w wi ] bi } else
V [1,5] max{V [1 1,5], V [1 1,5 2] 3}
max{0,3} 3
26
24/05/2023
Items:
Example (9) 1: (2,3)
2: (3,4)
i\W 0 1 2 3 4 5 3: (4,5)
i=2
0 0 0 0 0 0 0 4: (5,6)
bi=4
1 0 0 3 3 3 3
wi=3
2 0 0
3 0 w=1
4 0 w-wi =-2
V [i 1, w] if wi w
V [i, w]
max{V [i 1, w], V [i 1, w wi ] bi } else
V [2,1] V [2 1,1] =0
27
24/05/2023
Items:
Example (10) 1: (2,3)
i\W 0 1 2 3 4 5 i=2 2: (3,4)
0 0 0 0 0 0 0 3: (4,5)
bi=4
1 0 0 3 3 3 3 4: (5,6)
wi=3
2 0 0 3
3 0 w=2
4 0 w-wi =-1
V [i 1, w] if wi w
V [i, w]
max{V [i 1, w], V [i 1, w wi ] bi } else
V [1,5] max{V [1 1,5], V [1 1,5 2] 3}
max{0,3} 3
28
24/05/2023
Items:
Example (11) 1: (2,3)
2: (3,4)
i\W 0 1 2 3 4 5 3: (4,5)
i=2
0 0 0 0 0 0 0
bi=4 4: (5,6)
1 0 0 3 3 3 3
wi=3
2 0 0 3 4
3 0 w=3
4 0 w-wi =0
V [i 1, w] if wi w
V [i, w]
max{V [i 1, w], V [i 1, w wi ] bi } else
V [2,3] max{V [2 1,3], V [2 1,3 3] 4}
max{0, 4} 4
29
Items:
24/05/2023
1: (2,3)
2: (3,4)
Example (12) 3: (4,5)
i\W 0 1 2 3 4 5 i=2 4: (5,6)
0 0 0 0 0 0 0
bi=4
1 0 0 3 3 3 3
wi=3
2 0 0 3 4 4
3 0 w=4
4 0 w-wi =1
V [i 1, w] if wi w
V [i, w]
max{V [i 1, w], V [i 1, w wi ] bi } else
V [2, 4] max{V [2 1, 4], V [2 1, 4 3] 4}
max{0, 4} 4
30
Items:
24/05/2023
1: (2,3)
Example (13) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=2 4: (5,6)
0 0 0 0 0 0 0
bi=4
1 0 0 3 3 3 3
wi=3
2 0 0 3 4 4 7
3 0 w=5
4 0 w-wi =2
V [i 1, w] if wi w
V [i, w]
max{V [i 1, w], V [i 1, w wi ] bi } else
V [2,5] max{V [2 1,5], V [2 1,5 3] 4}
max{0,3 4} 7
31
Items:
24/05/2023
1: (2,3)
Example (14) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=3 4: (5,6)
0 0 0 0 0 0 0
bi=5
1 0 0 3 3 3 3
wi=4
2 0 0 3 4 4 7
3 0 0 3 4 w= 1..3
4 0
32
24/05/2023
Items:
Example (15) 1: (2,3)
2: (3,4)
i\W 0 1 2 3 4 5 3: (4,5)
i=3
0 0 0 0 0 0 0 4: (5,6)
bi=5
1 0 0 3 3 3 3
wi=4
2 0 0 3 4 4 7
3 0 0 3 4 5 w= 4
4 0 w- wi=0
V [i 1, w] if wi w
V [i, w]
max{V [i 1, w], V [i 1, w wi ] bi } else
V [3, 4] max{V [3 1, 4], V [3 1, 4 4] 5}
max{3, 5} 5
33
Items:
24/05/2023
1: (2,3)
Example (16) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=3 4: (5,6)
0 0 0 0 0 0 0
bi=5
1 0 0 3 3 3 3
wi=4
2 0 0 3 4 4 7
3 0 0 3 4 5 7 w= 5
4 0 w- wi=1
V [i 1, w] if wi w
V [i, w]
max{V [i 1, w],V [i 1, w wi ] bi } else
V [3,5] max{V [3 1,5], V [3 1,5 4] 5}
max{7, 5} 7
34
Items:
24/05/2023
1: (2,3)
Example (17) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=4 4: (5,6)
0 0 0 0 0 0 0
bi=6
1 0 0 3 3 3 3
wi=5
2 0 0 3 4 4 7
3 0 0 3 4 5 7 w= 1..4
4 0 0 3 4 5
35
Items:
24/05/2023
1: (2,3)
Example (18) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=4 4: (5,6)
0 0 0 0 0 0 0
bi=6
1 0 0 3 3 3 3
wi=5
2 0 0 3 4 4 7
3 0 0 3 4 5 7 w= 5
4 0 0 3 4 5 7 w- wi=0
V [i 1, w] if wi w
V [i, w]
max{V [i 1, w],V [i 1, w wi ] bi } else
V [4,5] max{V [4 1,5], V [4 1,5 5] 6}
max{7, 6} 7
How to find actual Knapsack Items