Introduction to Algorithms, Recitation 15
Introduction to Algorithms, Recitation 15
006
Massachusetts Institute of Technology
Instructors: Erik Demaine, Jason Ku, and Justin Solomon Recitation 15
Recitation 15
Dynamic Programming
Dynamic Programming generalizes Divide and Conquer type recurrences when subproblem de-
pendencies form a directed acyclic graph instead of a tree. Dynamic Programming often applies to
optimization problems, where you are maximizing or minimizing a single scalar value, or counting
problems, where you have to count all possibilities. To solve a problem using dynamic program-
ming, we follow the following steps as part of a recursive problem solving framework.
2. Relate subproblem solutions recursively x(i) = f (x(j), . . .) for one or more j < i
4. Base cases
• State solutions for all (reachable) independent subproblems where relation doesn’t ap-
ply/work
5. Original problem
6. Time analysis
P
• x∈X work(x), or if work(x) = O(W ) for all x ∈ X, then |X| · O(W )
• work(x) measures nonrecursive work in relation; treat recursions as taking O(1) time
Recitation 15 2
Implementation
Once subproblems are chosen and a DAG of dependencies is found, there are two primary methods
for solving the problem, which are functionally equivalent but are implemented differently.
• A top down approach evaluates the recursion starting from roots (vertices incident to no
incoming edges). At the end of each recursive call the calculated solution to a subproblem
is recorded into a memo, while at the start of each recursive call, the memo is checked to see
if that subproblem has already been solved.
Top down is a recursive view, while Bottom up unrolls the recursion. Both implementations are
valid and often used. Memoization is used in both implementations to remember computation from
previous subproblems. While it is typical to memoize all evaluated subproblems, it is often possi-
ble to remember (memoize) fewer subproblems, especially when subproblems occur in ‘rounds’.
Often we don’t just want the value that is optimized, but we would also like to return a path of
subproblems that resulted in the optimized value. To reconstruct the answer, we need to maintain
auxiliary information in addition to the value we are optimizing. Along with the value we are
optimizing, we can maintain parent pointers to the subproblem or subproblems upon which a
solution to the current subproblem depends. This is analogous to maintaining parent pointers in
shortest path problems.
The player wins the round if the value of the player’s hand (i.e., the sum of cards drawn by the
player in the round) is ≤ 21 and exceeds the value of the dealer’s hand; otherwise, the player
loses the round. The game ends when a round ends with fewer than 5 cards remaining in the deck.
Given a deck of n cards with a known order, describe an O(n)-time algorithm to determine the
maximum number of rounds the player can win by playing simplified blackjack with the deck.
Recitation 15 3
Solution:
1. Subproblems
• Choose suffixes
• x(i) : maximum rounds player can win by playing blackjack using cards (ci , . . . , cn )
2. Relate
3. Topo
4. Base
5. Original
• Solve x(i) for i ∈ {1, . . . , n + 1}, via recursive top down or iterative bottom up
• x(1): the maximum rounds player can win by playing blackjack with the full deck
6. Time
• # subproblems: n + 1
• work per subproblem: O(1)
• O(n) running time
Recitation 15 4
Solution:
1. Subproblems
2. Relate
• The first line must break at some word, so try all possibilities
• x(i) = min{b(i, j) + x(j + 1) | i ≤ j < n}
3. Topo
4. Base
5. Original
6. Time
• # subproblems: O(n)
• work per subproblem: O(n2 )
Recitation 15 5
Optimization
1. Subproblems
2. Relate
P
• x(i, j) = k wk takes O(j − i) time to compute, slow!
• x(i, j) = x(i, j − 1) + wj takes O(1) time to compute, faster!
3. Topo
4. Base
5. Original
6. Time
• # subproblems: O(n2 )
• work per subproblem: O(1)
• O(n2 ) running time
MIT OpenCourseWare
https://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms