Skip to content

Dynamic Programming / DP

Steps

Source: https://ocw.mit.edu/courses/6-006-introduction-to-algorithms-spring-2020/resources/lecture-15-dynamic-programming-part-1-srtbot-fib-dags-bowling/

Acronym: SRTBOT

  • Sub-problem
    • Use words to describe what the subproblem means
  • Recurrence
    • Express sub-problem as a function of sub-problems
  • Topological order
    • Decide what order to loop through the variables, so that the dependencies are acyclic.
  • Base case
  • Original problem
  • Time complexity
    • Find number of sub-problems
    • Find time taken per sup-problem

Info

The topological ordering is really only useful when you're doing bottom-up dp.

Other tips

  • Think about how you would brute force all possiblities
  • Think in terms of Graphs, sometimes it might be easier
  • Divine inspiration

Push vs Pull DP

This distinction affects how you formulate the recurrence.

Pull DP

To compute dp[i][j], you look at all previous states that dp[i][j] depends on, and combine them.

The question asked is: "Which past states can help me compute this?"

Push DP

Given a finalised state dp[i][j], you look at all future states that it can contribute to, and update them.

The question asked is: "Given what I know, which future states can I help solve?"

Applications

  • Text justification