# Dynamic Programming | Set 1 (Overlapping Subproblems Property)

Dynamic Programming is an algorithmic paradigm that solves a given complex problem by breaking it into subproblems and stores the results of subproblems to avoid computing the same results again. Following are the two main properties of a problem that suggest that the given problem can be solved using Dynamic programming.

1) Overlapping Subproblems
2) Optimal Substructure

1) Overlapping Subproblems:
Like Divide and Conquer, Dynamic Programming combines solutions to sub-problems. Dynamic Programming is mainly used when solutions of same subproblems are needed again and again. In dynamic programming, computed solutions to subproblems are stored in a table so that these don’t have to recomputed. So Dynamic Programming is not useful when there are no common (overlapping) subproblems because there is no point storing the solutions if they are not needed again. For example, Binary Search doesn’t have common subproblems. If we take example of following recursive program for Fibonacci Numbers, there are many subproblems which are solved again and again.

```/* simple recursive program for Fibonacci numbers */
int fib(int n)
{
if ( n <= 1 )
return n;
return fib(n-1) + fib(n-2);
}```

Recursion tree for execution of fib(5)

```
fib(5)
/             \
fib(4)                fib(3)
/      \                /     \
fib(3)      fib(2)         fib(2)    fib(1)
/     \        /    \       /    \
fib(2)   fib(1)  fib(1) fib(0) fib(1) fib(0)
/    \
fib(1) fib(0)
```

We can see that the function f(3) is being called 2 times. If we would have stored the value of f(3), then instead of computing it again, we would have reused the old stored value. There are following two different ways to store the values so that these values can be reused.

a) Memoization (Top Down):
b) Tabulation (Bottom Up):

a) Memoization (Top Down): The memoized program for a problem is similar to the recursive version with a small modification that it looks into a lookup table before computing solutions. We initialize a lookup array with all initial values as NIL. Whenever we need solution to a subproblem, we first look into the lookup table. If the precomputed value is there then we return that value, otherwise we calculate the value and put the result in lookup table so that it can be reused later.

Following is the memoized version for nth Fibonacci Number.

## C/C++

```/* C/C++ program for memoized version for nth Fibonacci number */
#include<stdio.h>
#define NIL -1
#define MAX 100

int lookup[MAX];

/* Function to initialize NIL values in lookup table */
void _initialize()
{
int i;
for (i = 0; i < MAX; i++)
lookup[i] = NIL;
}

/* function for nth Fibonacci number */
int fib(int n)
{
if (lookup[n] == NIL)
{
if (n <= 1)
lookup[n] = n;
else
lookup[n] = fib(n-1) + fib(n-2);
}

return lookup[n];
}

int main ()
{
int n = 40;
_initialize();
printf("Fibonacci number is %d ", fib(n));
return 0;
}
```

## Python

```# Python program for Memoized version of nth Fibonacci number

# function to calcualte nth Fibonacci number
def fib(n, lookup):

# Base case
if n == 0 or n == 1 :
lookup[n] = n

# If the value is not calculated previously then calculate it
if lookup[n] is None:
lookup[n] = fib(n-1 , lookup)  + fib(n-2 , lookup)

# return the value corresponding to that value of n
return lookup[n]
# end of function

#-----Driver program to test the above function----
n = 34
# Declaration of lookup table
# Handles till n = 100
lookup = [None]*(101)
print "Fibonacci Number is ", fib(n, lookup)
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)
```

b) Tabulation (Bottom Up): The tabulated program for a given problem builds a table in bottom up fashion and returns the last entry from table.

## C/C++

```/* C program for tabulated version */
#include<stdio.h>
int fib(int n)
{
int f[n+1];
int i;
f[0] = 0;   f[1] = 1;
for (i = 2; i <= n; i++)
f[i] = f[i-1] + f[i-2];

return f[n];
}

int main ()
{
int n = 9;
printf("Fibonacci number is %d ", fib(n));
return 0;
}```

## Python

```# Python program Tabulated (bottom up) version
def fib(n):

# array declaration
f = [0]*(n+1)

# base case assignment
f[1] = 1

# calculating the fibonacci and storing the values
for i in xrange(2 , n+1):
f[i] = f[i-1] + f[i-2]
return f[n]

# Driver program to test the above function
n = 9
print "Fibonacci number is " , fib(n)
# This code is contributed by Nikhil Kumar Singh (nickzuck_007)
```

Output:
`Fibonacci number is 34 `

Both tabulated and Memoized store the solutions of subproblems. In Memoized version, table is filled on demand while in tabulated version, starting from the first entry, all entries are filled one by one. Unlike the tabulated version, all entries of the lookup table are not necessarily filled in memoized version. For example, memoized solution of LCS problem doesn’t necessarily fill all entries.

To see the optimization achieved by memoized and tabulated versions over the basic recursive version, see the time taken by following runs for 40th Fibonacci number.

Also see method 2 of Ugly Number post for one more simple example where we have overlapping subproblems and we store the results of subproblems.

We will be covering Optimal Substructure Property and some more example problems in future posts on Dynamic Programming.

Try following questions as an exercise of this post.
1) Write a memoized version for LCS problem. Note that the tabular version is given in the CLRS book.
2) How would you choose between Memoization and Tabulation?

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

References:

Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.

• ankit

