Recursion and Dynamic programming are two effective methods for solving big problems into smaller, more manageable subproblems. Despite their similarities, they differ in some significant ways.
Below is the Difference Between Recursion and Dynamic programming in Tabular format:
Features |
Recursion |
Dynamic Programming |
---|---|---|
Definition |
By breaking a difficulty down into smaller problems of the same problem, a function calls itself to solve the problem until a specific condition is met. |
It is a technique by breaks them into smaller problems and stores the results of these subproblems to avoid repeated calculations. |
Approach |
Recursion frequently employs a top-down method in which the primary problem is broken down into more manageable subproblems. |
Using a bottom-up methodology, dynamic programming starts by resolving the smallest subproblems before moving on to the primary issue. |
Base Case |
To avoid infinite loops, it is necessary to have a base case (termination condition) that stops the recursion when a certain condition is satisfied. |
Although dynamic programming also needs a base case, it focuses mostly on iteratively addressing subproblems. |
Performance |
Recursion might be slower due to the overhead of function calls and redundant calculations. |
Dynamic programming is often faster due to optimized subproblem solving and memoization. |
Memory Usage |
It does not require extra memory, only requires stack space |
Dynamic programming require additional memory to record intermediate results. |
Examples |
It include computing factorials, using the Fibonacci sequence. |
It include computing the nth term of a series using bottom-up methods, the knapsack problem |
Let’s take an example: Find out the Nth Fibonacci number
1) Using Recursion:
Below is the code of Fibonacci series using recursion:
C++
#include <iostream> int fibonacci_recursive( int n) { if (n <= 1) { return n; } else { return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2); } } int main() { int n = 5; int result = fibonacci_recursive(n); std::cout << result << std::endl; return 0; } |
Python
def fibonacci_recursive(n): if n < = 1 : return n else : return fibonacci_recursive(n - 1 ) + fibonacci_recursive(n - 2 ) print (fibonacci_recursive( 5 )) |
5
Time Complexity: O(2n), which is highly inefficient.
Auxiliary Space: Recursion consumes memory on the call stack for each function call, which can also lead to high space complexity. Inefficient recursive algorithms can lead to stack overflow errors.
2) Using Dynamic Programming:
Below is the code of Fibonacci series using Dynamic programming:
Python
def fibonacci_dp(n): fib = [ 0 ] * (n + 1 ) fib[ 1 ] = 1 for i in range ( 2 , n + 1 ): fib[i] = fib[i - 1 ] + fib[i - 2 ] return fib[n] print (fibonacci_dp( 5 )) |
5
Time Complexity: O(n), a vast improvement over the exponential time complexity of recursion.
Auxiliary Space: DP may have higher space complexity due to the need to store results in a table. In the Fibonacci example, it’s O(n) for the storage of the Fibonacci sequence.
Application of Recursion:
- Finding the Fibonacci sequence
- Finding the factorial of a number
- Binary tree traversals such as in-order, pre-order, and post-order traversals.
Application of Dynamic Programming:
- Calculation of Fibonacci numbers and storing the results
- Finding longest subsequences
- To identify the shortest pathways in graphs, dynamic programming techniques like Dijkstra’s.
Conclusion:
Recursion and dynamic programming both use common problem-solving techniques, although they focus differently on optimisation and memory usage. The nature of the issue and the intended outcome of the solution will determine which option is best.
Solve DSA problems on GfG Practice.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!