diff options
| author | mo khan <mo@mokhan.ca> | 2021-11-21 15:26:51 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2021-11-21 15:26:51 -0700 |
| commit | 771a9582dd36e81e5de81a914766c18860691f3f (patch) | |
| tree | c91c5bbf4f38426d494dead3930c06f8ee165a33 /notes.md | |
| parent | 575b7cbdbefca2aab2ce004465b126d0e1b89d36 (diff) | |
add some notes on dynamic programming
Diffstat (limited to 'notes.md')
| -rw-r--r-- | notes.md | 53 |
1 files changed, 53 insertions, 0 deletions
@@ -1469,3 +1469,56 @@ f(n) = n | tight bound | theta | Useful when you cannot get the exact polynomial for a function. + +## Dynamic Programming + +Dynamic Programming means tabular programming or Dynamic Planning. This term was +created before coding style programming was a thing. + +This is a design technique used to solve a class of problems. + +Example: Longest Common Subsequence (LCS) + +Important in computation biology like finding commonalities in two DNA strings. + +Given two sequences `x[1...m]` and `y[1..n]`, find *a* longest sequence common to +both. "a", not "the" LCS. + + +```plaintext +x: A B C B D A B | BDB +y: B D C A B A | BAB + | BDAB + | BCAB + | BCBA + = LCS(x, y) + * functional notation but not a function it's a relation. + * classic misuse of notation +``` + +Brute force algorithm for solving this algorithm. + * check every subsequence of x from 1..m (`x[1..m]`) to see if it's also a subsequence of `y[i..n]` + * analysis check: + * Q: How long does it take me to see if it's a subsequence of y? + * A: The length of y which is `O(n)`. + * Check = `O(n)` + * Q: How many subsequences of `x` are there? + * A: 2^m subsequences of `x` + * running time: `O(n*2^m)` (exponential time = slow!) + +Simplification: + +1. Look at length of `LCS(y, y)`. +2. Extend the algorithm to find LCS itself. + +Notation: `|s|` denotes length of seq s. + + +15.1 Dynamic Programming + +Dynamic Programming, like the divide and conquer method, solves problems by +combining solutions to subproblems. + +Divide and conquer algorithms partition the problem into disjoint subproblems, +solve the subproblems recursively, and then combine their solutions to solve the +original problem. |
