Recursion
Recursion basically is function calling itself, when you call a function - from another function i.e functionA() calling functionB() or functionC() calling itself, it will create an stack, and the idea of calling functionX() is basically solving the smaller problems or solving the problems with small use case or inputs. Recursion technique is widely used if you want to Break the problem into small usecases and use the result or the output of the usecase to solve the larger problem.
lets take an example - Factorial of the number n! ? the nth factorial would be n!= n * (n-1)! example 5! = 5 * 4!
Different Algorithms where the recursion is used extensively
1. Divide and Conquer
2. Backtracking
3. Tree Data structure - iteration
Common Steps of recursion
The first step is to identify the Base case, The base case defines the edge case or the smallest part of the problem, beyond which you cannot break the problem, example in case of the factorial function base case will be ZERO or ONE.
The Hypothesis i.e. The reasoning - how do we want to break this problem ?
Induction - solving the problem.
Problems
1. Josephus problem (Reduction Problem) - this is an very popular problem, there are many techniques for solving the problem, but lets try to solve this problem using recursion.
2. Tower of Hanoi (Move from A -> B )-
Base case ? The base case is the last pending disk that needs to be moved to destination
3. Knapsack problem (The Choice / Decision tree)
4. Generate all subsequences (Decision Tree + )
5. Generate all the permutation of String (Backtracking)
6. Knapsack problem using the Dynamic Programming
1. IBH - Induction Base Hypothesis
2. Choice Diagram, i.e. create the decision tree
1. Tower Of Hanoi
2. All Permutation of Strings
3. No of ways to reach from Point A to Point B using the GRID
4. Knapsack - Bounded & unbounded
5. Sliding Window problem
Comments
Post a Comment