Divide and Conquer
Divide and Conquer
In this, a problem is divided into sub-problems and the solutions of these sub-problems
are combined into a solution for the large problem. It has three steps:
i. Divide- the problem into a number of subproblems that are smaller instances of the
same problem.
ii. Conquer- the subproblems by solving them recursively. If the subproblem sizes are
small enough, however, just solve the subproblems in a straightforward manner.
iii. Combine- the solutions to the subproblems into the solution for the original problem.
Problem
Divide
Solution
T(1) , n=1
T(n) = a.T(n/b) + f(n) , n > 1
Derivation of Recurrence Relation: Let P be a problem, which is divided into a set of sub-
problems P1, P2 and so on. Let the size of the problem be n which is equal to 2k.
i.e. Size of problem-> n=2k and also assume the size of the whole tree=k.
So, P As n=2 k
P1 P2 n/2=2k/2=2k-1
P3 P4 P5 P6 n/4=2k/4=2k/22=2k-2
P7 P8 Similarly, n/8=2k-3 so on.
And so on……
Divide till the problem becomes small or equal to 1 (20) so that we can not divide it further.
To determine the time complexity in terms of Recurrence relation:
Let P be a problem of size n where T(n) represents the Time Complexity of problem P.
So, T(n) = T(n/2) +T(n/2) +f(n)
Time required to divide the problem Cost-function required to combine the solution
= 2. T(n/2) +f(n)
If the problem is divided into three sub-problems (P1, P2, P3) then T(n)=3T(n/2)+f(n)
Therefore, in a general way, T(n) = a. T(n/b)+f(n) when n>1
x[0] x[5]
BEG = 0 END = 5
ITEM that we want to search
So, MID = (BEG + END)/2
i.e MID = (0 + 5)/2 = 5/2 ≈ 3
So, DATA [MID] = 3rd element i.e. 20.
Here, ITEM < DATA [MID]
So, END = MID-1
i.e END = 3-1=2
Also, BEG = 0
So, MID = (BEG + END)/2 i.e. 0+2/2 = 1st element which is 10.
This means SEARCH IS SUCCESSFUL
The Complexity of the Binary Search
One approach is to go with Linear Search but it will take time so we are finding Minimum and
Maximum using Divide and Conquer technique.
Here, there are 8 elements and you have to find min and max numbers using DAC.
So, take the first element as i i.e i=9
And last element as j i.e. j=11 where i and j are the pointers that are pointing towards
the first and last element.
Now, find MID.
MID=(i+j)/2
This will divide the given list into smaller sub-list (That’s what we do in DAC)
Repeat the above steps till we are left with 2 elements in the list because we want to
compare them and find min and max so for comparison, we at least need 2 elements in
the list.
Compare them
Now, the COMBINE step occurs, start comparing the elements such that the min
element will be compared with the min AND
the max element will be compared with the max.
Repeat the same process till you get the min and max elements from the complete list.
This way, DAC works in finding the minimum and maximum elements from a given
list of numbers.
Quick Sort:
QuickSort is one of the different sorting techniques which is based on the concept
of Divide and Conquer just like Merge Sort.
But in Quick Sort, all the heavy lifting (major work) is done while dividing the array
into subarrays while in the case of Merge Sort, all the real work happens during the
merging of the arrays.
It is also called the Partition-Exchange Sort because it divides the big problem
into sub-parts, swapping of elements takes place and it follows the DAC
approach.
How does QuickSort work?
Selection of Pivot element: Pivot element can be any element from the array. It
can be the first element, the last element or any random element.
In the end, we will take the +∞ symbol because if our array is like 35 22 18 12 9 4 where
v=35 and P=22 so when we compare other elements with v then the rest of all elements are
lesser than P so it will move on. Therefore, to stop P we need some +∞ at the end. But for Q,
we will not take -∞ because when we are comparing Q with the pivot element, we are looking
for the element lesser than or equal to the Pivot element that is we are moving towards LHS so
when we compare other elements with v while moving towards LHS then at the end, there will
be a pivot element somewhere so Q will stop there. So, this is the concept of how P and Q are
moving.
Step 1 - Consider the first element of the list as a pivot (i.e., the element at the first position in
the list).
Step 2 - Define two variables P and Q. Set P and Q to the first and last elements of the list
respectively.