Divide and Conquer Technique
Multi-Branched Recursion
Last updated
Multi-Branched Recursion
Last updated
A divide-and-conquer algorithm works by recursively breaking down a problem into two or more sub-problems of the same or related type, until these become simple enough to be solved directly. The solutions to the sub-problems are then combined to give a solution to the original problem.
To adopt a divide and conquer based algorithm, first thing is to find the base case. Base case denotes the simplest sub-problem that can be solved directly. Next is the implementation of the recursive case. Recursive case deals with dividing the problem into sub-problems and merging the solutions of the sub-problems.
Any D&C procedure uses the following template
In the case of finding the Fibonacci number, the base case is when n is either 0 or 1. So, two base cases are included. In the recursive case, two sub-problems are created for (n-1) and (n-2). Addition is the function used to merge (or) combine the solutions.
Intuition: The first number of Fibonacci series is 0 and the second number in the series is 1. All the remaining elements are obtained by finding the sum of the previous two terms in the series
The problem with D&C are the inherent drawbacks with recursion. Recursion usually takes lot of memory space to keep track of the recursive calls (recursion call stack).
Test your Understanding
Identify the maximum depth of recursion call stack created by Fibonacci(5)? Here, depth refers to number of stack frames or activation records in the call stack at any point in time.
Refer to the following link to understand the call stack and its activation frame here
Also Check the Fibonacci.mp4