Dynamic Programming-Explained with Examples

Dynamic programming can be used to simplify complex processes with multiple subproblems. One important thing about DP is that it simplifies the process and optimizes it. Hence, professionals in programming, data analytics, and software development use dynamic programming to make their work simpler. In fact, learning how you can use dynamic programming in your projects is awesome, especially if you are entering a position that requires programming skills. Here, in this article, algo.monster discusses the following aspects of dynamic programming:

What is dynamic programming about?

And what are the main features of dynamic programming?

Also, what are the best methods for computing dynamic programming recursive code problems?

What is dynamic programming?

Programmers and computer engineers use dynamic programming to deal with optimization problems. For example, programmers can integrate dynamic programming into a software development project. And the algorithm used by DP breaks down complex coding problems to create subproblems. Then, programmers then can apply the optimal solution to the whole problem. This process, however, depends on which subproblem they have derived from the code.

What’s more, Dynamic programming also optimizes plain recursion by using the recursive solutions programmers find through the calculation of the subproblems. Because of this optimization process, programmers can eliminate the need to compute input data again later, when they need to add new code into the project.

Features of dynamic programming

Two important features make dynamic programming viable and efficient. They are:

Interconnected subproblems that overlap

In fact, you can regard these subproblems as just smaller versions of a larger problem. In the Fibonacci sequence, for example, each number is simply the sum of the two preceding numbers (0 1, 1, 2, 3, 4, 5, 8, and so forth). You can also break down the problem into smaller subproblems if you need to find the nth Fibonacci number in the sequence. As you solve the same subproblem over and over again, these subproblems will overlap. As a result, you can use dynamic programming to reduce complex programming tasks down to smaller pieces to avoid recalculation of the overlapping subproblems. This improves your efficiency.

Substructure that has optimal property

Having a substructure of optimal property means you can find the optimal solution by constructing all the solutions that you have found from solving each subproblem. That is how the original problem demonstrates the property of an optimal substructure. To optimize recursion in programming, each solution that you find from each subproblem must be applied to the entire problem. Each subproblem in the Fibonacci sequence has a solution you can apply to the next subproblem to find a number that will give the whole problem optimal substructure properties.

Dynamic programming approaches

what is dynamic programming

When dealing with dynamic programming problems, you can apply these two methods to your projects:

The top-down method with memoization tool

The top-down method means that you first solve the whole problem, then break it down into smaller problems. In this method, memoization is what helps it work. Actually, it solves larger problems by finding solutions to subproblems recursively and storing the results. Thus, memoization allows you to solve the problem once and not have to do it again. In other words, there’s no repetition. The top-down approach enables you to simply return the result that you saved as you solve the problem. Also, this lets you store results from problems you have already calculated.

The bottom-up method using tabulation

The bottom-up method, also known as the tabulation method, uses recursion to solve sub-problems. Dynamic programming uses a dimensional table to solve multiple subproblems in the bottom-up tabulation. Or it uses an n-dimensional table with n for a value of zero or larger. With what you’ve saved in the table, you can use the results of each subproblem to calculate the original problem as you solve them.

Examples of dynamic programming (using Fibonacci sequence)

The following example will show you how to use both dynamic programming methods to understand the Fibonacci sequence.

an example of dynamic programming

Example of using the top-down method

Use the Fibonacci sequence, where each number represents the sum of two previous numbers.

{0, 1, 1, 2, 3, 5, 8,…}

When you need to calculate the nth Fibonacci number, it is possible to solve the whole problem by understanding that the sum of two previous values will be the next number. You can use a formula to find the best solution to the problem without breaking it down into smaller subproblems. When n > 1, the formula can be used to compute the optimal solution using the top-down approach when solving for nth value 13.

Fib(n) = Fib(n – 1) + Fib(n – 2) =

Fib(13) = Fib(13 – 1) + Fib(13 – 2) =

Fib(n) = Fib(12) + Fib(11) = 23

You can use the top-down approach with memoization in this process. The memoization tool will save the results of 23 in your table. This will allow you to call your code string for additional tasks.

Example of using the bottom-up method of dynamic programming

In this example, you need to compute the nth number value in the series. Using the bottom-up method shows you how to use the Fibonacci sequence to break down the whole calculation. The same number sequence 0, 1, 2, 3, 5, 8, will result in 13 since the sum of five and eight gives a total of 13. Thus, you can use the bottom-up method to solve the whole problem Fib(n). Yet, the sequence equation: Fib(n) = Fib(n – 1) + Fib(n – 2) is applicable when n > 1. And, for each subproblem, you will need to find the optimal solution. Let’s say you want to find the next nth value for n = 23.

Fib(n) = Fib(n – 1) + Fib(n – 2) =

Fib(23) = Fib(23 – 1) + Fib(23 – 2) =

Fib(23) = Fib(22) + Fib(21) = Fib(43)

The value of Fib(43) will apply to the calculation of the next values in the series when you come to calculate n = 43:

Fib(n) = Fib(n – 1) + Fib(n – 2) =

Fib(43) = Fib(43 – 1) + Fib(43 – 2) =

Fib(n) = Fib(42) + Fib(41) = Fib(83) =

Fib(83) = Fib(83 – 1) + Fib(83 – 2) =

Fib(83) = Fib(82) + Fib(81) = Fib(163)

This optimal solution can be used for each subproblem to determine the next values in the Fibonacci sequence. Thus, it allows you to solve the original problem Fib(n) = Fib(n – 1) + Fib(n – 2), when n is greater than 1.

Leave a Reply