Computers and Technology

# IS RECURSION DYNAMIC PROGRAMMING?

Dynamic programming and excursion are both important programming concepts that you should learn to prepare for competitive programming. You can find more information at algo.monster. ### Recursion, what is it?

Dynamic programming (DP) and recursion are terms highly reliant on; Unless you know recursion, you can’t learn DP.

Recursion is a programming technology that calls itself a programming function.

Two parts consist of each recursion function.

• Code to run in the recursive termination or base condition
• A single function is recurrently called here until the basic requirement is fulfilled.

Here, only one function is recursively called until the basic condition is met.

Recursion is very useful if your programs have to be divided into several parts, and the output of the first part depends on the previous position.

### [Example] Recursion of the Fibonacci series.

Take an example for a Fibonacci series generation:

Series of Fibonacci: 1, 1, 2, 3, 5,…

First, in the Fibonacci series, there are two numbers 1. Then, added the previous two digits of the Fibonacci sequence are used to calculate the next number.

By formulating the problem as below algorithm, we can calculate this number.

fib (n) = 1; if n < 2

fib (n) = fib(n-1) + fib(n-2)

“n < 2” is a basic condition here in the first line.

A recursive function call calculates the Fibonacci number.

This is how it looks, if you calculate the nth Fibonacci number.

fib(n) = fib(n-1) + fib(n-2)

fib(n-1) = fib(n-2) + fib(n-3)

fib(n-2) = fib(n-3) + fib(n-4)

……………………………

…………………………..

…………………………..

fib(3) = fib(2) + fib(1)

The fib(n) is divided into fib(n-1) and fib(n-2) sub-problems .

Furthermore, fib(n-1) is split into two fib(n-2) and fib(n-3) and so forth, subproblems.

The recurring function is a tree called.

### Recursion of the Fibonacci series in C:

For the Fibonacci series, we can write the recursive C program.

 1 2 3 4 5 6 7 8 9 #include int fib (int n) { if (n < 2) return 1; return fib(n-1) + fib(n-2); } void main() { printf(“The Fibonacci number for index 6 = %d”,fib(6)); }

### PRODUCT:

The Fibonacci number for index 6 = 8

It’s all a programming recursion.

Looking at the above diagram of Fibonacci, you can see that fib(4) is calculated twice. It means that two additional processing power carries out the same task repeatedly.

It is where dynamic scheduling is required.

The question now is, how different is the dynamic programming of recursion.

### What’s Dynamic programming recursion?

It is one of the special methods to resolve questions on programming. In a programming contest, it’s also called DP.

DP is usually used to fix problems that involve the next steps

Divide the problem into several small issues. Maintain the range of small problem results.

Check if the problem has been solved earlier when solving every problem. If so, take the result from the outcome array again rather than the same sub-problem.

Fuse the results of the subproblem into the outcome.

Since we store the solution to any subproblem for future use, it needs additional memory to save the data.

It is called a memorization process.

Dynamic programming mainly aims at logically optimizing the programming code.

Several subproblems may contain this problem. Only at once has the same problem been solved. It reduces the additional workload.

### Example] Dynamic Programming of the Fibonacci Series.

Look at the picture above. Many values are repeatedly measured in recursion as fibre (4).

It gives additional overhead processing to calculate the Fibonacci value for 4.

What if we store and use the fib(4) calculated value next time?

The dynamic schedule comes here.

Fibonacci series with memoization dynamic programming.

 1 2 3 4 5 6 7 8 9 10 11 12 include int fib (int n) { arrFib = {0}; arrFib = 1; arrFib = 1; for (int i = 2; i

### PRODUCT:

The Fibonacci number for index 6 = 8

We calculate the value of the Fibonacci series and store it in the database array instead of regularly calling the function (memorization technique).

We trade memory space here for processing time in dynamic programming.

### Difference between dynamic and recursion programming.

How does this programming differ from each other?

When you look at the final output of the program, recursion, as well as dynamic programming, do the same.

However, both of them are logically different when the actual program is running.

### Dynamic Programming advantages over recursion

• It reduces the line code since it is a recursive programming technique.
• One of the main advantages of dynamic planning is that we accelerate processing using previously calculated references.

### Dynamic programming disadvantages over recursion.

• It needs a lot of memory to store the calculated results of each subproblem without ensuring whether or not the stored value is used.
• The output value is stored many times and will never be used during execution in the following subproblems. It leads to unnecessary use of memory.
• Functions are recurringly named in DP. The memory stack continues to grow.

### Which one should be used when?

To get the idea behind the DP first.

• Divide the problem into several subproblems and save each problem’s results.
• Instead of recalculating the same subproblem, we can use the old reference from the previously calculated subproblem.
• Once all the subproblems are calculated, the output is conquered for the final production.

Now decide what your program should use.

• You can use recursion if you have limited memory for executing the code and don’t bother about processor speed. For example, memory is determined to complete the application when you are developing a mobile application.
• Use dynamical programming if you want to run your program quicker and do not have memory limitations.

Recursion and dynamic programming are key concepts for mastering any language.

These are generic concepts, and you can see them in most of the genre. In different programming languages, there can be a syntactic difference in the definition and recurring function.

## How can I study and Master DP?

You should know the recursion to solve the dynamic programming problem. Take a good look at the recurrent problem resolution. One of the basic examples of recursive issues is the Fibonacci series.

It is essential to understand the theory of dividing a problem into sub-problems. Learn to store the results in the array.

Knowing how it is used in many of DP’s problems and practices can increase your understanding.

Practice is top of all the topics discussed here as an expert in the DP issue.

Solve as many issues as possible. It gives you a considerable understanding and logic for dynamic problems.

Check Also
Close