Divide and Conquer Notes
Divide and Conquer Notes
DIVIDE-AND-CONQUER
➢ General Method
➢ Binary search and its complexity
➢ Finding the Maximum and Minimum and its
Complexity
➢ Merge sort and its complexity
➢ Quick sort and its complexity
➢ Selection
➢ Strassen’s matrix multiplication
➢ Convex Hull problem
DIVIDE-AND-CONQUER
GENERAL METHOD
• These sub problems must be solved, and then a method must be found to combine
sub solutions into a solution of the original problem if necessary.
• If sub problems are still relatively large, then the divide-and-conquer strategy can
possibly reapplied to sub problems.
II. Conquer: solve the individual sub problems, if it is small problems otherwise apply
divide process
III. Combine: combine the solutions of sub problems into a solution to original problem (if
necessary)
Algorithm:
Algorithm DAndC(P)
{
if small(P) then return s(P);
else
{
divide P into smaller instaances P1,P2,…..Pk.;
apply DAC to each of these sub problems;
return combine(DAndC(P1),DAndC(P2)…..DAndC(Pk));
}
}
DIVIDE-AND-CONQUER
STEP 1:
Given problem size is n and if n is small then directly we compute the solution without applying
DAC. So that the time required isT(1).
STEP 2:
If the size of ‘p’ is ‘n’ and if it is not smaller then divide the problem into k sub problems
n1,n2…..nk.Each of size is 1/b where b is a constant.
g(n) if n is small
T(n) =
T(n1/b)+T(n2/b)+……….+T(nk/b)+f(n); otherwise
T(1) if n=1
T(n ) =
aT(n/b) + f(n) if n>1
DIVIDE-AND-CONQUER
Substitution method:
T(n) = 2 T(n/2)+n;
By substitution method,
T(n) = 2 T(n/2)+n
T(n) = 2{ 2 T(n/4) +(n/2)}+n = 4 T(n/4) +n+n = 22 T( n/22)+ 2n
= 2k T( n/ 2k ) + kn
= n T(1) + nlog2n
= 2 n+ nlog2n
DIVIDE-AND-CONQUER
BINARY SEARCH
• Let a[i], 1<=i<=n be a list of elements that are sorted in non-descending order.
• If n=1,small(p) be true then there is no requirement of applying divide and conquer,if n>1
then it can be divided into new sub problems.
• Pick an index 'q' = (i+l)/2 in the range[i,l] and compare x with a[q] then the following 3
possibilities will occur,
III. If x>a[q], then x is searched in right sub list a[q+1], a[q+2], …., a[l]
Algorithm binsearch( a, n, x )
{
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;
}
DIVIDE-AND-CONQUER
Algorithm Recbinsearch( a, i, l, x )
{
if ( i = l ) then //small(p)
{
if ( a[i] = x ) then return i;
else return 0;
}
else
{
mid = ( i+l )/2;
if (x < a[mid] ) then return Recbinsearch ( a, i, mid-1, x );
else if ( x> a[mid] ) then return Recbinsearch ( a, mid+1, l, x );
else return mid;
}
}
EXAMPLE:
Consider the list n=14 where a={-15, -6, 0, 7, 9, 23, 54, 82, 101, 112, 125, 131, 142, 151}
index 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 14 (1+14)/2 = 7
8 14 (8+14)/2 = 11
12 14 (12+14)/2 = 13
14 14 (14+14)/2 = 14 found
DIVIDE-AND-CONQUER
1 14 (1+14)/2 = 7
1 6 (1+6)/2 = 3
1 2 (1+2)/2 = 1
2 2 (2+2)/2 = 2
2 1 not found
1 14 (1+14)/2 = 7
1 6 (1+6)/2 = 3
4 6 (4+6)/2 = 5 found
return 14
DIVIDE-AND-CONQUER
return 0
Call Recbinsearch( a, 1, 6, 9)
Call Recbinsearch( a, 4, 6, 9)
Call Recbinsearch( a, 5, 5, 9)
return 5
DIVIDE-AND-CONQUER
Analysis
The basic operation of the binary search algorithm is element comparison. So, for
calculate time complexity it is enough to calculate number of element comparisons
require for binary search algorithm.
Time Complexity for binary search is
1 if n = 1
T(n) =
1 + T(n/2) if n> 1
Worst Case:
DIVIDE-AND-CONQUER
The problem is to find the maximum and minimum elements in an array of n elements.
The straight forward algorithm is
The basic operation of the algorithm is element comparison. So, we concentrate on the
number of element comparisons required this algorithm.
The algorithm requires 2(n-1) element comparisons in best, worst and average cases.
Best case:
Best case occurs when the elements are in increasing order. The number of element
comparisons required is n-1.
DIVIDE-AND-CONQUER
Worst case:
Worst case occurs when the elements are in decreasing order. The number of element
comparisons required is 2(n-1).
Average case:
On the average, a[i] is greater than max half the time,
The average number of comparisons is n/2 + 2(n-1)/2 = n/2 + n-1 = 3n/2 – 1
DIVIDE-AND-CONQUER
a[i] 22 13 -5 -8 15 60 17 31 37
1 , 9, 60, -8
6, 9, 60, 17
1, 5, 22, -8
1, 2, 22, 3, 3, -5, -5
13
0 if n = 1
T(n) = 1 if n = 2
DIVIDE-AND-CONQUER
T(n) = 2T(n/2) + 2
= 2[ 2T(n/22) + 2] + 2 = 22T(n/22) + 22 + 2
= 2[22T(n/23) +2] + 22 + 2 = 23T(n/23) + 23 + 22 + 2
K-1 times
= 2k-1T(n/2k-1)+ [ 2k-1 + 2k-2 + ……+ 23 + 22 + 2]
= (n/2)T(2) + 2(2k-1 – 1)/(2-1)
= n/2 + 2k – 2 = n/2 + n -2
= (3n/2) -2
DIVIDE-AND-CONQUER
MERGE SORT
The elements are to be sorted in non-decreasing order. Given a sequence of n elements. The
general idea is to imagine them split into two equal size sets. Each set is individually sorted and
the resulting sorted sequences are merged to produce a single sorted sequence of n elements.
The divide-and-conquer strategy in which the list is splitting into two equal subsets and sorts
them and combines operation is the merging of two sorted sub lists into a single sorted list.
EXAMPLE:
DIVIDE-AND-CONQUER
Algorithm merge(low,mid,high)
{
i:=low;
j:=mid+1;
k:= low;
while((i<=mid) and (j<=high)) do
{
if(a[i]<=a[j]) then
{
b[k]:=a[i];
i:=i+1;
}
else
{
b[k]:=a[j];
j:=j+1;
}
k:= k+1;
}
if (i>mid) then
for h = j to high do
{
b[k]:= a[h];
k:= k+1;
}
else
for h = i to mid do
{
DIVIDE-AND-CONQUER
b[k]:= a[h];
k:=k+1;
}
for h = low to mid do
a[h]:=b[h];
}
1, 10
1, 5 6, 10
1, 3 4, 5 6, 8 9, 10
1, 2 3, 3 4, 4 5, 5 6, 7 8, 8 9, 9 10, 10
1, 1 2, 2 6, 6 7, 7
1, 2, 3 4, 4, 5 6, 7, 8 9, 9, 10
6, 8, 10
1, 3, 5
1, 5, 10
DIVIDE-AND-CONQUER
ANALYSIS:
The basic operation of the Merge sort algorithm is element comparison.
Let T(n) = the number of element comparisons required for Merge sort algorithm for n elements.
= 2[ 2T(n/22) + (n/2) -1] + n-1 = 22T(n/22) + n-2 + n-1 = 22T(n/22) + 2n-(2 +1)
=22[2T(n/23) + (n/22)-1]+2n –(2+1) =23T(n/23) + n-22+2n –(2+1) =23T(n/23) + 3n-(22 +2+1)
K times
= 2kT(n/2k) + kn - ( 1+2+…..+2k-1)
= nT(1) + nlogn – 2k-1 + 1
= nlogn – (n/2) +1 = ( nlogn)
DIVIDE-AND-CONQUER
QUICKSORT
1. Quicksort technique based on divide and conquer strategy,in this technique at every step
each element is placed in the proper position.
2. It performs very well on longer lists,it works recursively by first selecting random pivot
element from the array then it partitions the list into left sub list and right sub list that are
less than the pivot element and greater than pivot element respectively.
3. The problem of sorting a given list is reduced to the problem of sorting two sublists and
process continuous until the list is sorted.
RULES:
DIVIDE-AND-CONQUER
i j
1 2 3 4 5 6 7 8 9
65 75 80 85 60 55 50 45 +∞
i j
1 2 3 4 5 6 7 8 9
65 45 80 85 60 55 50 75 +∞
i j
1 2 3 4 5 6 7 8 9
65 45 50 85 60 55 80 75 +∞
j i
1 2 3 4 5 6 7 8 9
65 45 50 55 60 85 80 75 +∞
j i
1 2 3 4 5 6 7 8 9
65 45 50 55 60 85 80 75 +∞
1 2 3 4 5 6 7 8
60 45 50 55 65 85 80 75
DIVIDE-AND-CONQUER
Initially
i j
1 2 3 4 5
55 45 50 60 ∞
j i
1 2 3 4 5
55 45 50 60 ∞
Initially
i j
1 2 3 3 4
50 45 ∞ 55 60
j i
1 2 3
50 45 ∞
1 2
45 50
Initially
i j
6 7 8 9
85 80 75 ∞
j i
6 7 8 9
85 80 75 ∞
Initially
i j
DIVIDE-AND-CONQUER
6 7 8 8
75 80 ∞ 85
J i
6 7 8
75 80 ∞
6 7
75 80
1 2 3 4 5 6 7 8
45 50 55 60 65 75 80 85
QUICKSORT ALGORITHM:
Algorithm Quicksort(p, q)
{
if(p<q)then
{
j:=partition(a, p, q+1);
Quicksort(p, j-1);
Quicksort(j+1, q);
}
}
Algorithm partition(a, m, p)
DIVIDE-AND-CONQUER
{
pivot:= a[m];
i:=m;
j:=p;
repeat
{
repeat
{
i:=i+1;
} until (a[i] >= pivot);
repeat
{
j:=j-1;
} until (a[j] <= pivot);
if (i<j)thenswap(a[i],a[j]);
} until (i>= j);
swap (a[j], pivot);
return j;
}
Analysis:
The basic operation of the algorithm is element comparison.
Let C(n) = The number of element comparisons required for n elements in quick sort algorithm.
Best case:
In this case the pivot element is fixed in the middle of the list in each partition. If the list contain
same elements (2, 2, 2, 2,….,2) then quick sort algorithm gives the best case.
Cbest(n) =2Cbest(n/2) + (n + 1)
= 2[2Cbest(n/22) + (n/2 + 1)] + (n+1) = 2 2Cbest(n/22) + 2n +(1+2)
= 23Cbest(n/23) + 3n +(1+2+22)
K times
= 2kCbest(n/2k) + kn +(1+2+…..+2 k-1) = nCbest(1) + nlogn + (2k-1)
SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR
ENGINEERING COLLEGE Page 22
DESIGN AND ANALYSIS OF ALGORITHM
DIVIDE-AND-CONQUER
=nlogn + n – 1= (nlogn)
Worst case:
In this case the pivot element is fixed in the extreme position (first or last) of the list in each
partition. If the list contain elements are in sorting order then quick sort algorithm gives the
worst case.
Cworst(n) = Cworst(n-1) + (n + 1)
= Cworst(n-2) + n + (n+1)
= Cworst(n-3) + (n-1) + n + (n+1)
= Cworst(1) + 3 + 4 + ….+n+(n+1) = 3+4+….+(n+1)
=( (n+1)(n+2)/2)-3 = O(n2)
Average case:
1 *n, we obtain,
n * CA(n) = n(n+1) + [ CA(0) + CA(n-1) + CA(1) + CA(n-2) +…..+CA(n-1) + CA(0) ]
= n(n+1) + 2[ CA(0) + CA(1) + CA(2) +…..+CA(n-1) ] ------ 2
Replace n by n-1 in , we
2 obtain
3
SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR
ENGINEERING COLLEGE Page 23
DESIGN AND ANALYSIS OF ALGORITHM
DIVIDE-AND-CONQUER
SELECTION
The partition algorithm can be used to obtain an efficient algorithm for the selection problem.
The problem is, we are given n elements a[1], a[2], ….,a[n] and are required to determine the
k-th smallest element.
If k > j then k-th smallest element is (k-j)-th smallest element in a[j+1], a[j+2], …..,a[n].
DIVIDE-AND-CONQUER
EXAMPLE:
65 70 75 80 85 60 55 50 45
i j
1 2 3 4 5 6 7 8 9 10
65 70 75 80 85 60 55 50 45 ∞
i j
1 2 3 4 5 6 7 8 9 10
65 45 75 80 85 60 55 50 70 ∞
i j
1 2 3 4 5 6 7 8 9 10
65 45 50 80 85 60 55 75 70 ∞
i j
1 2 3 4 5 6 7 8 9 10
65 45 50 55 85 60 80 75 70 ∞
i j
1 2 3 4 5 6 7 8 9 10
65 45 50 55 60 85 80 75 70 ∞
j i
1 2 3 4 5 6 7 8 9 10
65 45 50 55 60 85 80 75 70 ∞
1 2 3 4 5 6 7 8 9
60 45 50 55 65 85 80 75 70
So j=5 and k=7 , since k > 7, we find the k= (7-5) = 2nd smallest element in the right sub array
DIVIDE-AND-CONQUER
j i
1 2 3 4 5
85 80 75 70 ∞
j i
1 2 3 4 5
70 80 75 85 ∞
1 2 3
4
70 80 75 85
So j=4 and k=2, since k < j, we find the 2 nd smallest element in the left sub array
j i
1 2 3 4
70 80 75 ∞
1 2 3
80 75
70
So j=1 and k = 2, since k > j, we find the k = (k-j) = 1st smallest element in the right sub array
i j
1 2 3
SRINIVAS ASSOCIATE PROFESSOR INFORMATION TECHNOLOGY SRKR
ENGINEERING COLLEGE Page 26
DESIGN AND ANALYSIS OF ALGORITHM
DIVIDE-AND-CONQUER
80 75 ∞
j i
1 2 3
80 75 ∞
1 2
75 80
So j = 2 and k = 1, k < j, we find the 1 st smallest element in the left sub array
Algorithm
DIVIDE-AND-CONQUER
STRASSEN’S MULTIPLICATION
Let A and B be two nXn matrices. The product matrix C = A * B is also nXn matrix whose
elements C[i, j] = ∑𝑛𝑘=1 𝐴 [𝑖, 𝑘] ∗ 𝐵[𝑘, 𝑗]
The time complexity for the resulting matrix is (n3)
The divide and conquer strategy suggest another way to compute the product of two nXn
matrices. For simplicity we assume that n = 2 k. if n ≠ 2k, then enough rows and columns of zeros
can be added to both A and B.
Imagine that A and B are partitioned into four square sub matrices, each sub matrix is
n/2Xn/2.
DIVIDE-AND-CONQUER
* =
DIVIDE-AND-CONQUER
K times
= 8kT(n/2k) + cn2 [1+8/4+(8/4)2+….+(8/4)k-1] = 8kT(1) + cn2(2k-1)
= 8log2n + cn2(n-1) = n log28 +cn3 – cn2
= n 3 +cn3 – cn2 = (n3)
Hence no improvement over divide-and-conquer strategy. Since we use 8 matrix multiplications
and 4 matrix additions.
Volker strassen has discovered a way to compute the formulas using only 7 matrix
multiplications and 18 additions or subtractions for computing C 11, C12, C21, and C22.
P = (A11+A22)(B11+B22 )
Q = (A21+A22) B11
R = A11(B12 – B22)
S = A22(B21 – B11)
T = (A11 + A12)B22
U = (A21 – A11) (B11 + B12)
V = (A12 – A22) (B21+ B22)
C11 = P + S – T + V
C12 = R + T
C21 = Q + S
C22 = P + R – Q + U
DIVIDE-AND-CONQUER
K times
= 7kT(n/2k) + cn2 [1+7/4+(7/4)2+….+(7/4)k-1] ≤7kT(1) + cn2(7/4)k
= n log27 +cn2 * nlog27/4
= n log27 +cn log27 = (n log27) = (n 2.81)
CONVEX HULL
Let S be a set of points in the plane.
Convex:
A polygon is defined to be convex if for any two points P1 and P2 inside the polygon, the direct
line segment from P1 to P2 is fully contained in the polygon.
Convex Hull:
The convex hull of a set S of points in the plane is defined to be the smallest convex polygon
containing all the points of S.
DIVIDE-AND-CONQUER
First identify the two points (call P1 and P2) of X with the smallest and largest x-coordinate
values, both P1 and P2 are extreme points and part of the convex hull.
The set X is divided into X1 and X2 so that X1 has all points to the left of the line
segment <P1, P2> and X2 has all the points to the right of the line segment <P1, P2>. Both X1
and X2 include the two points P1 and P2. Then the convex hull of the X1 and X2 (called Upper
hull and Lower hull) are computed using divide-and-conquer algorithm (Quick hull algorithm).
Identify a point P3 in X1 with the largest area of the triangle. The algorithm identifies all the
points of X1 that are to the left of the line <P1, P3>, along with P1 and P3 will make up the set
X11. The points of X1 that are to the left of the line <P3, P2> along with P2 and P3 will make up
the set X12.
2. The points inside the triangle P1P2P3 can be eliminate from the further consideration.
DIVIDE-AND-CONQUER
The algorithm recursively continues for construction of upper hull on X11 and X12. Concatenate
all the points P1, P2, P3, …. And so on, we get the upper hull of X1