@GFG
which approach is better bottom-up or top-down?

• Gaurav Kapoor

With Normal recursion OUTPUT for n=30 : time taken to calculate the fibonacci of 30 is 11 microseconds and value is 832040

With Memorization OUTPUT for n=30 : time taken to calculate the fibonacci of 30 is 1647 microseconds and value is 832040

Why its taking less time in Recursion ?? It should be the reverse way

• avidullu

@GeeksforGeeks Can you please remove the link to web.iiit.ac.in/avidullu That page has been removed.

• The Teacher

first Two links are giving 404 ERROR !!!!

• anjali

please give examples for top-down and bottom-up?

• myth17

2 of the Reference links are hitting 404.

#include

int unsigned long fc(int n)
{
int i;int unsigned long previousFib = 0, currentFib = 1,newFib;
if( n == 0)
return 0;
else for(i=1;i<n;i++) // loop is skipped if n=1
{
newFib= previousFib + currentFib;
previousFib = currentFib;
currentFib = newFib;

}
return currentFib ;
}

int main()
{
int val;
printf("\nEnter number you want to generate fb :");
scanf("%d",&val);
printf("new bottum to down :%lu",fc(val));
return 0;
}

• anon

can any DP problem be solved by both top-down and bottom-up approach??

• geekguy

Almost every problem can be solved by both top-down and bottom-up approach.

``` ```
/* Paste your code here (You may delete these lines if not writing code) */
``` ```
• mafia
``` ```
/* /* function for nth Fibonacci number */
int fib(int n)
{
if(lookup[n] == NIL)
{
if ( n <= 1 )
lookup[n] = n;//if(n==0) then lookup[n]=n;wrong??
else
lookup[n] = fib(n-1) + fib(n-2);
}

return lookup[n];
}*/
``` ```

here if (n==0){lookup[n]==1;}

• Himani

/* /* function for nth Fibonacci number */
int fib(int n)
{
if(lookup[n] == NIL)
{
if ( n <= 1 )
lookup[n] = n;/*right if series goes on as 0 1 1 2 3 5 .. */
else
lookup[n] = fib(n-1) + fib(n-2);
}

return lookup[n];
}*/

• shiwakant.bharti

@mafia
1.> Please consider the series as 0 1 1 2 3 as rightly said by @Himani.
2.> If you just the write the code to handle n==0 then it will break at n=1 as it will try to calculate it from n=0 and n=-1. This will end up in negative index of lookup. In Java you might get ArrayIndexOutOfBoundsException, in C/C++ (depends on versions) it might iterate and finally lead to segmentation fault/stackoverflow or something.

• Pulkit

Awesome Post..!!

``` ```
/* Paste your code here (You may delete these lines if not writing code) */
``` ```
• Neol

Which one is more efficient to store solutions of subproblems ?
In my view, Memoization will be efficient as we don’t have to fill complete lookup table but in tabulation we have to fill all entries . Correct me if I am wrong.

• jain.ankitk

Its just in memoization stack size would be O(n) as the function will recurse till base case before starting to fill the solution for subproblems.

``` ```
/* Paste your code here (You may delete these lines if not writing code) */
``` ```
• shrikant

will changing the fib code from
lookup[n] = fib(n-1) + fib(n-2)
to
lookup[n] = fib(n-2) + fib(n-1)
not make it better ?

• Anand
• Praveen

I think its better to write
lookup[n] = fib(n-2) + fib(n-1)

than

lookup[n] = fib(n-1) + fib(n-2)
nice comment..

• Praveen

srry for above…

It doesn’t matter about the order …

since we each time look up for the table.

• Anand
• Anand
• shubh

You have written Memoization as Top Down, but standard books say that Dynamic Programming is Bottom Up. Please explain.

• GeeksforGeeks

@shubh: CLRS book considers Memoization as a variation of Dynamic Programming that offers efficiency of Dynamic Programming while maintaining a top down strategy.

• Anand

Here is blog that has all solved DP problem frequently asked in interviews.

http://anandtechblog.blogspot.com/2011/01/amazon-question-dynamic-programming.html

• satya

@geeksfoegeeks..Great Post & Gud Work Keep Posting All Standerd DP Problems Please Keep it Up

Satya

• SDiZ

I just realize how flawed the wikipedia page is.

Definition: Solve an optimization problem by caching subproblem solutions (memoization) rather than recomputing them.

• kartik

@SDiZ: The Wikipedia page talks about general definition of Memoization in computer science as Memoization is not limited to algorithms. NIST DADS talks about algorithm specific definition. Following is another link for algorithm specific definition.

http://www.algorithmist.com/index.php/Memoization

• @GeeksForGeeks, Can You post the shortest path algo like bellmon ford , dijkshtra , all pair shortest path etc .

``` ```
/* Paste your code here (You may delete these lines if not writing code) */
``` ```
• SDiZ

.. is an algorithmic paradigm that solves a given complex problem by breaking it into subproblems ..

This is called “divide and conquer”.

Dynamic Programming is about memorization.

• GeeksforGeeks

@SDiZ: Both Dynamic Programming(DP) and Divide and Conquer(DC) solve problems by breaking them into subproblems. The key difference is: In DP, results of subproblems are stored in a table as these results might be needed later. In DC, results of subproblems are not stored. Memoization is one of the techniques to store results of subproblems.