You are on page 1of 16

Module-2

DIVIDE AND CONQUER


General Method:
The Divide-and-conquer strategy suggests splitting the inputs of size ‘n’ into ‘k’
distinct subsets such that 1<k≤n, producing k sub problems. These sub problems must be
solved & then a method to be found to combine the solutions of sub problems (sub
solutions) to produce the solution to the original problem of size ‘n’. If the subproblems are
relatively large then divide & conquer strategy must be reapplied. For the reapplication
subproblems the divide & conquer strategy will be expressed as recursive algorithm.

HEMANTH T D, Dept. of ISE, Acharya Institute of Technology. Page 1


Example: Detecting a Counterfeit Coin:
Detecting a counterfeit coin problem will ask us to find the whether counterfeit coin
is present or not out of the given set of coins. The assumptions here is counterfeit coins are
lighter than the genuine ones. To find the counterfeit coin we are given with a machine that
compares the weights of two sets of coins, and tells us whether set is lighter or both the
sets have the same weight.
Let us assume that we are given with a 16 coin instance & consider it has a large
instance. The divide & conquer approach will work as follows.
 We divide the original instance into two or more smaller instances. Let us divide our 16
coin instance into two 8-coin instances, let those instances be A&B.
 Now we need to find whether set A or B has the counterfeit, for this we use the
weighing machine to compare the weights of the coin sets A & B. If both the sets have
the same weights, the counterfeit coin is not present in the given set of coins.
 If A & B have the different weights, the counterfeit coin is present & is present in the
lighter set.
 Now suppose particularly we need to identify the counterfeit coin, we have to define a
smaller instance to be one with two or three coins.
 In the above 16-coin instance problem, it is divided into two instances A & B of 8 coins.
By comparing the weights of these two instances, we determine whether or not the
counterfeit is present or not. If no counterfeit coin the algorithm will terminates.
 Otherwise we will take the subinstance that has the lighter weight, which is having
counterfeit coin. Suppose if A is lighter set, we divide it into two sets of 4 coins each, let
them be A1 & A2.
 These two sets are compared; one of the set must be lighter. Let A 2 will be the lighter
one, then A2 will be divided into two sets A21 & A22 of two coins each.
 These two sets A21 & A22 are compared; one of the set must be lighter. Let A21 will be the
lighter one, then A21 will be divided into two sets A211 & A212 of one coin each. Now
comparing the weights two sets A211 & A212 the coin with the lighter weight is the
counterfeit coin in the given set of 16 coins.
The below DandC algorithm initially invokes the function DandC(P), where P is the
problem to be solved. Small(P) is a Boolean valued function that determines whether the

HEMANTH T D, Dept. of ISE, Acharya Institute of Technology. Page 2


