Divide and Conquer is a fundamental algorithmic technique used to solve complex problems by breaking them down into smaller, more manageable subproblems. The idea is to divide the problem into smaller subproblems, solve each subproblem recursively, and then combine the solutions of the subproblems to solve the original problem.
A Divide and Conquer algorithm follows three main steps:
Divide: Split the problem into smaller subproblems. These subproblems should be of the same type as the original problem but simpler to solve.
Conquer: Solve each of the subproblems. If the subproblems are small enough, solve them directly; otherwise, apply the divide and conquer technique recursively to break them down further.
Combine: After solving the subproblems, combine the solutions of the subproblems to form the solution to the original problem.
To better understand how Divide and Conquer works, let’s take the example of Merge Sort, a popular sorting algorithm that follows the divide and conquer approach.
Divide: Split the unsorted array into two halves until each subarray contains a single element (which is trivially sorted).
Conquer: Recursively sort the two halves.
Combine: Merge the two sorted halves back together to form a sorted array.
Suppose we have an unsorted array:
[38, 27, 43, 3, 9, 82, 10]
We repeatedly divide the array into halves:
[38, 27, 43, 3, 9, 82, 10] into [38, 27, 43] and [3, 9, 82, 10][38, 27, 43] into [38] and [27, 43][27, 43] into [27] and [43][3, 9, 82, 10] into [3, 9] and [82, 10][3, 9] into [3] and [9][82, 10] into [82] and [10]Since each subarray contains only one element, each is considered trivially sorted. We now start merging them back together, sorting as we go.
[27] and [43] into [27, 43][3] and [9] into [3, 9][82] and [10] into [10, 82]Now, we begin merging the sorted subarrays:
Merge [38] and [27, 43] into [27, 38, 43]
Merge [3, 9] and [10, 82] into [3, 9, 10, 82]
Finally, merge [27, 38, 43] and [3, 9, 10, 82] into the fully sorted array:
[3, 9, 10, 27, 38, 43, 82]
Thus, the original array is now sorted.
The time complexity of Merge Sort can be derived by analyzing how many times we divide the array and how much work is done during each merge:
Divide: At each level of recursion, the array is divided in half, so the depth of the recursion tree is O(log n) (since the array is halved at each level).
Conquer: Each merge operation takes linear time, O(n), where n is the size of the array at that level of recursion.
So, the total time complexity is the depth of the recursion tree multiplied by the time to process each level (merge):
Since Merge Sort requires additional space to store the temporary subarrays during the merge step, its space complexity is:
Quick Sort is another widely used sorting algorithm that follows the divide and conquer technique. It selects a "pivot" element, partitions the array into two subarrays (one with elements smaller than the pivot, and one with elements greater than the pivot), and then recursively sorts the subarrays.
Steps:
Time Complexity:
Space Complexity: O(log n) for the recursive stack.
Binary Search is a search algorithm that works by repeatedly dividing a sorted array in half to find the target element. It eliminates half of the remaining elements in each step, which makes it very efficient for searching in large datasets.
Steps:
Time Complexity: O(log n)
Space Complexity: O(1) for iterative binary search and O(log n) for recursive binary search.
Strassen’s algorithm for matrix multiplication is a divide and conquer algorithm that multiplies two matrices in fewer steps than the standard matrix multiplication method, reducing the time complexity.
Time Complexity: O(n^log2(7)) ≈ O(n^2.81), which is faster than the standard O(n³) approach.
Divide and Conquer is a powerful algorithmic paradigm that solves complex problems efficiently by breaking them into smaller, more manageable subproblems. Algorithms like Merge Sort, Quick Sort, Binary Search, and Strassen's Matrix Multiplication are classic examples of this approach. While these algorithms are highly efficient for many problems, they also come with trade-offs, such as higher memory usage or recursion overhead. Nonetheless, Divide and Conquer remains a core technique in computer science due to its ability to optimize the performance of many common problems.
Open this section to load past papers