|
| 1 | +# Dynamic Programming |
| 2 | +This is the not for the DP part (4 lectures) of [MIT 6.006](https://www.youtube.com/watch?v=moPtwq_cVH8&list=PLUl4u3cNGP61Oq3tWYp6V_F-5jb5L2iHb&index=23). |
| 3 | + |
| 4 | +## What is DP |
| 5 | +~= "careful brute force" |
| 6 | +~= "subproblems" + "reuse" + guessing |
| 7 | + |
| 8 | +## 5 steps |
| 9 | +1. define subproblems |
| 10 | +2. guess (part of solution) |
| 11 | + - in step2(& 3): which subprob to use to solve larger subprob |
| 12 | + - in step1: add more subprob to guess/remember more (remember more information about past) (for complicate DP) |
| 13 | +3. relate subproblem solution |
| 14 | +4. recurse & memoize -> acyclic -> topo order OR build DP table bottom-up |
| 15 | + - For memoize: use array OR parent pointer (remember which guess was best) |
| 16 | +5. solve original problem (time = number of subprob * time/subprob) |
| 17 | + |
| 18 | +## For subproblems of strings / sequences |
| 19 | +- suffixes x[i:] O(n) |
| 20 | +- prefixes x[:i] O(n) |
| 21 | +- substrings x[i:j] O(n^2) |
| 22 | + |
| 23 | +## Examples |
| 24 | +### Fibonacci number |
| 25 | +1. subproblems: prefixes fib(n - 1), if memo O(1) each |
| 26 | +2. guess: sum of fib(n - 1) and fib(n - 2) |
| 27 | +3. fib(n) = fib(n - 1) + fib(n - 2) |
| 28 | +4. Easily turn to a DAG, each node indegrees is last two nodes. |
| 29 | +5. fib(n) |
| 30 | +```python |
| 31 | +# Recursive |
| 32 | +if n in memo: return memo[n] |
| 33 | +if n <= 2: f = 1 |
| 34 | +else: f = fib(n - 1) + fib(n - 2) |
| 35 | +memo[n] = f |
| 36 | +return f |
| 37 | + |
| 38 | +# Bottom up |
| 39 | +fib = {} |
| 40 | +for k in range(k): |
| 41 | + if k <= 2: f = 1 |
| 42 | + else: f = fib[k - 1] + fib[k - 2] |
| 43 | + fib[k] = f |
| 44 | +return fib[n] |
| 45 | +``` |
| 46 | +### Shortest paths |
| 47 | +### Text justification |
| 48 | +Split text into "good" lines, badness(i, j): how bad the line start with i, end with j - 1. Should minimize the total badnesss value. |
| 49 | +1. subproblems: suffixes words[i:], each sub O(n) |
| 50 | +2. guess: where to start the next line |
| 51 | +3. `DP(i) = min(i, j)(DP(j) + badness(i, j)), DP(n) = 0` |
| 52 | +4. topoOrder: n, n - 1, n - 1, ..., 0 |
| 53 | + total time: O(n ^ 2) |
| 54 | +5. original prob: DP(0) |
| 55 | +### Perfect information blackjack |
| 56 | +- Deck: c0, c1, ..., cn-1 |
| 57 | +- 1 player vs dealer |
| 58 | +- $1 bet/hand |
| 59 | +- Max income |
| 60 | +1. suffix ci |
| 61 | +2. guess: how many hits? |
| 62 | +3. |
| 63 | +``` |
| 64 | +BJ(i) = max({-1, 1, 0} + BJ(j - 1) for j in range(i + 4, n) if valid game) |
| 65 | +``` |
| 66 | +5. O(n^3) |
| 67 | +### Parenthesization |
| 68 | +Optimal evealuation of associative expression |
| 69 | +1. subprob: optimal evealuation of Ai*...*Aj-1 (substring) |
| 70 | +2. guess: outermost/last multiplation |
| 71 | +3. |
| 72 | +``` |
| 73 | +DP(i, j) = min(DP(i, k) + DP(k, j) + cost of (Aik * Akj) for k in range(i + 1, j)) |
| 74 | +DP(i, i + 1) = 0 |
| 75 | +``` |
| 76 | +time/sub = O(n) |
| 77 | +4. topoorder: increasing substring size |
| 78 | +5. DP(0, n) |
| 79 | +### Edit distance |
| 80 | +### Knapsack |
| 81 | +### Piano/Guitar fingering |
| 82 | +### Tetris training |
| 83 | +### Super Mario Bros I |
0 commit comments