Professional Documents
Culture Documents
Jaehyun Park
CS 97SI
Stanford University
Dynamic Programming
1-dimensional DP
2-dimensional DP
Interval DP
Tree DP
Subset DP
Dynamic Programming 2
What is DP?
Dynamic Programming 3
Steps for Solving DP Problems
1. Define subproblems
2. Write down the recurrence that relates subproblems
3. Recognize and solve the base cases
Dynamic Programming 4
Outline
Dynamic Programming
1-dimensional DP
2-dimensional DP
Interval DP
Tree DP
Subset DP
1-dimensional DP 5
1-dimensional DP Example
5 = 1+1+1+1+1
= 1+1+3
= 1+3+1
= 3+1+1
= 1+4
= 4+1
1-dimensional DP 6
1-dimensional DP Example
◮ Define subproblems
– Let Dn be the number of ways to write n as the sum of 1, 3, 4
◮ Find the recurrence
– Consider one possible solution n = x1 + x2 + · · · + xm
– If xm = 1, the rest of the terms must sum to n − 1
– Thus, the number of sums that end with xm = 1 is equal to
Dn−1
– Take other cases into account (xm = 3, xm = 4)
1-dimensional DP 7
1-dimensional DP Example
◮ Recurrence is then
1-dimensional DP 8
Implementation
◮ Very short!
◮ Extension: solving this for huge n, say n ≈ 1012
– Recall the matrix form of Fibonacci numbers
1-dimensional DP 9
POJ 2663: Tri Tiling
1-dimensional DP 10
POJ 2663: Tri Tiling
◮ 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
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
Solving the LCS Problem
◮ Define subproblems
– Let Dij be the length of the LCS of x1...i and y1...j
◮ Find the recurrence
– If xi = yj , they both contribute to the LCS
◮ Dij = Di−1,j−1 + 1
– Otherwise, either xi or yj does not contribute to the LCS, so
one can be dropped
◮ Dij = max{Di−1,j , Di,j−1 }
– Find and solve the base cases: Di0 = D0j = 0
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
◮ Find the recurrence
– Consider a shortest palindrome y1...k containing xi...j
– Either y1 = xi or yk = xj (why?)
– y2...k−1 is then an optimal solution for xi+1...j or xi...j−1 or
xi+1...j−1
◮ Last case possible only if y1 = yk = xi = xj
Interval DP 23
Interval DP Example
◮ Find and solve the base cases: Dii = Di,i−1 = 0 for all i
Interval DP 24
Interval DP Example
Interval DP 25
An Alternate Solution
◮ Reverse x to get xR
◮ The answer is n − L, where L is the length of the LCS of x
and 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 don’t 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 minv∈V DV,v , where V is the given set of nodes
Subset DP 32
Subset DP Example
DS,v = min DS−{v},u + cost(u, v)
u∈S−{v}
Subset DP 33
Working with Subsets
Subset DP 34
Using Bitmasks
Subset DP 35
Conclusion
Subset DP 36