Dynamic Programming

Dynamic programming is a method for developing algorithms that are improvements over traditional recursive algorithms. Richard Bellman introduced and developed it into a formal methodology. Dynamic programming applies to problems —

  1. that are composed of smaller and simpler sub-problems.
  2. whose solutions are combinations of the solutions of its sub-problems.
  3. whose sub-problems overlap with each other.

For example, generating the Fibonacci series is a good candidate for dynamic programming, because —

  1. F[n] can be be broken down into subproblems F[n-1] and F[n-2].
  2. F[n] is the sum of F[n-1] and F[n-2].
  3. F[n-1] itself depends on F[n-2], (and also F[n-3], on which F[n-2] depends).

What are the two approaches to dynamic programming?

There are two approaches to dynamic programming.

  • Top-down approach (memoization): This approach is regular recursion with an added memoization step. Memoization enables the reuse of solutions to sub-problems from past computations. Pure functions simplify this memoization as their output depends solely on the input.

    This approach is comparatively slower than bottom-up as recursion is still an expensive operation in itself.

  • Bottom-up approach (tabulation): This approach works by identifying the sub-problems as the building blocks of the higher-level problems. By solving the lowest-level problems first and tabulating their solutions, the algorithm can directly evaluate solutions to higher-level problems.

    This approach is faster compared to the top-down approach as each sub-problem is only encountered once.

Automate objective parts of code reviews

Automate objective parts of code reviews

Get started