1 of 14

Dynamic Programming

2 of 14

XKCD

http://www.geeksforgeeks.org/travelling-salesman-problem-set-1/

3 of 14

What is Dynamic Programming?

  • Finding overlapping subproblems
    • Store subproblems in a table
    • So you don’t need to solve them over and over again

  • Simple example, naive Fibonacci - O(2n)
    • With memoization - O(n)
    • Exponential speedup!

4 of 14

History of Dynamic Programming

  • Coined by Richard Bellman to describe his mathematics research in 1950s

“We had a very interesting gentleman in Washington named Wilson. He was Secretary of Defense, and he actually had a pathological fear and hatred of the word, research. I'm not using the term lightly; I'm using it precisely. His face would suffuse, he would turn red, and he would get violent if people used the term, research, in his presence. You can imagine how he felt, then, about the term, mathematical.”

  • programming: planning resource allocation (a la 'Linear Programming')
  • dynamic: refers to a multi-stage process
    • plus “It's impossible to use the word, dynamic, in a pejorative sense.”

5 of 14

Boyer-Moore String Matching

  • Search right to left
  • Create a table of how far forward you can jump after n-matches then bad.
  • O(n+m) instead of O(nm)
    • Consider: find AAAAB in AAAAAAAAAAAAAAAAAAB
    • Or ABABABc in ABABABABABABABABABABABABABABABc

6 of 14

Needleman–Wunsch String Edit Distance

  • https://web.stanford.edu/class/cs124/lec/med.pdf
  • Find "edit distance" between two strings
  • Build a table: rows are index into one string, columns are index into other string
  • Dynamic programming corresponds to choosing between matching (diagonal), skipping a letter in one string or the other (horizontal and vertical).

Common variant: Smith-Waterman, which doesn't assume the start and end points are aligned. This is useful in nucleotide sequencing.

7 of 14

Needleman-Wunch Edit Distance Table

8 of 14

Optimization for a single variable

  • Input variables subject to physical constraints (e.g., factory floorspace)

  • Independent utility functions for activities (nonlinear, nonmonotonic)

  • Find maximum utility for input (the single variable we're optimizing for is "utility")

9 of 14

Discrete optimization dynamic programming

  • Enumerating X unique values for each of N variables takes O(XN)
  • But, utility from each variable is independent
    • Let’s add each new activity in one at a time

  • Calculating each Fk is O(X2)
    • O(X) for each sample of Fk(x), O(X) samples
  • Total: O(NX2) time, O(X) space

10 of 14

Discrete optimization - pseudocode

N=product types

R=amount of production available

F=profit from producing the best thing(s)

+ gi(k) )

// Add variables one at a time

// Check each possible total

// Try out xi == k

11 of 14

RNA Folding

Goal: use existing RNA fabrication to build functional nanostructures.

Need to find the lowest energy state to predict how RNA will fold.

12 of 14

RNA types of folds

13 of 14

RNA energy states

  • We can organize substructures as a matrix
  • Energy of a loop depends on energy of substructures
    • Interior loops is the largest search space - O(N2)

14 of 14

Dynamic Programming + inner loops

  • The size of the inner loop is constant on a diagonal
    • The ‘loop’ energy term drops out

  • Diagonals are shared
    • O(N) search space
  • O(N4) -> O(N3)