Professional Documents
Culture Documents
03 - Dynammic Programming
03 - Dynammic Programming
Contents
2
Presentation of the problem: Calculation of
the number of rabbits
Conditions are given as follows. What is the number of rabbits
in nth month?
In the first month, only a pair of new born rabbits exist.
Rabbits with two month or more old can reproduce.
A pair of reproducible rabbits give birth to a pair of babies every
month.
Rabbits never die.
3
Calculation of the number of rabbits
1st month 2nd month 3rd month 4th month 5th month 6th month 7th month
8 pairs
4
Calculation of the number of rabbits
n+2th month.
5
Calculation of the number of rabbits
Let f(n) be the number of rabbits in nth month
6
Fibonacci sequence
The sequence starting with 0 and 1, and having the sum of the
previous two numbers as its next element is called the
Fibonacci sequence.
0, 1, 1, 2, 3, 5, 8, 13, ...
The function F calculating the ith value of Fibonacci sequence
can be defined as:
F0 = 0, F1 = 1
Fi = Fi-1 + Fi-2 for i ≥2
7
Recursive function for obtaining Fibonacci number
fibo(n)
IF n < 2 : RETURN n
ELSE : RETURN fibo(n - 1) + fibo(n - 2)
8
In the previous example, the algorithm implementing function
calculating the Fibonacci number with a recursive function has
a problem.
The problem is “there are tremendous redundant calls”.
Call Tree of Fibonacci sequence
fib(7)
fib(6)
fib(5) fib(5)
fib (2) fib(2) fib(2) fib (2) fib(2) fib (2) fib(2) fib(2)
10
Redundancy of Fibonacci sequence
The values which should be known to find the nth Fibonacci
number are:
fibo(n) can be obtained when the values of fibo(0) ~ fibo(n-1) are
known.
In other words, the value can be obtained by calling fibo()function n
times.
When calculating the nth Fibonacci number through recursion,
if fibo(n)function is called,
fibo()function is called more than 2 n/2 times.
2 n/2 > n
Therefore, it can be seen that the function is called
redundantly by applying the principle of pigeon hole.
11
Exercise 1
f(1) f(2)
12
Memoization
13
In the algorithm calculating Fibonacci number, the execution
time can be reduced to O(n) by storing the value of fibo1(n) as
soon as the calculation is performed (memoize).
The algorithm to which memoization method is applied is as
follows.
fibo1(n)
IF n > 2 AND memo[n] == 0
memo[n] ← fibo1(n-1) + fibo1(n-2)
RETURN memo[n]
14
Memoization
Additional memory space is needed.
15
Dynamic Programming Algorithm
Dynamic Programming algorithm is, like greedy algorithm, an
algorithm for solving optimization problem.
16
Application requirements of Dynamic
programming
Problems should comply with the following requirements for
applying dynamic programming.
Optimal substructure
Overlapping subproblems
17
Overlapping subproblems
DP solves a big problem by solving the small problems constituting
the big problem first, then by using the optimal solutions of small
problems in circulation.
In order to explicitly express the recurrence relation, dynamic programming
generally uses recurrence formula, a general mathematical tool.
When the stored solutions are needed again, they are obtained by
referencing the table without recalculating the problem for the
solution, avoiding redundant calculations.
18
Optimal substructure
Dynamic programming cannot be applied to all problems on
optimization. Given problem should satisfy the principle of optimality
for applying dynamic programming.
19
Example to which the principle of optimality does not apply:
Longest path problem A
The longest path from A to D is [A, C, B, D]. 1
1
However, the longest path from A to C, which 2
B C
is a subpath of the above path, is [A, B, C] 3
20
Comparison of Divide and Conquer and
Dynamic programming
Divide and Conquer
Divide the problem into unassociated subproblems.
Solve the subproblem recursively.
Combine the solutions of subproblems.
Example: Merge sort, quick sort
DP
Cannot be applied when subproblems are not associated, i.e.,
subproblems share smaller subproblems.
Every subproblem in calculated just once and then the result is
stored for reuse.
Calculation is performed again when subproblems like Divide and
Conquer appear.
21
In DP, dependent relationships exist among subproblems.
For example, solutions of E, F and G are used for solving C.
A A
B C B C
D E F G D E F G
22
Approach by applying 3-step DP
23
Application of DP for Fibonacci number
Fibonacci number is constructed of optimal substructures since the answer of
the main problem can be obtained from answers of subproblems.
1. Divide the problem into subproblems.
2. Define the problem through recurrence formulae.
3. Obtain the solution starting from the smallest subproblem. Store the
result in a table, and obtain the solution of upper problem by using
the solutions of subproblems stored in the table.
24
Algorithm of applying DP for Fibonacci number
fibo_dp(n)
f[0] ← 0
f[1] ← 1
FOR i in 2 → n
f[i] ← f[i - 1] + f[i - 2]
RETURN f[n]
25
Calculating Fibonacci number – DP algorithm analysis
DP algorithm shows faster execution speed.
Reasons
26
Exercise 2
27
Presentation of the problem: Making changes
of coins
Kinds of coins
1 cent, 4 cents, 6 cents
8 cents are to be given as changes. At least how many coins
are necessary for the changes?
28
Approach of greedy method
6 cents, 1 cent, 1 cent
What is optimal?
4 cents, 4 cents
29
First, a recursive algorithm for changes of 8 cents
Select each of 3 types of coins and solve recursively.
30
8
1 6
Call tree 4
7 4 2
6 3 1 3 0 X 1 X X
5 2 0 2 XX 0 2 X X 0 X X
4 1 X 1 X X 1 X X 1 X X
3 0 X 0 X X 0 X X 0 X X 0 X X
2 X X
1 X X
Redundancy (overlapping subproblem)
0 X X
31
DP Approach: Bottom up approach
Optimal solution for 1 cent (select) optimal solution for 2 cents
(select) optimal solution for 3 cents (select) optimal solution for 4
cents (select) …
32
Subproblem definition
C[i][ j] = Optimum when giving j cents with Di coins
(Di1 is 1 cent, Di2 is 4 cents, Di3 is 6 cents)
Principle of optimization
Looking at the subproblems, the number of changes for the whole
problem is not the optimal solution if optimal change is not
accomplished when giving changes of j cents with ith coin.
Recurrence formula and table
C[i][ j] = min( C[i][ j - di] + 1, C[i - 1][ j] ) O(i * j)
0 1 2 3 4 5 6 7 8 9
Changes
cent cent cent cent cent cent cent cent cent cent
33
Presentation of the problem: Birthday gift
4 kg/400
dollars 10kg
5 kg/100
dollars
6 kg/300
dollars 3 kg/500
dollars
34
0-1 Knapsack
Knapsack problem is the problem of finding the maximum values of
items that can be put into a knapsack of capacity W when n items, the
weight, wi, and value, vi, of the item i are given, provided that the
total weight of the items put into the knapsack should not exceed W
and that there are only one for each item.
4 kg/400
dollars 10kg
5 kg/100
dollars
6 kg/300
dollars 3 kg/500
dollars
35
Let’s approach the knapsack problem with DP.
First, consider the given conditions of the problem in order to find out
subproblems of the knapsack problem.
A total of 4 elements include the item, the weight of the item, the value of the
item and the capacity of the knapsack.
Among them, the item and its weight are essential in defining the
subproblem.
The reason is that, starting from the state of empty knapsack, whether
to put an item in the knapsack or not should be determined based on
the sum of the items currently contained in the knapsack.
Also, it should be checked if the capacity of the knapsack is exceeded
when putting an item into the knapsack.
36
Therefore, subproblems of the knapsack problem can be defined as
follows:
K[i, w] = Maximum value when considering only items 1-i and the capacity of
0 if i = 0 or w = 0
K[i, w] = K[i - 1], w] if wi > w
max(vi + K[i - 1, w - wi], K[i - 1, w]) if i > 0 and wi ≤ w
37
Considering the ith item:
0 if i = 0 or w = 0
K[i, w] = K[i - 1], w] if wi > w
max(vi + K[i - 1, w - wi], K[i - 1, w]) if i > 0 and wi ≤ w
Total value is
The maximum value in the case the capacity of the knapsack is (w - wi)
39
0-1 Knapsack algorithm
Capacity of knapsack W
n items and weight, wi, and value, vi, of the ith item, provided i = 1,
2, ⋯, n
Output: K[n, W]
FOR i in 0 → n : K[i, 0] ← 0
FOR w in 0 → W : K[0, w] ← 0
FOR i in 1 → n
FOR w in 1 → W
IF wi > w
K[i, w] ← K[i - 1, w]
ELSE
K[i, w] ← max(K[i-1, w - wi] + vi , K[i-1, w])
RETURN K[n, W]
40
Process of performing 0-1 Knapsack
algorithm
The capacity of the knapsack is W = 10kg, and the weight and
value of each item is given as follows.
i v w
2
1
1 10 5 10kg
5 kg/100 4 kg/400
2 40 4 dollars dollars
3 30 6
3 4
4 50 3
6 kg/300 3 kg/500
dollars dollars
41
In lines 1-2, the elements at the 0th row and the 0th column
are initialized to 0.
W = 10
i v w K[i, w] w
i Selected items 0 1 2 3 4 5 6 7 8 9 10
1 10 5
0 {} 0 0 0 0 0 0 0 0 0 0 0
2 40 4
1 {1} 0
3 30 6 2 {1, 2} 0
4 50 3 3 {1, 2, 3} 0
4 {1, 2, 3, 4} 0
42
In order to consider item one by one in line 3, the item number i
varies in the range of 1-4, and in line 4, the (temporary) capacity of
the knapsack w is incremented by 1 kg to finally give 10 kg, the
capacity of the knapsack.
43
When w=2, 3, 4, item 1 cannot be put into the knapsack since
w1>w respectively. Thus, K[2,1]=0, K[1,3]=0, K[1,4]=0
respectively. That is, item 1 with weight of 5 kg cannot be put
into the knapsack even if the capacity of the knapsack is
increased to 4 kg.
4kg
3kg
2kg
5kg/100
dollars
44
When w=5 (capacity of the knapsack is 5 kg), item 1 can be
put into the knapsack since w1=w, i.e., the weight of item 1 is
5 kg. Therefore,
5 kg
45
When w=6, 7, 8, 9, 10, item 1 can be put into the knapsack in each
case as in the case of w=5. Therefore, K[1,6] = K[1,7] = K[1,8] =
K[1,9] = K[1,10] = 10 respectively.
46
Following tables show the results of algorithm which is carried out by
increasing the capacity of the knapsack from 1 to W only for item 1.
W = 10
i v w K[i, w] w
i Selected items 0 1 2 3 4 5 6 7 8 9 10
1 10 5
0 {} 0 0 0 0 0 0 0 0 0 0 0
2 40 4
1 {1} 0 0 0 0 0 10 10 10 10 10 10
3 30 6 2 {1, 2} 0
4 50 3 3 {1, 2, 3} 0
4 {1, 2, 3, 4} 0
47
When i=2 (that is, the solution of subproblem for item 1 has been obtained
when i=1, and item 2 is considered by using the solution.)
4kg/400 dollars
48
When w=4 (capacity of the knapsack is 4 kg), item 2 can be put into
the knapsack.
= max( 0, 0 + 40 )
2
= max( 0, 40 ) = 40.
4 kg/400
dollars
49
When w=5 (capacity of the knapsack is 5 kg), item 2 can also be put
into the knapsack since its weight is 4 kg. In this case, however, the
values of item 1 and item 2 are compared when they are put into the
knapsack, and the item giving more value is put into the knapsack.
5kg
= max(10, 0 + 40 ) 2
1
50
When w=6, 7, 8, more value is obtained in each case when item 1 is
taken out and item 2 is put into the knapsack. Therefore, K[2,6] =
K[2,7] = K[2,8] = 40 respectively.
6kg 7kg 8kg
4 kg/400
dollars
51
When w=9 (capacity of the knapsack is 9 kg), item 2 is to be put into
the knapsack. Since w2<w, however, item 2 can be put into the
knapsack. Therefore,
9kg
1
K[2, 9] = max( K[i-1, w], K[i-1, w-wi] + vi )
5kg/100
= max( K[2-1, 9], K[2-1, 9-4] + 40 ) dollars
= max( 10, 10 + 40 ) 2
That is, both items 1 and 2 can be put into the knapsack, which
means that the value becomes 50.
52
When w=10 (capacity of the knapsack is 10 kg), K[2,10]=50 since
w2<w, in the same way as in the case of w=9, which means that
value of 50 is acquired by putting items 1 and 2 into the knapsack.
i v w K[i, w] w
i Selected items 0 1 2 3 4 5 6 7 8 9 10
1 10 5
0 {} 0 0 0 0 0 0 0 0 0 0 0
2 40 4
1 {1} 0 0 0 0 0 10 10 10 10 10 10
3 30 6 2 {1, 2} 0 0 0 0 40 40 40 40 40 50 50
4 50 3 3 {1, 2, 3} 0
4 {1, 2, 3, 4} 0
53
LIS
Presentation of the problem: What is the
length of the longest increasing sequence?
A sequence is arranged from left to right as follows.
3, 2, 6, 4, 5, 1
55
LIS (Longest Increasing Subsequence)
56
Brute-force approach
First calculate all subsets of the sequence, and determine if the
subsets are increasing sequences. Then, find the sequence with the
longest length from the increasing sequences.
Ex: S = { 3, 2, 6, 4, 5, 1}
2N = 26 = 64 subsets
Length
0 {}
1 {3}, {2}, {6}, {4}, {5}, {1}
2 {3,2}, {3,6}, {3,4}, {3,5}, {3,1}, {2,6}, {2,4}, …
… …
5 {3, 2, 6, 4, 5}, {3, 2, 6, 4, 1}, {3, 2, 6, 5, 1}, …
6 {3, 2, 6, 4, 5, 1}
It is advantageous to examine from
the subset with longer length
57
Algorithm
O(2n), exponential time complexity
FOR i in N → 1
find all subsequence of S with length of i
IF there is one increasing subsequence
BREAK
58
DP approach
59
Case2: If LIS(i) contains ai, LIS(i) = ?
Find aj with the relation of increasing sequences aj < ai.
60
DP approach algorithm
O(n2)
FOR i in 1 → n
LIS[i] = 1
FOR j in 1 → i − 1
IF aj < ai AND 1 + LIS[ j] > LIS[i]
LIS[i] = 1 + LIS[ j]
RETURN max LIS[]
{ 3, 2, 6, 4, 5, 1}
61
More efficient method using binary search
C[k]: Store the smallest value of the increasing sequences with length
k at C[k].
O(nlogn)
8, 2, 4, 3, 6, 11, 7, 10, 14, 5
8 2 2 2 2 2 2 2 2 2 C[1]
4 3 3 3 3 3 3 3 C[2]
6 6 6 6 6 5 C[3]
11 7 7 7 7 C[4]
10 10 10 C[5]
14 14 C[6]
62
Exercise 1
3, 2, 6, 4, 5, 1
63
ALL PAIRS SHORTEST PATH
Presentation of the problem: All pairs
shortest path
What is the shortest path between each pair of vertices? The
value of the shortest path from 1 to 2 is 4, and the value of the
shortest path from 1 to 5 is 8. What are the values of the
shortest paths between other vertices?
4
1 2
2 3
1 1
5 -2 3 4 -3
2
1
3
1
4 5
2
65
Summary of terms on Graph
Path 5 -2 v3 4 -3
Acyclic graph v4 v3
2
Length
Adjacent vertices
66
Shortest Path
67
Brute-force approach
First find the lengths of all paths from a vertex to another vertex, and
then find the shortest length among them.
Assume the graph is a complete graph having n vertices,
When the paths from a certain vertex i to another vertex j are
collected, the paths also include the paths which pass through other
all vertices only once, the number of which is to be calculated first.
The number of vertices that can reach the first starting from i is n-2,
and by selecting one vertex among them, the number of vertices
that can reach next is n-3, which leads to total number of paths of
(n-2)(n-3)…1 = (n-2)!.
Even when considering the number of the paths, it can be seen that
the algorithm is absolutely inefficient since the number is much
larger than the exponent!
68
DP approach
In order to solve this problem, it is necessary to carry out the
shortest path algorithm of Dijkstra with each point as the starting
point.
The time complexity at this step is (n-1)xO(n2) = O(n3) when arrays
are used, where n is the number of points.
Warshall proposed a dynamic programming algorithm for finding out
whether the path for all pairs exists in a graph (transitive closure),
and Floyd modified the algorithm to design the algorithm for finding
out all pairs shortest path.
Therefore, the dynamic programming algorithm for finding all pairs
shortest path is called the Floyd-Warshall algorithm (shortly referred
to as ‘Floyd algorithm’).
69
The time complexity of Floyd algorithm is O(n3), which is the
same as the time complexity when Dijkstra algorithm is used
(n-1) times.
However, Floyd algorithm is very simple and so more efficient
than using Dijkstra algorithm.
70
In order to solve all pairs shortest path problem through
dynamic programming algorithm, subproblems should be found
first.
For this purpose, first consider the case where the number of
points in a graph is small.
When there are 3 points in a graph, in order to find the
shortest path from point i to point j, the shorter of the 2 paths,
that is, the path of going directly from point i to point j and the
path via the point 1 should be selected.
i j
71
Another important idea is to calculate
the distance of the shortest path by adding the points that can be
visited starting from point 1, then points 1 and 2, and then points 1,
2 and 3, and so on, and then by finally considering all points from 1
to n as the points that can be visited.
Definition of sub problems: Suppose the points in the input
graph are 1, 2, 3, ... , n respectively.
Dijk = Distance of the shortest path among all paths from point
i to point j, considering only points {1, 2, ⋯, k}
as the points that can be visited
It should be noted that it does not mean the path that should
pass through all points from point 1 to point k.
72
1
i j Dij1
73
Then the shorter of the distance of the path going from i to j
via point 2 and Dij1 is set as Dij2, where Di21 + D2j1 is the
distance of the path passing through point 2.
The next bigger problem is to calculate Dij2 for all pairs i and j,
where i≠2 and j≠2.
i j Dij2
74
The shorter of the distance of the path going from i to j via
point k and Dijk-1 is set as Dijk, where Dikk-1 + Dkjk-1 is the
distance of the path passing through point k, and i≠k and j≠k.
i j Dijk
75
In this way, calculations of Dijk are made until k reaches n from
1, then Dijn, that is, the distance of the shortest path of all
pairs i and j which are considered as the points that are
available for passing through can be found, which is the all
pairs shortest path algorithm of Floyd.
76
All pairs shortest path algorithm
77
In the for-loop of line 1, k varies from 1 to n, which is for extending
the points that can be passed to the range from 1 to n.
Lines 2~3: Loops for considering each pair of points, i.e., 1-1, 1-2,
1-3, ⋯, 1-n, 2-1, 2-2, ⋯, 2-n, ⋯, n-1, n-2, ⋯, n-n one by one.
However, the loop is not executed when i-i, i=k or j=k.
78
D[i,k] k D[k,j]
i j new D[i,j]
old D[i,j]
79
Execution process of AllPairsShortest
algorithm
The process of renewing the elements of the array D as k
increases from 1 to 5 will be considered.
4
v1 v2 D 1 2 3 4 5
2 3
1 0 4 2 5 ∞
1 1
2 ∞ 0 1 ∞ 4
5 -2 v3 4 -3
3 1 3 0 1 2
2
1 4 -2 ∞ ∞ 0 2
3
1
v4 v3 5 ∞ -3 3 1 0
2
80
When k=1:
D[2,3] = min{D[2,3], D[2,1]+D[1,3]} = min{1, ∞+2} = 1
D[2,4] = min{D[2,4], D[2,1]+D[1,4]} = min{∞, ∞+5} = ∞
D[2,5] = min{D[2,5], D[2,1]+D[1,5]} = min{4, ∞+∞} = 4
D[3,2] = min{D[3,2], D[3,1]+D[1,2]} = min{3, 1+4} = 3
D[3,4] = min{D[3,4], D[3,1]+D[1,4]} = min{1, 1+5} = 1
D[3,5] = min{D[3,5], D[3,1]+D[1,5]} = min{2, 1+∞} = 2
D[4,2] = min{D[4,2], D[4,1]+D[1,2]} = min{∞, -2+4} = 2 // Renewed
D[4,3] = min{D[4,3], D[4,1]+D[1,3]} = min{∞, -2+2} = 0 // Renewed
D[4,5] = min{D[4,5], D[4,1]+D[1,5]} = min{2, -2+∞} = 2
D[5,2] = min{D[5,2], D[5,1]+D[1,2]} = min{-3, ∞+4} = -3
D[5,3] = min{D[5,3], D[5,1]+D[1,3]} = min{3, ∞+2} = 3
D[5,4] = min{D[5,4], D[5,1]+D[1,4]} = min{1, ∞+5} = 1
81
When k=1, D[4,2] and D[4,3] are renewed to 2 and 0
respectively. Other elements remain unchanged.
4
v1 v2 D 1 2 3 4 5
2 3
1
1 0 4 2 5 ∞
1
5 -2 v3 4 -3
2 ∞ 0 1 ∞ 4
2 3 1 3 0 1 2
1
3 4 -2 2 0 0 2
1
v4 v3 5 ∞ -3 3 1 0
2
82
When k=2:
D[1,5] is renewed to 8, the distance of 1 → 2 → 5.
D[5,3] is renewed to -2, the distance of 5 → 2 → 3.
4
v1 v2 D 1 2 3 4 5
2 3
1
1 0 4 2 5 8
1
5 -2 v3 4 -3
2 ∞ 0 1 ∞ 4
2 3 1 3 0 1 2
1
1
3 4 -2 2 0 0 2
v4 v3 5 ∞ -3 -2 1 0
2
83
When k=3
D 1 2 3 4 5
1 0 4 2 3 4
2 2 0 1 2 3 4
3 1 3 0 1 2 v1 v2
2 3
4 -2 2 0 0 2
1 1
5 -1 -3 -2 -1 0
5 -2 v3 4 -3
When k=4 2
1
D 1 2 3 4 5 1
3
1 0 4 2 3 4 v4 v3
2
2 0 0 1 2 3
3 -1 3 0 1 2
4 -2 2 0 0 2
5 -3 -3 -2 -1 0
84
When k=5, total of 3 elements are renewed, which becomes
the final solution for the given input.
D 1 2 3 4 5 v1
4
v2
1 0 1 2 3 4 2 3
2 0 0 1 2 3 1 1
3 -1 -1 0 1 2 5 -2 v3 4 -3
2
4 -2 -1 0 0 2 1
3
5 -3 -3 -2 -1 0 v4
1
v3
2
85
Time complexity
Since the time complexity of AllPairsShortest is calculated for every i,
j pair for each k as shown in the above example, total of n x n x n =
n3 calculations are made, with each calculation taking O(1) time.
Therefore, the time complexity of AllPairsShortest is O(n3).
86
Exercise 2
Calculate and output the shortest path for each pair of the
following graph G.
4
v1 v2
2 3 D 1 2 3 4 5
1 1 1 0 1 2 3 4
2 0 0 1 2 3
5 -2 v3 4 -3
3 -1 -1 0 1 2
2
1
4 -2 -1 0 0 2
3 5 -3 -3 -2 -1 0
1
v4 v3
2
87