Click here to Skip to main content
15,747,908 members
Articles / General Programming / Optimization
Technical Blog
Posted 10 Oct 2020


1 bookmarked

Dynamic Programming – What, How & When

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
10 Oct 2020CPOL5 min read
An optimization programming technique
In this post, we will take a look at dynamic programming which is an optimization programming technique.

There are many programming techniques, which when applied appropriately to a specific situation, leads to efficient results (either time wise or space wise or both). Dynamic Programming (DP) is one such optimization concept.


Not to be confused with Dynamic programming language or Dynamic type in C#

Image 1

What Is?

Dynamic programming is solving a complicated problem by breaking it down into simpler sub-problems and make use of past solved sub-problems.


Dynamic Programming is mainly an optimization over plain recursion. 

Intent of this post is to easily understand and visualize the concept of DP. Thus, let’s use Fibonacci series as an example to understand this in detail.


Fibonacci Series is a sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1.

0, 1, 1, 2, 3, 5, 8, 13, 21…

When To?

It is best applied to problems that exhibits Overlapping Subproblems & Optimal Substructure.

Following is how our Fibonacci problem fib(4) breakdown would look like:

Image 2

Problem tree for fib(4)

We can see that fib(0), fib(1) & fib(2) are occurring multiple times in the graph. These are overlapping subproblems.


Binary search tree would not fall into same category. There would no common subproblem by binary search definition itself.

We can see for optimal solution of fib(4), we would need optimal solution of fib(3) & fib(2) and so on. This tells it has optimal substructure.


Longest path problem does not exhibit as such. Optimal solution for longest distance between A & C might not be optimal for B & C that involves A.

How To?

It’s not an unknown or a new technique. Our brain does it almost all the time. Let’s use Fibonacci series as an example to understand this and then we will apply DP to solve it programmatically.

Logically …

Let’s say, I ask, what is the 4th number in the Fibonacci series? You would work from 0, 1, 1, etc. and come to fib(4) = 2. Now, if I ask, what is the 5th number? Would you restart from 0, 1, 1..? Or you would just make use of fib(4) you just solved and get fib(5)?

You see – it’s obvious, you will make use of previous solved result and right away come up with fib(5) instead of starting from 0. In programming language, this is tabulation using calculated fib(4) & fib(3).


Tabulation is a technique of starting from smallest sub-problem and storing the results of entries until target value is reached.

Let’s say, now I ask to calculate fib(8). Our natural way of working on it would be to first find fib(7) & fib(6). In order to know fib(7), we will figure out fib(6) & fib(5) and hence forth. With fib(7), we already traversed through fib(6). This is another approach where we calculated the next subproblem and keep on storing them in case of need later. In programming language, this is memoization.


Memoization is a technique of storing the results of expensive function calls and returning the cached result when the same inputs occur again.

DP breaks the problem into sub-problems and uses memoization or tabulation to optimize. We will understand about them with examples below.

Programmatically in Action …

In order to compare the optimization cost, we will use recursion as another way to solve the problem.

static void Main(string[] args)
    Stopwatch stopwatch = new Stopwatch();

    Console.WriteLine($"Fibonacci Recursive:");
    for (int i = 30; i <= 50; i+=5)
        var _ = FibonacciRecursive(i);
        Console.WriteLine($"{i}th took time: ({stopwatch.Elapsed})");

    Console.WriteLine($"Dynamic Programming:");
    for (int i = 30; i <= 50; i+=5)
        var _ = FibonacciDPTabulation(i);
        Console.WriteLine($"{i}th took time: ({stopwatch.Elapsed})");

static long FibonacciRecursive(int number)
    if (number <= 1)
        return number;
    return FibonacciRecursive(number-2) + FibonacciRecursive(number - 1);

static long FibonacciDPTabulation(int number)
    long[] arr = new long[100];
    arr[0] = 1; arr[1] = 1; 
    for( int x=2; x <= number; x++)
        arr[x] = arr[x-1] + arr[x-2];
    return arr[number];

With the above code, we got the following output:

  Recursive Dynamic Programming (Tabulation) Dynamic Programming (Memoization)
30th took time (00:00:00.0090536) (00:00:00.0002756) (00:00:00.0183122)
35th took time (00:00:00.0908688) (00:00:00.0000037) (00:00:00.0000009)
40th took time (00:00:00.9856354) (00:00:00.0000006) (00:00:00.0000011)
45th took time (00:00:10.7981258) (00:00:00.0000006) (00:00:00.0000008)
50th took time (00:02:24.8694889) (00:00:00.0000006) (00:00:00.0000008)

Difference is astonishing! DP is too fast in comparison. Just look at the 50th time difference.


With simple iterative approch, Fibonacci Series can be calculated in the same ballpark time as of DP. Current example is just to keep things simple and understand the DP concept. Please look at the end of post for common examples that would clarify where DP would be of real help over recurrsion. (& iterative approach would be difficult to code)

The above approach of DP is considered Bottom-Up approach as we started with bottom (lowest term) and then moved to the highest one. This is tabulation. We keep on filling on the cache here till we reach the target.

Alternatively, there is a Top-down approach. We start solving from highest term and store solutions of sub problems along the way. This is memoization. A code for this approach would look like:

