Chapter 4 Dynamic Programming
Chapter 4 Dynamic Programming
Dynamic Programming
and traversal techniques
2
Some of Designing algorithms
Strategies
Brute force
Divide and conquer
Greedy approach
Dynamic programming
Backtracking
3
3
Brute force Approach
Brute force is a type of algorithm that tries a large
number of patterns to solve a problem. In some
cases, they are extremely simple and rely on raw
computing power to achieve results.
A common example of a brute force algorithm is a
security threat that attempts to guess a password
using known common passwords. Such an
algorithm might also try dictionary words or even
every combination of ASCII strings of a certain
length.
4
Divide and Conquer
Divide and conquer is an algorithm design
paradigm based on multi-branched recursion.
A divide and conquer algorithm works by recursively
breaking down a problem into two or more sub-
problems of the same or related type, until these
become simple enough to be solved directly.
The solutions to the sub-problems are then combined
to give a solution to the original problem.
5
Divide-and-Conquer
a problem of size n
(instance)
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
It leads to a
a solution to
the original problem recursive
algorithm!
6
greedy approach
Among all the algorithmic approaches, the straightforward
approach is the Greedy method.
In this approach, the decision is taken on the basis of current
available information without worrying about the effect of
the current decision in the future.
This approach never reconsiders the choices taken
previously.
Suppose that a problem can be solved by a sequence of
decisions. In the greedy method, each decision is locally
optimal. These locally optimal solutions will finally add up
to a globally optimal solution.
Make choices that look good in the short term but not
necessarily in the long term. 7
Greedy Approach
8
Backtracking
Backtracking is a general algorithm for finding
all (or some) solutions to some computational
problems, notably constraint satisfaction problems
(CSP), that incrementally builds candidates to the
solutions, and abandons each partial candidate
("backtracks") as soon as it determines that the
candidate cannot possibly be completed to a valid
solution.
Backtracking is an important tool for
solving constraint satisfaction problems, such
as crosswords, verbal arithmetic, Map Coloring….
9
Backtracking
10
Dynamic Programming
• Main idea:
- set up a recurrence relating a
solution to a larger instance to
solutions of some smaller instances
- solve smaller instances once
- record solutions in a table
- extract solution to the initial instance
from that table
* the distinguishing feature of dynamic
programming is Memorization or Tabling
12
Example Fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21, …
13
Reuse earlier results! f(7)
(“memoization” or “tabling”) f(6)
0, 1, 1, 2, 3, 5, 8, 13, 21, …
f(5)
f(0) = 0.
f(1) = 1. f(4)
f(N) = f(N-1) + f(N-2) …
if N 2.
int f(int n) {
if n < 2
return n
else
return fmemo(n-1) + fmemo(n-2)
}
14
0-1 Knapsack problem
15
Knapsack problem
Given some items, pack the knapsack to get
the maximum total value. Each item has some
weight and some value. Total weight that we can
carry is no more than some fixed number W.
So we must consider weights of items as well as
their values.
17
0-1 Knapsack problem
Given a knapsack with maximum capacity W, and
a set S consisting of n items
Each item i has some weight wi and benefit value
bi (all wi and W are integer values)
Problem: How to pack the knapsack to achieve
maximum total value of packed items?
max bi subject to wi W
iT iT
18
0-1 Knapsack problem:
brute-force approach
19
0-1 Knapsack problem:
dynamic programming approach
We can do better
with an algorithm
based on dynamic
programming
We need to
carefully identify
the sub-problems
20
Defining a Subproblem
Given a knapsack with maximum capacity W, and
n items
Each item i has some weight wi and benefit value
bi (all wi and W are integer values)
Problem: How to pack the knapsack to achieve
maximum total value of packed items?
Find
» V[i,w]???
21
Recursive Formula
V [i 1, w] if w i w
V [i , w ]
max{V [i 1, w], V [i 1, w w i ] bi} else
The best subset of Si that has the total weight w,
either contains item i or not.
First case: wi>w. Item i can’t be part of the solution,
since if it was, the total weight would be > w, which
is unacceptable.
Second case: wi w. Then the item i can be in the
solution, and we choose the case with greater value.
22
0-1 Knapsack Algorithm
for w = 0 to W
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
for i = 1 to n
for w = 0 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
23
Running time
for w = 0 to W
O(W)
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
for i = 1 to n Repeat n times
for w = 0 to W
O(W)
< the rest of the code >
n = 4 (# of elements)
W = 5 (max weight)
Elements (weight, benefit):
(2,3), (3,4), (4,5), (5,6)
25
Example (2)
i\W 0 1 2 3 4 5
0 0 0 0 0 0 0
1
2
3
4
for w = 0 to W
V[0,w] = 0
26
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
for i = 1 to n
V[i,0] = 0
27
Items:
1: (2,3)
Example (4) 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
wi=2
2 0
3 0 w=1
4 0 w-wi =-1
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
28
Items:
1: (2,3)
Example (5) 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
wi=2
2 0
3 0 w=2
4 0 w-wi =0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
29
Items:
1: (2,3)
Example (6) 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
wi=2
2 0
3 0 w=3
4 0 w-wi =1
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
30
Items:
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
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
31
Items:
1: (2,3)
Example (8) 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 3
wi=2
2 0
3 0 w=5
4 0 w-wi =3
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
32
Items:
1: (2,3)
Example (9) 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 0 w=1
4 0 w-wi =-2
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
33
Items:
1: (2,3)
Example (10) 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
3 0 w=2
4 0 w-wi =-1
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
34
Items:
1: (2,3)
Example (11) 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
3 0 w=3
4 0 w-wi =0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
35
Items:
1: (2,3)
Example (12) 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
3 0 w=4
4 0 w-wi =1
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
36
Items:
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
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
37
Items:
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
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
38
Items:
1: (2,3)
Example (15) 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 w= 4
4 0 w- wi=0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
39
Items:
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
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
40
Items:
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
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
41
Items:
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
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
42
How to find actual Knapsack Items
43
How to find actual Knapsack
Items
All of the information we need is in the table.
V[n,W] is the maximal value of items that can be
placed in the Knapsack.
Let i=n and k=W
if V[i,k] V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
i = i1 // Assume the ith item is not in the knapsack
44
Items:
1: (2,3)
Finding the Items 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 k= 5
1 0 0 3 3 3 3 bi=6
2 0 0 3 4 4 7 wi=5
3 0 0 3 4 5 7 V[i,k] = 7
4 0 0 3 4 5 7 V[i1,k] =7
i=n, k=W
while i,k > 0
if V[i,k] V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
i = i1 45
Items:
1: (2,3)
Finding the Items (2) 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 k= 5
1 0 0 3 3 3 3 bi=6
2 0 0 3 4 4 7 wi=5
3 0 0 3 4 5 7 V[i,k] = 7
4 0 0 3 4 5 7 V[i1,k] =7
i=n, k=W
while i,k > 0
if V[i,k] V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
i = i1 46
Items:
1: (2,3)
Finding the Items (3) 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 k= 5
1 0 0 3 3 3 3 bi=5
2 0 0 3 4 4 7 wi=4
3 0 0 3 4 5 7 V[i,k] = 7
4 0 0 3 4 5 7 V[i1,k] =7
i=n, k=W
while i,k > 0
if V[i,k] V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
i = i1 47
Items:
1: (2,3)
Finding the Items (4) 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 k= 5
1 0 0 3 3 3 3 bi=4
2 0 0 3 4 4 7 wi=3
3 0 0 3 4 5 7 V[i,k] = 7
4 0 0 3 4 5 7 V[i1,k] =3
i=n, k=W
k wi=2
while i,k > 0
if V[i,k] V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
i = i1 48
Items:
1: (2,3)
Finding the Items (5) 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 k= 2
1 0 0 3 3 3 3 bi=3
2 0 0 3 4 4 7 wi=2
3 0 0 3 4 5 7 V[i,k] = 3
4 0 0 3 4 5 7 V[i1,k] =0
i=n, k=W
k wi=0
while i,k > 0
if V[i,k] V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
i = i1 49
Items:
1: (2,3)
Finding the Items (6) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=0 4: (5,6)
0 0 0 0 0 0 0 k= 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7 The optimal
knapsack
4 0 0 3 4 5 7
should contain
i=n, k=W {1, 2}
while i,k > 0
if V[i,k] V[i1,k] then
mark the nth item as in the knapsack
i = i1, k = k-wi
else
i = i1 50
Items:
1: (2,3)
Finding the Items (7) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 4: (5,6)
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7 The optimal
knapsack
4 0 0 3 4 5 7
should contain
i=n, k=W {1, 2}
while i,k > 0
if V[i,k] V[i1,k] then
mark the nth item as in the knapsack
i = i1, k = k-wi
else
i = i1 51
Conclusion
Dynamic programming is a useful technique of
solving certain kind of problems
When the solution can be recursively described in
terms of partial solutions, we can store these
partial solutions and re-use them as necessary
(memorization)
Running time of dynamic programming algorithm
vs. naïve algorithm:
» 0-1 Knapsack problem: O(W*n) vs. O(2n)
52