Definition:
A divide and conquer algorithm is a strategy of solving a large problem by:
1. breaking the problem into smaller sub-problems
2. solving the sub-problems, and
3. combining them to get the desired output
To use the divide and conquer algorithm, recursion is very importance.
Time Complexity
The complexity of the divide and conquer algorithm is calculated using the master theorem.
Divide and Conquer Vs. Dynamic Approach
- The divide and conquer approach divides a problem into smaller subproblems; these subproblems are further solved recursively. The result of each subproblem is not stored for future reference, whereas, in a dynamic approach, the result of each subproblem is stored for future reference.
- Use the divide and conquer approach when the same subproblem is not solved multiple times. - - Use the dynamic approach when the result of a subproblem is to be used multiple times in the future.
Example: Fibonacci
//Divide and Conquer approach:
fib(n)
If n < 2, return 1
Else , return f(n - 1) + f(n -2)
//Dynamic approach:
mem = []
fib(n)
If n in mem: return mem[n]
else,
If n < 2, f = 1
else , f = f(n - 1) + f(n -2)
mem[n] = f
return f
//In a dynamic approach, mem stores the result of each subproblem.
Advantages of Divide and Conquer Algorithm
Application
1. Quick Sort
2. Merge Sort
3. Binary Sort