Unit 3 Divide and Conquer
Unit 3 Divide and Conquer
The general logic (Control abstraction) code for the divide and conquer strategy can
be expressed as
Algorithm DC(P)
{
if P is too small then
return solution of P;
else
{
Divide P into smaller problems P1, P2, .. Pk k>=1
Apply DC to each of the sub problems
Return combine( DC(P1), DC(P2), DC(P3)…..DC(Pk));
}
}
Let T(n) be the time required to find the solution for the given problem. Let g(n) be
the time required to solve the problem if n is small.
Let f(n) be the time required for dividing the problem P into smaller sub problems.
Then the computing time can be expressed as recursive relation.
The complexity of many divide and conquer algorithms is given by recurrence relation.
Where a and b are known constants. We assume that T(1) is known and n is a power
of b i.e n=bk.
One of the methods for solving any such recurrence relation is called the substitution
method. This method repeatedly makes substitutions for each occurrence of the
function T in the right hand side until all such occurrences disappear.
Dr.DSK DAA UNIT-III Divide & Conquer Page 1
Consider the case in which a=2 and b=2. Let T(1)=2 and f(n)=n. We have
T(n) = 2T(n/2)+n
T(n) = 2[2T(n/4)+n/2]+n
T(n) = 4T(n/4)+2n
T(n) = 4[2T(n/8)+n/4]+2n
T(n) = 8T(n/8)+3n
:
:
In general we see that T(n) = 2iT(n/2i)+in for any log2n >=i>=1. In particular, then
T(n)=2lognT(n/2 logn)+nlogn corresponding to the choice of i = log n. Thus
T(n)=nT(1)+nlog2n+2n
Example 1) Solve the following recurrence relation
T(n) = 2 if n=1
2T(n/2)+n if n>1
n
T (n) = 2.T + n (1)
2
n n
T (n) = 2. 2T + + n Putting n = n / 2 inT (n)
4 2
n
T (n) = 4T + 2n
4
n n
T (n) = 4 2T + + 2n
8 4
n
T (n) = 8T + 3n
8
:
:
k n
: T ( n) = 2 T k + kn
2
T (n) = 2n.T (1) + n log 2 n ( Put n = 2k ⇒ k = log 2 n)
T (n) = n log 2 n + 2n (∵ T (1) = 2)
n
T (n) = 2T + 2
2
n
T (n) = 2 2T + 2 + 2
4
n
T ( n) = 2 2 T + 2 2 + 2
4
n
T (n) = 22 2T + 8 + 22 + 2
8
n
T ( n ) = 2 3 T 3 + 23 + 2 2 + 2
2
:
:
:
Applications :
Binary Search,
Finding Minimum and Maximum,
Merge Sort,
Quick Sort,
Strassen’s Matrix multiplication
Quick Sort
The technique involved in this quick sort is :
1) Consider the first element of the array as the pivot element.
2) Place the pivot element in the array such that all elements before the pivot element
have a value less than the pivot element and all the elements after the pivot element
have a value greater than the pivot element.
3) At this stage we will be obtaining two different arrays with the pivot element
positioned in between. Apply the same technique to these arrays and repeat the
process until the final solution is obtained.
The quick sort scheme is developed by C.A.R.Hoare, has the best average behavior among all
the sorting methods.
Let ‘A’ be an array and ‘n’ the number of elements in the array to be sorted. Choose an
element ‘x’ from a specific position within the array i.e., x = a[1]
Suppose the elements of array ‘a’ are partitioned, such that ‘x’ is placed into position ‘j’ and
the following conditions hold.
1. Each of the element in positions ‘1’ through ‘j-1’ is less than or equal to ‘x’.
2. Each of the elements in positions ‘j+1’ through ‘n’ is greater than or equal to ‘x’.
Notice that if these two conditions hold for a particular ‘x’ and ‘j’, ‘x’ is the jth smallest
element of array ‘a’, so that ‘x’ remains in position ‘j’ when the array is completely sorted. If
the foregoing process is repeated with the sub arrays a[1] through a[j-1] and a[j+1] through
a[n] and any sub arrays created by the process in successive iterations, the final result is a
sorted array.
At this point 25 is in its proper position in the array a[2], each element below that
position is less than or equal to 25 and each element above that position is greater than or
equal to 25 Since 25 is in its final position the original problem is decomposed into the
problem of sorting two arrays.
(12) and (57 48 37 92 86 33)
The array of one element is already sorted. To sort the second sub array the process is
repeated and the sub array is further sub divided. The entire array is now be viewed as
12 25 ( 57 48 37 92 86 33)
Where parentheses enclose the sub arrays that are yet to be sorted.
12 25 ( 48 37 33 ) 57 ( 92 86 )
and further representations are
12 25 ( 37 33 ) 48 57 ( 92 86 )
12 25 ( 33) 37 48 57 86 (92 )
12 25 33 37 48 57 86 92
Unsorted array
25 57 48 37 12 92 86 33
(12) 25 (57 48 37 92 86 33)
12 25 (48 37 33) 57 (92 86)
12 25 (37 33) 48 57 (92 86)
12 25 33 37 48 57 (92 86)
12 25 33 37 48 57 86 92 Sorted array
Note that the final array is sorted. By this time you should noticed that the quick sort may be
defined most conveniently as a recursive procedure.
And also assume that the proper position for pivot always points the exact middle element of
the sub array. In that case there will be approximately n comparisons. Actually n-1
comparisons.
The file of size n will be splitted into 2 subfiles each of size n/2.
The file of size n/2 will be splitted into 2 subfiles each of size n/4.
The file of size n/4 will be splitted into 2 subfiles each of size n/8.
The file of size n/8 will be splitted into 2 subfiles each of size n/16.
And so on until the subfile having size 1.
After having the subfile m times there are n files of size 1. Thus the total no of comparisions
for the entire sort is approximately
= n + 2*n/2 + 4 * n/4 + 8*n/8 + ……………………….n*n/n upto m terms
=n*m
= n*log n
= O(n log n)
In analyzing Quick sort, we count only the number of element comparisons C(n). It is easy to
see that the frequency count of other operations is of the same order as C(n). The number of
element comparisons in each call of partition is at most high-low+1. Let r be the total number
of elements in all the calls to partition at any level of recursion.
Cw(n) = 1+2+3+…..+n
Worst case time complexity O(n2)
Average case time complexity O(n log n)
Binary Search
Divide and Conquer Method can be used to solve this problem.
Example)
Position 1 2 3 4 5 6 7 8 9 10 11 12 13 14
value -15 -6 0 7 9 23 54 82 101 112 125 131 142 151
MERGE SORT
Worst Case time complexity O(n log(n))
Assume that the elements are to be sorted in non decreasing order.
Assume that the Array contains n elements a[1], a[2],……a[n]. Imagine them split in to two
sub arrays.
a[1],a[2] ….a[n/2]
a[n/2+1]……a[n]
Each set is individually sorted and the resulting sorted sequences are merged to produce a
single sorted sequence of n elements. Thus we have another ideal example of divide and
conquer strategy in which the splitting is into two equal sized sets and the combining
operation is the merging of two sorted sets into one.
Example
A[1:10] = (310, 285, 179, 652, 351, 423, 861, 254, 450, 520)
A[1:5]=( 310, 285, 179, 652, 351)
A[6:10]=( 423, 861, 254, 450, 520)
Now the merge begins. Note that no movement of data has yet taken place. A record of the
sub arrays is implicitly maintained by the recursive mechanism.
(310| 285| 179|, 652, 351| 423, 861, 254, 450, 520)
(285, 310| 179|, 652, 351 | 423, 861, 254, 450, 520)
(179, 285, 310| 652, 351 | 423, 861, 254, 450, 520)
(179, 285, 310| 351, 652 | 423, 861, 254, 450, 520)
(179, 285, 310, 351, 652 | 423, 861, 254, 450, 520)
(179, 285, 310, 351, 652 | 423| 861| 254| 450, 520)
(179, 285, 310, 351, 652 | 423, 861| 254| 450, 520)
(179, 285, 310, 351, 652 | 254, 423, 861| 450, 520)
(179, 285, 310, 351, 652 | 254, 423, 450, 520, 861)
Final
(179, 254, 285, 310, 351, 423, 450, 520, 652, 861)
If the time for merging operation is 4n, then the computing time for merge sort is described
by the recurrence relation
T(n) = a if n=1
2T(n/2)+cn if n>1
Where n is a power of 2 i.e n=2k. We can solve this equation by successive substitutions
Algorithm minmax(a,n,min,max)
{
min:=max:=a[1];
for i:= 2 to n do
{
if a[i] < min then min:=a[i];
if a[i] > max then max:=a[i];
}
}
In analyzing the time complexity of this algorithm we once again concentrate on the number
of element comparisons. In the conventional method there are 2(n-1) element comparisons in
the best average and worst cases. Time complexity is O(n)
This is a recursive algorithm that finds the minimum and maximum of the set of elements
(a[1],a[2],a[3]….a[n]}. The situation of set sizes one ( lowpos = highpos ) and two
(lowpos=highpos-1) are handled separately. For sets containing more than two elements the
midpoint is determined and two new sub problems are generated. When the min and max of
these two sub problems are determined, the two max are compared and the two min are
compared to achieve the solutions for the entire set.
The number of comparisons needed for minmax() will be T(n). The recurrence relation is
T(n)= T(n/2)+T(n/2)+2 n>2
1 n=2
0 n=1
n
T (n) = 2T + 2
2
n
T (n) = 2 2T + 2 + 2
4
n
T (n) = 4T + 4 + 2
4
:
:
T (n) = 2k −1T ( 2 ) + ∑ 2i
1≤i ≤ k −1
3n
T (n) = 2k −1 + 2k − 2 = −2
2
Example ) Suppose we simulate the minmax on the following nine elements of array a:
Position 1 2 3 4 5 6 7 8 9
Array Value 22 13 -5 -8 15 60 17 31 47
minmax(1,9,min,max)
minmax(1,5,min,max) minmax(6,9,min,max)
minmax(1,3,min,max) minmax(4,5,min,max)
minmax(1,2,min,max) minmax(3,3,min,max)
minmax(1,1,min,max) minmax(2,2,min,max)
minmax(4,4,min,max) minmax(5,5,min,max)
minmax(6,7,min,max) minmax(8,9,min,max)
minmax(6,6,min,max) minmax(7,7,min,max)
minmax(8,8,min,max) minmax(8,8,min,max)
minmax(9,9,min,max) minmax(9,9,min,max)
1)Write and explain the control abstraction algorithm of divide and conquer.
2)Design Divide and conquer algorithm for computing the number of levels on a binary tree.
3) Analyze the average case time complexity for quick sort.
4)Write and explain the divide and conquer merge sort algorithm. Also compute its time
complexity.
5) Suggest refinements to Merge Sort to make it in-place.
6) Explain the Strassen’s matrix multiplication concept with an example.
7) Write deletion algorithm of binary search tree.