input size is small enough, such that the answer can be computed without further splitting.
Then the function S is invoked, otherwise the problem P is divided into smaller
subproblems, these subproblems P1, P2, P3, . . .,Pk are solved by recursive calling of DandC.
Combine is a function that determines the solution to P using the solutions to the k
subproblems.
Algorithm DandC(P)
{
if Small(P) then return S(P)
else
{
divide P into smaller instances P1, P2 , P3, . . . Pk, k≥1
Apply DandC to each of these subproblems;
return Combine(DandC(p1) , DandC(p2) , DandC(p3), . . ., DandC(pk))
}
If the size of P is n & the sizes of the k subproblems are n 1, n2, . . . , nk, then the computing
time of DandC is given by the following recurrence relation.
g(n) n small
T(n) =
T(n1)+T(n2)+. . .+T(nk)+f(n) Otherwise

Where,
 T(n) is the time for DandC on any input of size n.
 g(n) is the time to compute the answer directly for small inputs.
 f(n) is time for dividing P & combining the solutions of subproblems.
The complexities of many divide and conquer algorithms is given by the recurrence of the
form:

T(1) n=1
T(n) =
aT(n/b)+f(n) n>1

HEMANTH T D, Dept. of ISE, Acharya Institute of Technology. Page 3


Where a & b are known constants. We assume that T(1) is known and n is a power
of b i.e n=bk.
The method used for solving recurrence relation is called Substitution method.
This method makes substitution for each occurrence of the function T in the RHS until such
occurrences disappears.
Master Theorem:
The efficiency analysis of divide & conquer algorithm is simplified by the Master form:

If f(n)∈Θ(nd) where d≥0 in recurrence equation then,

(Analogous results hold for O & Ω notations also).

Binary Search:-
Let ai (1≤i≤n) is a list of elements stored in non decreasing order. The searching
problem is to determine whether a given element is present in the list. If key ‘K’ is present
we have to determine the index value j such that a j=K. If K is not in the list j is set to be zero.
Let P= (n, al, . . . ah, K) denotes an instance of binary search problem. Divide &
Conquer can be used to solve this binary search problem. Let Small (P) be true if n=1. S(P)
will take the value i if K=ai , otherwise it will take 0.
If P has more than one element it can be divided into new sub-problems as follows:
Take an index q with in the range [l, h] & compare K with a q. There are three possibilities.
i. If K= aq the problem is immediately solved(Successful search)
ii. If K< aq , key x is to be searched only in sublist a l, al+1, . . ., aq-1.
iii. If K >aq , key x is to be searched only in sublist a q+1, aq+2, . . ., ah.
If q is chosen such that aq is the middle element i.e q=(n+1)/2, then the resulting
searching algorithm is known as Binary Search algorithm.

HEMANTH T D, Dept. of ISE, Acharya Institute of Technology. Page 4


Recursive Binary search Algorithm:
Algorithm BinarySearch(a, low, high, mid)
// Implements recursive Binary search algorithm
// Input: An array a[low:mid] sorted in ascending order and a search key ‘K’
// Output: If ‘K’ is present return j such that K=a[j]; else return 0.
{
if( low=high) then
{
if(K=a[low]) then return low;
else return 0;
}
else
{

mid= ⌊(low+high)/2⌋;
if(K=a[mid]) then return mid;
else if (K<a[mid]) then
BinarySearch(a, low, mid-1, K);
else
BinarySearch(a,mid+1, high, K);
}
}

Non-recursive Binary search Algorithm:


Algorithm BinarySearch(a, n, x)
// Implements non-recursive Binary search algorithm
// Input: An array a[1....n] sorted in ascending order and a search key x
// Output: If x is present return j such that x=a[j]; else return 0.
{

HEMANTH T D, Dept. of ISE, Acharya Institute of Technology. Page 5


low=1; high=n;

while(low≤high) do
{

mid=⌊(low+high)/2⌋;
if(x < a[mid]) then high=mid-1;
else if(x > a[mid]) then low=mid+1;
else return mid;
}
return 0;
}

Theorem: Algorithm BinarySearch(a, n, x) works correctly.


Proof:-
Assume all the statements will work as expected & comparisons operations are
carried out appropriately. Initially low=1, high=n, n≥0 & a[1]≤a[2] ≤. . . ≤a[n]. If n=0, the
algorithm does not enter the loop & 0 is returned. Otherwise through the loop the possible
elements to be checked for equality with x are a[low], a[low+1], ... ,a[mid], ..., a[high]. If
x=a[mid] the algorithm terminates successfully. Otherwise the range is narrowed by either
increasing low to mid+1 or decreasing high to mid-1. This narrowing of range does not
affect the outcome of the search. If low becomes greater than high, then key x is not present
& the loop is exited.

Testing:-
To fully test binary search, we need not concern with the values of a[1:n]. By varying
x sufficiently, we can observe all possible computation sequences of BinarySearch without
taking different values for a.

HEMANTH T D, Dept. of ISE, Acharya Institute of Technology. Page 6


To test all successful searches, x must take on the n values in a. To test all
unsuccessful searches, x need only take on n+ 1 different value. So complexity of testing
BinarySearch is 2n+1 for each n.

Analysis:-
We do the analysis with frequency count(Key operation count) and space required
for the algorithm. In binary search the space is required to store n elements of the array
and to store the variables low, high, mid and x i.e. n+4 locations.
To find the time complexity of the algorithm, as the comparison count depends on
the specifics of the input, so we need to analyze best case, average case & worst case
efficiencies separately.
We assume only one comparison is needed to determine which of the three
possibilities of if condition in the algorithm holds. Let us take an array with 14 elements.
Index pos 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Elements -10 -6 -3 -1 0 2 4 9 12 15 18 19 22 30
comparisons 3 4 2 4 3 4 1 4 3 4 2 4 3 4

From the above table we can conclude that, no element requires more the 4 comparisons.
The average number of comparisons required is (sum of count of comparisons/number of
elements) i.e 45/14=3.21 comparisons per successful search on average.
There are 15 possible ways that an unsuccessful search may terminate depending
on the value of x. if x < a[1], the algorithm requires three comparisons to determine x is not
present. For all the remaining possibilities the algorithm requires 4 element comparisons.
Thus the average number of comparisons for an unsuccessful search is
(3+14*4)/15=59/15=3.93.
To derive the generalized formula and for better understanding of algorithm is to
consider the sequence of values for mid that are produced by BinarySearch for all possible
values of x. these possible values can described using binary decision tree in which the
value in each node is the value of mid. The below figure is the decision tree for n=14.

HEMANTH T D, Dept. of ISE, Acharya Institute of Technology. Page 7


7

11
3

5 9 13
1

6 8 10 12 14
4
2

In the above decision tree, each path through the tree represents a sequence of
comparisons in the binary search method. If x is present, then algorithm will end at one of
the circular node (internal nodes) that lists the index into the array where x was found. If x
is not present, the algorithm will terminate at one of the square nodes (external nodes).

Theorem:
If n is in the range [2k-1, 2k), then BinarySearch makes at most k element comparisons for
successful search and either k-1 or k comparisons for an unsuccessful search. (i.e. The time
for a successful search is O(logn) and for unsuccessful search is Θ(logn)).
Proof:
Let us consider a binary decision tree, which describes the function of Binary search
on n elements. Here all successful search end at a circular node, where as all unsuccessful
search will end at square node. If 2k-1≤n<2k, then all circular nodes are at levels 1,2,…,k,
where as all square nodes are at levels k & K+1(root node is at level 1). The number of
element comparisons needed to terminate at a circular node on level i is i, where as the
number of element comparisons needed to terminate at a square node at level i is only i-1,
hence the proof.
The above proof will tell the worst case time complexity is O(logn) for successful
search & Θ(logn) for unsuccessful search.

HEMANTH T D, Dept. of ISE, Acharya Institute of Technology. Page 8


Assume n=2k, then
C(n) =T(n/2)+1 n=2k
=T(2k-1)+1
= (T(2k-2)+1)+1
=T(2k-2 )+2
=(T(2k-3)+1)+2
=T(2k-3)+3

….
= T(2k-k)+k
=T(1)+k n=2k
C(n) =1+k=k logn=klog2

T(n) =logn logn=k

To determine the average case efficiency, we have to equate, the size of the decision
tree to the number of element comparisons in the algorithm.
The distance of a node from the root is one less than its level. Let the internal path
length I is the sum of the distances of all internal nodes from the root. Similarly, the
external path length E is the sum of the distances of all external nodes from the root.
For any binary tree with n internal nodes, E & I are related by the formula
E=I+2n
Let As(n) be the average number of comparisons for successful search, and A u(n) be
the average number of comparisons for unsuccessful search. The number of comparisons
needed to find an element represented by an internal node is one more than the distance of
this node from the root. Therefore
As(n)=1+I/n
The number of comparisons on any path from root to external node is distance
between the root and the external node. Since binary tree has n+1 external nodes
Au(n)=E/(n+1)
As(n)=(1+1/n) Au(n)-1
From the above formula we can conclude that, As(n) & Au(n) are directly related the
minimum value of As(n) (& Au(n)) is achieved by binary decision tree which has minimum
external & internal path length. This is possible when all external nodes are in on adjacent

HEMANTH T D, Dept. of ISE, Acharya Institute of Technology. Page 9


levels. From the above theorem E is proportional to nlogn. Using this in the above formulas,
we can conclude that both As(n) & Au(n) are proportional to logn.
Thus we can conclude that in both worst case & average case the number of
comparisons for binary search is same with in a constant factor.
The best case analysis of binary search is possible when the key is present at the
middle of the array i.e the number of comparisons required is 1.
So the time complexity of binary search is:
Successful Searches Unsuccessful Searches
Best case Θ(1) Best case
Average case Average case Θ(logn)
Θ(logn)
Worst case Worst case

Quick Sort
In quick sort, the division of array into two sub arrays is made such that sorted
subarrays are need not to be merged later. This can be done by rearranging the elements of
a[1:n] such that, a[i]≤a[j] for all i between 1 and m and for all j between m+1 to n for some
m, such that 1≤m≤n. Thus the elements in a[1:m] and a[m+1:n] can be independently
sorted, no merge is required. The rearrangement of the elements is accomplished by
picking some element of a[ ], say P=a[1] and reordering the other elements so that all
elements appearing before P in a[1:n] are less than or equal to P and all the element
appearing after P are greater than or equal to P. This rearranging is called Partitioning.

i j
P All are ≤ P ≥P …….. ≤P All are ≥ P

j i
P All are ≤ P ≤P ≥P All are ≥ P

HEMANTH T D, Dept. of ISE, Acharya Institute of Technology. Page 10


ALGORITHM Quicksort(A[l..r])
//Sorts a subarray by quicksort
//Input: Subarray of array A[0..n − 1], defined by its left and right indices l and r
//Output: Subarray A[l..r] sorted in nondecreasing order
if l < r
s ←Partition(A[l..r]) //s is a split position
Quicksort(A[l..s − 1])
Quicksort(A[s + 1..r])

ALGORITHM Partition(A[l..r])
//Partitions a subarray by using the first element as a pivot
//Input: Subarray of array A[0..n − 1], defined by its left and right indices l and r (l<r)
//Output: Partition of A[l..r], with the split position returned as this function’s value
p←A[l]
i ←l; j ←r + 1
repeat
repeat i ←i + 1 until A[i]≥ p
repeat j ←j − 1 until A[j ]≤ p
swap(A[i], A[j ])
until i ≥ j
swap(A[i], A[j ]) //undo last swap when i ≥ j
swap(A[l], A[j ])
return j

Analysis:-
 Quick algorithm is a recursive algorithm.
 The size of the input is specified by the number of elements we need to sort.
 The key operation of the algorithm is comparison.
 The count of the comparison is not just depends only on the size of the input, it also
depends on the type of the input i.e. sorted array, unsorted array, partially sorted

HEMANTH T D, Dept. of ISE, Acharya Institute of Technology. Page 11


array etc. So we need to analyze best case, worst case & average case analysis
separately.
Best-case Analysis:
The best case of quick sort occurs, when the problem’s instance is divided into two
equal parts on each recursive call of the algorithm. So the recurrence relation will be
0 if n=1
T(n)=
T(n/2)+T(n/2)+n otherwise

0 if n=1
T(n)=
2T(n/2)+ n otherwise
By applying the Master’s theorem,
T(n)=aT(n/b)+f(n)
Therefore a=2, b=2, f(n)=n=n1=nd so, d=1.
So, according to Master theorem T(n) is given by
Θ(nd) if a<bd

T(n)= Θ(ndlogbn) if a=bd

Θ(nlogba) if a>bd
For the quick sort algorithm, a=bd holds good,

∴ T(n)= Θ(ndlogbn)= Θ(n1log2n)∈ Θ(nlogn)

T(n)∈Θ(nlogn)
Worst case Analysis:
The worst case of the quick sort occurs, when at each invocation of the quick
sort algorithm, the current array is partitioned into two sub arrays with one of them being
empty.
This situation occurs when the input list is arranged in either ascending or descending
order.

HEMANTH T D, Dept. of ISE, Acharya Institute of Technology. Page 12


Ex: 10 | 11 12 13
10 11 | 12 13
10 11 12 | 13
10 11 12 13
The recurrence relation for the above situation is :
0 if n=1
T(n)=
T(0)+ T(n-1) + n otherwise

To sort left sub array To sort right sub array Partitioning

∴ T(n)= T(0)+T(n-1)+n
=T(n-1)+n //T(0)=0
By the method of backward substitution
T(n) = T(n-1)+n
= T(n-2)+(n-1)+n
=T(n-3)+(n-2)+(n-1)+n
=T(n-4)+(n-3)+(n-2)+(n-1)+n
…..

=T(n-i)+(n-i-1)+(n-i-2)+ . . . . +(n-3)+(n-2)+(n-1)+n
.
….

=T(n-n)+(n-(n-1))+(n-(n-2))+ . . . . +(n-3)+(n-2)+(n-1)+n

=T(0)+1+2+3+4+. . . . +(n-3)+(n-2)+(n-1)+n //T(0)=0

=1+2+3+4+. . . . +(n-3)+(n-2)+(n-1)+n

T(n)=n(n+1)/2  n2

T(n)∈Θ(n2)

Average Case Analysis:-

The average case of the quick sort will appear for typical or random or real time
input. Where the given array may not be exactly portioned into two equal parts as in best
case or the sub-arrays are not skewed as in worst case.

HEMANTH T D, Dept. of ISE, Acharya Institute of Technology. Page 13


Here the pivot element is placed at an arbitrary position from 1 to n.
Let k be the position of the pivot element, as shown below
1, 2, 3, . . . . . . . . . . . . . . . .k-1 K K+1, k+2, . . . . . . . . . . . ,n

Then, T(n)= T(k-1) + T(n-k) + (n+1) For partitioning

Here the partitioning element may appear at any position from 1 to n.

So, the average time complexity is given by,

T(n)=1/n  [T (k  1)  T (n  k )  (n  1)]
k 1

Multiply by n

nT(n)=  [T (k  1)  T (n  k )  (n  1)]
k 1

n n

nT(n)=  [T (k  1)  T (n  k )] +  (n  1)
k 1 k 1
Eqn 1

 [T (k  1)  T (n  k )] =T(0)+T(n-1)+T(1)+T(n-2)+T(2)+T(n-3)+ . . . . +T(n-2)+
k 1

T(1)+T(n-1)+T(0)

 [T (k  1)  T (n  k )] =2[T(0)+T(1)+T(2)+. . . .+T(n-2)+T(n-1)]
k 1
Eqn 2

 (n  1) =(n+1)(n-1+1)= (n+1)n
k 1
Eqn 3

Substituting Eqn 2 & Eqn 3 in Eqn 1

nT(n)= 2[T(0)+T(1)+T(2)+. . . .+T(n-2)+T(n-1)] +(n+1)n Eqn 4

Replace n by (n-1) in Eqn 4

HEMANTH T D, Dept. of ISE, Acharya Institute of Technology. Page 14


(n-1)T(n-1)= 2[T(0)+T(1)+T(2)+. . . .+T(n-2)] + n(n-1) Eqn 5

Subtract Eqn 5 from Eqn 4

nT(n)-(n-1)T(n-1)= 2T(n-1)+n2+n-n2+n

nT(n)-(n-1)T(n-1)=2T(n-1)+2n

Divide the above equation by n(n+1)

nT (n) (n  1)T (n  1) 2n
= +
n(n  1) n(n  1) n(n  1)

T (n) T ( n  1) 2
= +
(n  1) n n 1

T ( n  2) 2 2
= + + // Replace n by (n-1)
n 1 n n 1

T ( n  3) 2 2 2
= + + +
n2 n 1 n n 1

T ( n  4) 2 2 2 2
= + + + +
n3 n  2 n 1 n n 1
….

T ( 0) 2 2 2 2 2 2
= + + + +....+ + +
1 2 3 4 n 1 n n 1

2 2 2 2 2 2
= 0+ + + +....+ + + // T(0)=0
2 3 4 n 1 n n 1

T ( n) 1 1 1 1 1 1
= 2( + + + . . . . + + + )
(n  1) 2 3 4 n 1 n n 1

n 1
1

n 1
T ( n) 1
=2 = 2  di
(n  1) i2 i i 2
i

T ( n)
= 2 log i 
n 1

(n  1) 2

T ( n)
= 2[log(n+1)-log2]  2 log( n  1)
(n  1)

HEMANTH T D, Dept. of ISE, Acharya Institute of Technology. Page 15


T ( n)
 2 log( n  1)
(n  1)

T(n)=(n+1)2log(n+1)  nlogn

T(n)   (nlogn)

HEMANTH T D, Dept. of ISE, Acharya Institute of Technology. Page 16

You might also like