private static Dictionary<int, long> memo 
                    = new Dictionary<int, long>();

static long FibonacciDPMemoization(int number)
    if (number == 0 || number == 1) {
        return number;
    // see if we've already calculated this
    if (memo.ContainsKey(number)) {                
        return memo.GetValueOrDefault(number);

    long result = FibonacciDPMemoization(number - 1) 
                + FibonacciDPMemoization(number - 2);
    memo.Add(number, result);

    return result;

memoization is sometimes simpler to understand and write code because of it’s a natural way of solving.


Generally, tabulation outperforms memoization by a constant factor. This is because of no overhead for recursion and can be stored as a pre-allocated array. We first calculate and store the value of a sub problem repeated most number of times here.

Few Common Examples?

  • Tower of Hanoi
  • Checkerboard
  • Egg dropping puzzle
  • Matrix chain multiplication

Details of these above problem can be read here.


Apart from avoiding problems that do not have Overlapping Subproblems & Optimal Substructure, one needs to understand we are doing a trade here – space for time!

We can always discuss that though DP is using extra space to store the sub-problems data, but in turn helps save in memory calculation which could be expensive. Thus, in case of constrained space, we need to evaluate usage of DP.

Keep learning!.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Architect Intuit India
India India

A software professional for more than 17+ years building solutions for Web and Desktop applications.

Currently working at Intuit India.

Website: Learn By Insight
Github: Sandeep Mewara
LinkedIn: Sandeep Mewara

Strongly believe in learning and sharing knowledge.

Comments and Discussions

Questiontail recursion in C# ... and, other Fibonacci algorithms Pin
BillWoodruff12-Oct-20 22:15
professionalBillWoodruff12-Oct-20 22:15 
AnswerRe: tail recursion in C# ... and, other Fibonacci algorithms Pin
Sandeep Mewara13-Oct-20 1:51
mveSandeep Mewara13-Oct-20 1:51 
GeneralRe: tail recursion in C# ... and, other Fibonacci algorithms Pin
BillWoodruff13-Oct-20 6:44
professionalBillWoodruff13-Oct-20 6:44 
GeneralRe: tail recursion in C# ... and, other Fibonacci algorithms Pin
Sandeep Mewara14-Oct-20 6:44
mveSandeep Mewara14-Oct-20 6:44 
GeneralRe: tail recursion in C# ... and, other Fibonacci algorithms Pin
BillWoodruff14-Oct-20 7:27
professionalBillWoodruff14-Oct-20 7:27 
SuggestionA faster tabulation ? Pin
Patrice T12-Oct-20 11:36
mvePatrice T12-Oct-20 11:36 
GeneralRe: A faster tabulation ? Pin
BillWoodruff12-Oct-20 22:02
professionalBillWoodruff12-Oct-20 22:02 
GeneralRe: A faster tabulation ? Pin
Patrice T13-Oct-20 0:32
mvePatrice T13-Oct-20 0:32 
GeneralRe: A faster tabulation ? Pin
Sandeep Mewara13-Oct-20 1:41
mveSandeep Mewara13-Oct-20 1:41 
GeneralRe: A faster tabulation ? Pin
Sandeep Mewara13-Oct-20 2:09
mveSandeep Mewara13-Oct-20 2:09 
Questionconnotations of "dynamic programming" ? Pin
BillWoodruff12-Oct-20 2:43
professionalBillWoodruff12-Oct-20 2:43 
AnswerRe: connotations of "dynamic programming" ? Pin
Sandeep Mewara13-Oct-20 1:37
mveSandeep Mewara13-Oct-20 1:37 
QuestionBenchmark the 3 methods Pin
Patrice T11-Oct-20 18:26
mvePatrice T11-Oct-20 18:26 
AnswerRe: Benchmark the 3 methods Pin
Sandeep Mewara11-Oct-20 20:39
mveSandeep Mewara11-Oct-20 20:39 
AnswerRe: Benchmark the 3 methods Pin
Sandeep Mewara11-Oct-20 21:07
mveSandeep Mewara11-Oct-20 21:07 
GeneralRe: Benchmark the 3 methods Pin
Patrice T12-Oct-20 0:47
mvePatrice T12-Oct-20 0:47 
AnswerRe: Benchmark the 3 methods Pin
Sandeep Mewara13-Oct-20 1:32
mveSandeep Mewara13-Oct-20 1:32 
GeneralPoor Comparison Pin
SledgeHammer0110-Oct-20 5:23
SledgeHammer0110-Oct-20 5:23 
GeneralRe: Poor Comparison Pin
Sandeep Mewara10-Oct-20 9:37
mveSandeep Mewara10-Oct-20 9:37 
GeneralRe: Poor Comparison Pin
SledgeHammer0111-Oct-20 5:11
SledgeHammer0111-Oct-20 5:11 
GeneralRe: Poor Comparison Pin
Sandeep Mewara11-Oct-20 5:17
mveSandeep Mewara11-Oct-20 5:17 
GeneralRe: Poor Comparison Pin
Sandeep Mewara13-Oct-20 2:10
mveSandeep Mewara13-Oct-20 2:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.