04 Dynamic Programming JG
04 Dynamic Programming JG
Jaehyun Park
CS 97SI
Stanford University
Outline
Dynamic Programming
1-dimensional DP
2-dimensional DP
Interval DP
Tree DP
Subset DP
Dynamic Programming
What is DP?
Dynamic Programming
1. Define subproblems
2. Write down the recurrence that relates subproblems
3. Recognize and solve the base cases
Dynamic Programming
Outline
Dynamic Programming
1-dimensional DP
2-dimensional DP
Interval DP
Tree DP
Subset DP
1-dimensional DP
1-dimensional DP Example
1-dimensional DP
1-dimensional DP Example
Define subproblems
Let Dn be the number of ways to write n as the sum of 1, 3, 4
1-dimensional DP
1-dimensional DP Example
Recurrence is then
Dn = Dn1 + Dn3 + Dn4
1-dimensional DP
Implementation
Very short!
Extension: solving this for huge n, say n 1012
Recall the matrix form of Fibonacci numbers
1-dimensional DP
1-dimensional DP
10
Define subproblems
Define Dn as the number of ways to tile a 3 n board
Find recurrence
Uuuhhhhh...
1-dimensional DP
11
Troll Tiling
1-dimensional DP
12
Defining Subproblems
1-dimensional DP
13
Defining Subproblems
1-dimensional DP
14
Finding Recurrences
1-dimensional DP
15
Finding Recurrences
Exercise:
1-dimensional DP
16
Outline
Dynamic Programming
1-dimensional DP
2-dimensional DP
Interval DP
Tree DP
Subset DP
2-dimensional DP
17
2-dimensional DP Example
2-dimensional DP
18
Define subproblems
Let Dij be the length of the LCS of x1...i and y1...j
Dij = Di1,j1 + 1
2-dimensional DP
19
Implementation
2-dimensional DP
20
Outline
Dynamic Programming
1-dimensional DP
2-dimensional DP
Interval DP
Tree DP
Subset DP
Interval DP
21
Interval DP Example
Example:
x: Ab3bd
Can get dAb3bAd or Adb3bdA by inserting 2 characters
(one d, one A)
Interval DP
22
Interval DP Example
Define subproblems
Let Dij be the minimum number of characters that need to be
inserted to make xi...j into a palindrome
Interval DP
23
Interval DP Example
1 + min{Di+1,j , Di,j1 }
Di+1,j1
xi 6= xj
xi = xj
Find and solve the base cases: Dii = Di,i1 = 0 for all i
Interval DP
24
Interval DP Example
Interval DP
25
An Alternate Solution
Reverse x to get xR
Interval DP
26
Outline
Dynamic Programming
1-dimensional DP
2-dimensional DP
Interval DP
Tree DP
Subset DP
Tree DP
27
Tree DP Example
Subproblems:
First, we arbitrarily decide the root node r
Bv : the optimal solution for a subtree having v as the root,
where we color v black
Wv : the optimal solution for a subtree having v as the root,
where we dont color v
Answer is max{Br , Wr }
Tree DP
28
Tree DP Example
Tree DP
29
Outline
Dynamic Programming
1-dimensional DP
2-dimensional DP
Interval DP
Tree DP
Subset DP
Subset DP
30
Subset DP Example
Subset DP
31
Subset DP Example
Define subproblems
DS,v : the length of the optimal path that visits every node in
the set S exactly once and ends at v
There are approximately n2n subproblems
Answer is minvV DV,v , where V is the given set of nodes
Subset DP
32
Subset DP Example
DS,v =
Subset DP
min
uS{v}
DS{v},u + cost(u, v)
33
Subset DP
34
Using Bitmasks
Intersection: x & y
Symmetric difference: x y
Subset DP
35
Conclusion
Subset DP
36