Dynamic Programming
Dynamic Programming
Paradigm
0/1 Knapsack
Matrix chain multiplication
Bellman-Ford algorithm
2
Dynamic Programming - DP
3
Plan
Paradigm
Fibonacci
Shortest path in multistage graph
0/1 Knapsack
Matrix chain multiplication
Bellman-Ford algorithm
4
Fibonacci number
F1 = F2 = 1
Fn = Fn-1 + Fn-2
Compute Fn
5
Naive Algorithm
6
Memorized DP algorithm
Pseudo code
memo = {}
int Fibo(int n){
if (n in memo) return memo[n]
if (n<=2) f=1
else f = Fibo(n-1) + Fibo(n-2)
memo[n] = f
return f
}
T(n) = n T(n) = O(n)
7
DP ~ recursion + memorization
8
Bottom-up DP Algorithm
T(n) = O(n)
9
Bottom-up DP Algorithm
10
Plan
Paradigm
Fibonacci
Shortest path in multistage graph
0/1 Knapsack
Matrix chain multiplication
Bellman-Ford algorithm
11
Shortest path in multistage graph
12
Guessing
13
Multistage graph
14
Tables
15
Tables
16
Forward to produce results
1 - 2 - 7 - 10 - 12
1 - 3 - 6 - 10 - 12
17
Some summary of DP
18
5 steps for DP
Define subproblems
Guess (part of solution)
Relate subproblem solution
Recurse and memorize or build DP table bottom
up
Solve original problem
19
Divide and Conquer vs. Dynamic
Programming
20
Principle of optimality
In an optimal sequence of decisions or choices, each
subsequence must also be optimal
For some problems, an optimal sequence may be found by
making decisions one at a time and never making a mistake
True for greedy algorithms (except label correctors)
For many problems it’s not possible to make stepwise
decisions based only on local information so that the
sequence of decisions is optimal
One way to solve such problems is to enumerate all possible decision
sequences and choose the best
Dynamic programming can drastically reduce the amount of
computation by avoiding sequences that cannot be optimal by the
“principle of optimality”
21
Plan
Paradigm
0/1 Knapsack
Matrix chain multiplication
Bellman-Ford algorithm
22
0/1 Knapsack problem
max pi x i
0 i n
x i {0, 1}
w x
0 in
i i M
pi 0, w i 0, 0 i n
23
0/1 Knapsack example
p = {1, 2, 5, 6}
w = {2, 3, 4, 5}
M = 8; n=4
24
Filling the table
pi wi V 0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0 0 0
1 2 1 0 0 1 1 1 1 1 1 1
2 3 2 0 0 1 2 2 3 3 3 3
5 4 3 0 0 1 2 5 5 6 7 7
6 5 4 0 0 1 2 5 6 6 7 8
25
Trace result
pi wi V 0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0 0 0
1 2 1 0 0 1 1 1 1 1 1 1
2 3 2 0 0 1 2 2 3 3 3 3
5 4 3 0 0 1 2 5 5 6 7 7
6 5 4 0 0 1 2 5 6 6 7 8
26
Plan
Paradigm
0/1 Knapsack
Matrix chain multiplication
27
Matrix chain multiplication
28
Developing algorithm
29
Developing algorithm
30
Formula
31
Example
A1 x A2 x A3 x A4
3 2 2 4 4 2 2 5
d0 d1 d1 d2 d2 d3 d3 d4
32
Formula
Example
C 1 2 3 4 k 1 2 3 4
1 0 24 1 1
2 0 26 2 2
3 0 40 3 3
4 0 4
33
Formula
Example
C 1 2 3 4 k 1 2 3 4
1 0 24 28 58 1 1 1 3
2 0 26 36 2 2 3
3 0 40 3 3
4 0 4
34
Result
((A1)(A2A3))(A4)
Number of multiplications: 58
35
Plan
Paradigm
0/1 Knapsack
Matrix chain multiplication
Bellman-Ford algorithm
36
Single-source shortest path problem
37
Building DP algorithm
38
Algorithm
39
Example 1
40
Example 2
1 2 3 4 5 6
0 0
1 0 4 9 13 16 14
2 0 2 7 11 14 12
3 0 0 5 9 12 10
4 0 -2 3 7 10 8
There exists negative
5 0 -4 1 5 8 6
weight cycle
6 0 -6 -1 3 6 4
41
Thanks for your attention!
42