07 - Data Structures - Divide and Conquer
07 - Data Structures - Divide and Conquer
Conquer
Divide and conquer approach breaks down a problem into multiple sub-problems
recursively until it cannot be divided further. These sub-problems are solved first and
the solutions are merged together to form the final solution.
The common procedure for the divide and conquer design technique is as follows −
Divide − We divide the original problem into multiple sub-problems until they
cannot be divided further.
Conquer − Then these subproblems are solved separately with the help of
recursion
There are several ways to give input to the divide and conquer algorithm design
pattern. Two major data structures used are − arrays and linked lists. Their usage
is explained as
Arrays as Input
There are various ways in which various algorithms can take input such that they can
be solved using the divide and conquer technique. Arrays are one of them. In
algorithms that require input to be in the form of a list, like various sorting algorithms,
array data structures are most commonly used.
In the input for a sorting algorithm below, the array input is divided into subproblems
until they cannot be divided further.
Then, the subproblems are sorted (the conquer step) and are merged to form the
solution of the original array back (the combine step).
Since arrays are indexed and linear data structures, sorting algorithms most
popularly use array data structures to receive input.
Consider the merge sort algorithm on linked list; following the very popular tortoise
and hare algorithm, the list is divided until it cannot be divided further.
Then, the nodes in the list are sorted (conquered). These nodes are then combined
(or merged) in recursively until the final solution is achieved.
Various searching algorithms can also be performed on the linked list data structures
with a slightly different technique as linked lists are not indexed linear data
structures. They must be handled using the pointers available in the nodes of the list.
Examples
Merge Sort
Quick Sort
Binary Search
Closest Pair
There are various ways available to solve any computer problem, but the mentioned
are a good example of divide and conquer approach.