freeCodeCamp/guide/english/computer-science/dynamic-programming/index.md

1.9 KiB

title
Dynamic Programming

Dynamic Programming

Dynamic Programming(DP) is a programming technique for solving problems where the computations of its subproblems overlap: you write your program in a way that avoids recomputing already solved problems. This technique is usually applied in conjunction with memoization which is an optimization technique where you cache previously computed results, and return the cached result when the same computation is needed again.

An example with Fibonacci's series which is defined as:

F(N) = F(N-1) + F(N-2)

This is the tree to find F(5):

Fibonacci serie's tree

To compute F(5) it will need to compute many times the same F(i). Using recursion:

def fib(n)
{
    if n <= 1:
        return n
    return fib(n-1) + fib(n-2);
}

And below is the optimised solution (using DP)

For F(5), this solution will generate the calls depicted in the image above, running in O(2^N).

Here is an optimised solution which uses DP and memoization:

lookup = {1 : 1, 2 : 1} # Create a lookup-table (a map) inizialized with the first 2 Fibonacci's numbers

def fib(n)
{
    if n in lookup: # If n is already computed
        return n # Return the previous computed solution
    else 
        lookup[n] = fib(n-1) + fib(n-2) # Else, do the recursion.
    return lookup[n]
}

Caching computed solutions in a lookup table, and query it before go recursion will let the program have a running time of O(N).

More Information:

What is dynamic programming on StackOverflow Difference between memoization and DP on StackOverflow Why DP rather than function calling or looping