5. DP

Pseudo-polynomial: an algos running time is polynomial in the numerical value of the input, but not polynomial in the input length (number of bits). If you added one bit, its numerical value would double instead of increase polynomially, which would lead to an exponential runtime.

General DP Template

  1. Define the state (subproblem)
  2. Define the recurrence
  3. Define base cases
  4. Choose a computation order
  5. Extract the final answer
  6. Analyze runtime and space

Fibonacci Numbers

Compute the -th Fibonacci number.

State: = -th Fibonacci number

Base cases:

Recurrence:

Runtime: Space complexity: (store only last two values)

Maximum Subarray Sum (Kadane)

Find the maximum sum over all contiguous subarrays.

State: = maximum subarray sum ending at index

Base case:

Recurrence:

Extraction:

Runtime: Space complexity:

Jump Game (Minimum Jumps)

Minimum number of jumps to reach position .

State: = farthest index reachable with exactly jumps

Recurrence:

Extraction: Smallest such that

Runtime: Space complexity:

Longest Common Subsequence (LCS)

Length of the longest common subsequence of two strings.

State: = LCS length of prefixes and

Base cases:

Recurrence:

  • If :
  • If :

Runtime: Space complexity: (optimizable to )

Edit Distance (Levenshtein Distance)

Minimum number of insertions, deletions, and substitutions to transform one string into another.

State: = edit distance between and

Base cases:

Recurrence:

where if , else .

Runtime: Space complexity: (optimizable)

Subset Sum

Determine whether a subset sums to a target value .

State: = whether sum is achievable using the first elements

Base cases:

  • for all (can always add to zero)
  • for (nothing can never add to a nonzero integer)

Recurrence:

Runtime: (pseudo-polynomial) Space complexity:

Knapsack (0/1 Knapsack)

Maximize total value subject to weight limit .

State: = maximum value using first items with capacity

Base cases:

Recurrence:

where is the weight and the value of the -th item.

Runtime: Space complexity: (optimizable to )

Matrix Chain Multiplication

Example of range DP

Minimize the number of scalar multiplications when multiplying many matrices like by making use of matrix multiplication’s associativity ( vs and so on).

State: = minimum cost to multiply matrices through

Base case:

Recurrence: Try all possible splits:

where is the number of cols of the -th matrix (since the num rows must match the number of columns, the -th matrix has dimension ).

The number of rows after performing matrix mult. always equals the number of rows of the left matrix. The number of cols always equals the number of rows of the right matrix. Since this telescopes:

  • : number of rows of the left matrix
  • : number of cols of left and therefore number of rows of right matrix (dim must match)
  • : number of cols of right matrix

Calculation order: At the beginning, we only know the cost of multiplying two individual matrices. Therefore, we build up our solution by parenthesizing ever larger groups of matrices. This translates to starting with the diagonal and working upward.

Runtime: Space complexity: