Dynamic programming

 is an optimization technique that is used to optimize recursion problems. When we have a large number of recursion calls, in which each subproblem can be called many times with the same result, we can memorize these results so that we can avoid re-computing them when we subsequently solve problems of larger sizes. As a result, we can reduce the time complexity from exponential to polynomial.

Tabulation vs. memorization
There are two basic approaches to store the values corresponding to subproblems, so that values for the subproblems can be reused:
 * Tabulation: bottom up.
 * Memorization: top down.

Tabulation
Tabulation means to solve the problem in a bottom-up manner.


 * Example : Fibonacci sequence
 * For an input of value n:
 * Create an array of size n+1.
 * Set,.
 * For, fill in the table from left to right.
 * Return.

Here we start from the smallest subproblem (i = 0), and then iteratively fill in the result table. Every state can be computed directly from the previously computed value. So we call this the tabulation method.

Memorization
Another approach is memorization, which means solving the problem in a top-down manner.

This approach involves recursive calls to use values stored from subproblems.


 * Example : Fibonacci sequence
 * For an input of value n:
 * Create an array of size n+1.
 * For, use the recursive call:
 * if
 * 1 if
 * 0 if
 * Return.

Optimal substructure property
The optimal substructure property means that a problem's optimal solution can be obtained using the optimal solutions of its subproblems.


 * Example : Fibonacci sequence
 * The Fibonacci sequence problem satisfies the optimal substructure property, since the final solution and all intermediate subproblem solutions can be obtained using previous subproblem solutions, and the solution to each subproblem is an optimal solution.

Solving a dynamic programming problem
There are four major steps to solve a dynamic programming problem:
 * 1) Identify whether the problem is a dynamic programming problem.
 * 2) Determine a state expression with least parameters.
 * 3) Establish the transitional relationship between states.
 * 4) Perform tabulation (or memorization). Fill in the state table.

Step 1: Identify the DP problem

 * Typically, problems that asks us to maximize or minimize certain properties, or problems that say to count the arrangements under certain conditions, can be solved using dynamic programming.
 * All dynamic programming problems satisfy the overlapping subproblems property. Some problems also satisfy the optimal substructure property. Once we observe these patterns, we can attempt to solve it using dynamic programming.

Step 2: Determine the states of a problem
The essential part of solving dynamic programming problems is establishing states and transition relationships.

A state is a step such that certain parameters of a subproblem (inputs) and values (outputs) are stored, so that it can be referred again on later computations for solving subproblems of larger sizes. We want to use as least parameters as possible so that we can reduce the state space size.

For example: in the Edit distance problem, we can use  to store the minimum edit distance between the two substrings   and.
 * Indices i, j are both exclusive.
 * For edit distance between an empty substring and a non-empty substring, define:
 * (i > 0)
 * (j > 0)
 * (j > 0)

Step 3: Defining transition relationships
After states are defined, we then need to define the transition relationship, which bridges the state of a subproblem and the states of its pertinent subproblems.

Still consider the Edit distance problem:

Step 4: Fill in the table
This is the easiest part. We just fill in the table step-by-step and get the result.

But there are additional things to notice:
 * Filling in the table may take multiple passes, as in Largest plus sign.
 * Depending on the problem, the result may be stored separately instead of as intermediate states in the table, as in Largest plus sign.

Sample problems

 * Edit distance
 * Largest plus sign