0% found this document useful (0 votes)
43 views24 pages

2.divide and Conquer

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views24 pages

2.divide and Conquer

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Unit II – Divide and Conquer

UNIT II - Divide and Conquer


2.1 Introduction
A divide-and-conquer algorithm recursively breaks down a problem into two or more
sub-problems of the same or related type, until these become simple enough to be
solved directly. The solutions to the sub-problems are then combined to give a
solution to the original problem.

General Method:
1. An instance of the given problem is divided into smaller instances of same type and
equal size.
2. All the smaller instances of the problem are solved.
3. The solution of all the smaller instances are combined together to get a solution to
the original problem.
Problem of size N

subproblem A of size n/2 subproblem B of size n/2

subproblem 1 subproblem 2 subproblem 3 subproblem 4


of size n/4 of size n/4 of size n/4 of size n/4

solution 1 solution 2 solution 3 solution 4

solution to solution to
problem A problem B

Solution to original problem

2.2 Control Abstraction or general algorithm of DAC (Divide and Conquer)


Algorithm DAC(P)
//Purpose : Solve the problem of a given instance (P) by dividing into various smaller
instances such as P1,P2,P3,….Pn using divide and conquer

if small(P) // if the instance of a problem it too small return the solution


return G(P)

Design and analysis of algorithms - A Beginners Guide Page 1


Unit II – Divide and Conquer

else
Divide P into P1,P2,P3----- PK, k>=1; //divide P into several instances

S=DAC(P1) + DAC(P2) ------ DAC(PK) // solve P1 P2 – PK and combine the


// solutions

return S // return solution to the original


// problem
endif

Time complexity for Divide and Conquer method:


An instance of size n can be divided into several instances say a of size n/b and the
time complexity is obtained using general divide and conquer recurrence relation
T(n) = aT(n/b)+f(n)
Where a and b are positive constants such that b > 1 and f(n) is a function which is the
time spent on dividing the problem into smaller instances and combining them to get a
single solution.

The time for L Dand c/ on assize n isgiven by,

T(n)= g(n) if n is small


T(n1)+T(n2)+………+T(nk) otherwise

2.3 Sum of ‘N’ Natural Numbers


Algorithm to find the sum of ‘n’ natural numbers stored in a array.

ALGORITHM Sum(low,high,a)
1. if (low = high ) return a[low]
2. mid = (low+high)/2
3. return sum(low,mid,a)+sum(mid+1,high,a)

Tracing :
Sum(0,5,a)

Sum(0,2,a) Sum(3,5,a)

Sum(0,1,a) Sum(2,2,a) Sum(3,4,a) Sum(5,5,a)


a[2] a[5]

Sum(0,0,a) Sum(1,1,a) Sum(3,3,a) Sum(4,4,a)


a[0] a[1] a[3] a[4]

Analysis:
The recurrence relation for this algorithm

Design and analysis of algorithms - A Beginners Guide Page 2


Unit II – Divide and Conquer

T(n)= 0 if n=0
T(n/2) +T(n/2) +1 otherwise
Time required to add 2Nos

Time required to add the items in the right part of the array.
.
Time required to add the items in the left part of the array.

2.4 MaxMin Problem


Given a list of elements the problem is to find the maximum and minimum element in a given
set of elements.

Straightforward approach of MaxMin problem


In the straightforward approach we assume the first element is the minimum or maximum
element and the remaining elements are compared with the first and updating the maximum
or minimum if found.

Algorithm: Straight MAXMIN (a, n, i, max, min)


/* a is an array of n elements max is used to store the maximum element in the array and min
is used to store the minimum element in the array.*/
Step 1: Initially assume first element itself as max and min.
max = min=a[1];
Step 2: for i=2 to n do
if (a[i] > max)
max=a[i]
else if (a[i] < min)
min=a[i]
end for
Step 3: exit

Analysis:
Best case: The best case occurs when all the elements are in the increasing order. The
number of comparisons will be (n-1).
Worst case: The worst case occurs when all the elements are in the decreasing order. The
number of comparisons will be 2 (n-1).
Average case: The number of comparisons will be 3n/2 -1

Divide and Conquer approach of MaxMin problem


This approach requires the number of elements to be greater than 2.If MAX (P) and MIN (P)
are the maximum and minimum of the elements in P then
MAX (P) =the larger of MAX (P1) and MAX (P2)
MIN (P) = the larger of MIN (P1) and MIN (P2)
If P contains only one element then the solution can be computed without splitting.
The above procedure exists in 3 cases.
Case 1: if n=1
then max = min = a[1]
(If there is only one element in the array)

Design and analysis of algorithms - A Beginners Guide Page 3


Unit II – Divide and Conquer

Case 2: (if there are only 2 elements in the array)


If n=2 then problem can be solved with 1 comparison
Case 3: (if more than 2 elements are there in the array)
If n > 2 then the problem P is divided into two instances P1 and P2
After dividing into sub problems apply divide and conquer method recursively to get
the smallest sub problem.

Algorithm: DandCMAXMIN ( a, i, j, max, min)


/* a is an array of n elements where i indicates the index of the first element and j indicates
the index of the last element ,max is used to store the maximum element in the array and min
is used to store the minimum element in the array.*/

Step 1: if (i = = j)
max = min = a[i]
else
{
Step 2: if (i = =j-1)
{
Step 3: if(a[i] > a[j])
{
max = a [ i ]
min = a [ j ]
}
else
{
max=a [ j ]
min = a [ i ]
}

}
Step 4: else
{
mid=( i + j)/2
DandMaxMin (i , mid, max1, min1)
DandMaxMin (mid + 1, j, max2, min2)
if(max1 > max2)
max=max1
else
max=max2
if(min1 > min2)
min=min1
else
min=min2
}
}

Design and analysis of algorithms - A Beginners Guide Page 4


Unit II – Divide and Conquer

Tracing:
Consider an array with elements 23, 9, 8, 72, 66, 34wheren=6.
In the first call the array is broken into two halves. The list is again broken down as
long as the size of the array is 2 or 1.
23 9 8 72 66 34 i=1
j=6

23 9 8 i=1 72 66 34 i= 4
j=3 j=6

23 9 i=1 8 i=3 72 66 i=4 34 i=6


j=2 j=3 j=5 j=6

The maximum and minimum values are chosen from the smallest list and these values are
returned to the preceding step where the size is slightly big. This is continued until the entire
list is searched.

1, 6 max= 72
min= 8

1, 3 max=23 4,6 max= 72


min= 8 min= 34

1, 2 max=23 3,3 max= 8 4,5 max=72 6,6 max=34


min=9 min= 8 min=66 min=34

Analysis:

T(N)= 0 if n = 1
1 if n=2
T (n/2)+T (n/2) + 2 if n > 2

Let n = is the size of items in an array and T (n) = time required to apply the
algorithm on an array of size n. Here we divide the terms as T(n/2).

Addition with constant value 2 here tends to the comparison of the minimum with
minimum and maximum with maximum as in above example.

Design and analysis of algorithms - A Beginners Guide Page 5


Unit II – Divide and Conquer

T(n)=T(n/2)+T(n/2)+2

=2.T(n/2)+2 --->eq.(1)

Let n=2k

Now eq(1) can be written as

T(2k ) =2.T(2k /2)+2

T(2k )=2.T(2k -1)+2 ---->eq.(2)

Put k=k-1 in eq.(2)

T(2k-1 )=2.T(2k -2)+2 ----->eq.(3)

Similarly putting k=k-2 in eq.(2) we get

T(2k-2)=2.T(2k -3)+2 ----->eq.(4)

In e.(2) substitute the value for T(2k-1) from eq.(3) obtained above

T(2k )=2.T(2k-1)+2

=2.[2.T(2k-2)+2]+2

=4T(2k-2)+4+2

Substitute the value for T(2k-2) we get

=4.[2.T(2k -3)+2]+4+2

=8 T(2k -3)+8+4+2

T(2k )=23 T(2k -3)+23+22+……………+21

As T(2)=1

and so on…

Generalizing it with3=k-1

=2k-1T(2(k-(k-1))+[2k-1+2k-2+………+21]

T(2k )=2k-1T(2)[2k-1+2k-2+………+21]

As T(2)=1,

Design and analysis of algorithms - A Beginners Guide Page 6


Unit II – Divide and Conquer

T(2k )=2k-1 . 1 [2k-1+2k-2+………+21]

=2k-1 [21+22+23+……………+2k-2+2k-1]

=2k-1+21. (2k-1-1)
(2-1)
Using geometric progression, a(rn-1) where a=2,n=k-1 and r=2
r-1
=2k-1+21. (2k-1-1)
(2-1)
=2k-1+2k-1+1-2 =2k-1+2k-2
2-1 1
T(2k)= 2k +2k-2
2

Replace for 2k=n

T(n)=n +n-2
2
T(n)=3n - 2
2
Therefore the number of comparisons required applying the divide and conquer
algorithm on n element =3n - 2
2
2.5 Searching
Searching Algorithms are designed to check for an search item or retrieve an item from
any given set of elements.
2.5.1 Linear Search or Sequential search
The most straight forward method of finding a particular element in an unordered list
is linear search. This technique simply involves the comparison of each element of the
list in a sequential manner until the desired element is found.

Algorithm: Linear Search


A is an array with n elements and item is the element to be searched for.
Step 1:loc=-1
Step 2:i=0
Step 3: Repeat step 4 (while(i<n and loc= -1)
Step 4:if(item = = A[i])
loc=i;
else

Design and analysis of algorithms - A Beginners Guide Page 7


Unit II – Divide and Conquer

i=i;
Step 5:if(loc != -1) then
write ”item found at“ loc
else
write “item not found”
Step 6:exit

Tracing:
Case I: Element found in the list
Consider the elements 22 11 66 44 where n=4 and the item=66
i=0 ,LOC = -1
A[i]=22
22 != 66 incremnt i, and i=1
A[i]=11
11!= 66 incremnt i, and i=2
A[2] = =66
LOC=2
Therefore 66 is found in position 3 (LOC+1)
Case II: Element not found in the list
Consider the item 99 in the above list 22 11 66 44
i=0 ,LOC= -1
A[i]=22
22 != 99 increment i, and i=1
A[i]=11
11!= 99 increment i, and i=2
A[2] =66
66!=99 increment i, and i=3
A[3] =44
44!=99 increment i, and i=4

i is not less than n and LOC=-1 itself. Therefore display item not found in the list.

Analysis
Best case: It occurs if the item being searched is in the first location. Therefore Ω (1)
Worst case: It occurs if the item being searched is in the last position. Therefore O (n)
Average case:
Number of indexes=n
i.e)values are1,2,…n
Probability of finding an element at any position is same and it is 1/n
Number of comparisons = 1. 1 +2. 1 +3. 1 +………..+n.1
n n n n

= 1 . n(n+1) =(n+1) ≈ Ꝋ (n)


n 2 2

Therefore Average case = Ꝋ (n)

Design and analysis of algorithms - A Beginners Guide Page 8


Unit II – Divide and Conquer

2.5.2 Binary Search


An element can be found in an ordered list by using Binary search. In this the search element
is compared with the middle element in the list. If the search element is lesser than the middle
element the binary search is performed again only on the elements to the left of the middle
element. If the search element is greater than the middle element then the search is made to
the right of middle element. This process continues until the desired element is found or the
search list becomes empty.

Example:
Consider the elements in array A 11 22 33 44 where n=4 and search element ITEM is 11
low = 0(lower bound)
high = 3(upper bound)
mid = (0+3)/2 =1
ITEM<A[mid] so go to the lower half segment.
Therefore reset the value of high to mid-1
low=0 high=0
mid=0
ITEM= =A[0] and search element 11 is found at position 1 (mid+1).

Consider the search element 77 in the above list 11 22 33 44


low = 0(lower bound)
high = 3(upper bound)
mid = (0+3)/2 =1
A[mid]=22
ITEM>A[mid] so go to the upper half segment.
Therefore reset the value of low to mid+1
low=2 high=3
mid=2
A[mid]=33
ITEM>A[mid] so go to the upper half segment.
Therefore reset the value of low to mid+1
low=3 high=3
mid=3
a[mid]=44
ITEM>A[mid] so go to the upper half segment.
Therefore reset the value of low to mid+1
low=4
Now low>high and therefore conclude that the element is not found in the list.

Non recursive Binary search algorithm

ALGORITHM Binary Search(item,a,n)


//Search for an item in the list
//input item –the element to be searched
// a-the list of elements where the search takes place
// n-the no of elements in the list
//output position is returned if search is successful or -1 is returned

Design and analysis of algorithms - A Beginners Guide Page 9


Unit II – Divide and Conquer

Step 1. low←0
Step 2. high←n-1
Step 3. while(low<=high)
mid ←(low+ high)/2
if(item = =a[mid])
return mid+1
if(item < a[mid])
high←mid -1
else
low←mid+1
endif
endwhile
Step 4. return -1

Recursive Binary search algorithm

ALGORITHM Binary Search (item ,a, n)


//Search for an item in the list
//input item – the element to be searched
// a-the list of elements where the search takes place
// n-the no of elements in the list
//output position is returned if search is successful or -1 is returned
if (low==high)
{
if (key==a[low]) //if only one element is present
return low;
else
return(-1); //unsuccessful
}
else
{
mid=(low+high)/2; //reduce array into smaller arrays.
if(key==a[mid])
return mid;
else if(key < a[mid])
return binary search(a,low,mid-1,key)
else return binarysearch(a,mid+1,low,high)
}

Design and analysis of algorithms - A Beginners Guide Page 10


Unit II – Divide and Conquer

Analysis:
The recurrence relation of Binary search in worst case is

T(n)= a if n=1
T(n/2) + b otherwise

Time required to time required to


Search either the to compare
Upper or the lower the middle element
Part of the array

Best case:
This occurs when the search element is the mid element itself.Best case complexity is
Ω(1)
Worst case (successful and unsuccessful case):
T(n) = a if n=1
T(n/2)+b otherwise

Assume that the size of the array is always an integral power of 2.If n is the number of
elements in the array , let n = 2k. Now consider the recurrence relation:

T(n)=T(n/2)+b -----eq.(1)

Let n=2k

Now eq.(1) can be written as

T(2k ) =T(2k /2)+b

T(2k )=T(2k -1)+b ---->eq.(2)

Put k=k-1 in eq.(2)

T(2k-1 )=T(2k -2)+b ----->eq.(3)

Similarly putting k=k-2 in eq.(2) we get

T(2k-2)=T(2k -3)+b ----->eq.(4)

In e.(2) substitute the value for T(2k-1) from eq.(3) obtained above

T(2k )=T(2k-1)+b

=T(2k-2)+b]+b

=T(2k-2)+2b

Design and analysis of algorithms - A Beginners Guide Page 11


Unit II – Divide and Conquer

Substitute the value for T(2k-2) we get

=T(2k -3)+b]+=2b

=T(2k -3)+3b

T(2k )= T(2k -3)+3b

and so on…

Generalizing it with k=3

T(2k )=T(2k -k)+kb

=T(20)+kb
As n=2k , k=log2n
T(n) = a+log2n.b
Ignoring the constant T(n) =O(log2n)
Therefore time complexity of binary search in worst case is given by
T (n) = O (log2n)

Average case:
The search element is found somewhere in between the recursive calls but not at the
end.

c=n
T(n) = 1 ∑ c.2c-1
N c=1

c=k
T(n) = 1 ∑ c [ 2c - 2c-1 ]
n c=1

c=k
T(n) = 1 ∑ c .2c - c.2c-1
n c=1

c=k c=k
T(n) = 1 ∑ c .2c - ∑ c.2c-1
n c=1 c=1

c=k c=k-1

Design and analysis of algorithms - A Beginners Guide Page 12


Unit II – Divide and Conquer

T(n) = 1 ∑ c .2c - ∑ (c+1).2c+1 -1


n c=1 c=1

c=k c=k-1
T(n) = 1 ∑ c .2c - ∑ (c+1).2c
n c=1 c=1

c=k c=k-1 c=k-1


T(n) = 1 ∑ c .2c - ∑ c.2c - ∑ 2c
n c=1 c=0 c=0

As c=0 to k-1 only k term remains when the first two summations are equated

T(n) = 1 k .2k - [ 20+21+….2k-1]


n
Applying geometric progression

T(n) = 1 k.2k - [20(2k-1)]


n 2-1

= 1 k.2k - [1(2k-1)]
n 1

= 1 [ k.2k - 2k + 1]
n
= 1 [2k(k-1)+1]
n

= 1 [n(k-1)+1]
n

= n(k-1) + 1
n n

=(k-1)+ 1 = nk - n + 1
n n

As n=2k ,
k=log2n

T(n)=n log2n - n + 1

Therefore the average case Ꝋ (n) = n log2n

Design and analysis of algorithms - A Beginners Guide Page 13


Unit II – Divide and Conquer

2.6 SORTING
A Sorting mechanism is used to rearrange a given array or list elements according to a
comparison operator on the elements. The comparison operator is used to decide the
new order of element in the respective data structure - ascending or descending order.

2.6.1 Merge Sort


This sort uses the concept of merging to sort records.
(Merging is the process of combining two or more sorted files into a third sorted file)
It uses the divide and conquer technique where the given set of inputs are split into distinct
subsets and the required operation is applied to each subset separately and then the results
obtained in each subset is combined to get the result of the problem.. A list of ‘n’ elements is
divided into two subsets and each subset is sorted separately. The resultant lists are then
combined to give the sorted list. The lists are divided into smaller subsets till each subset
could be solved separately without splitting. Then they are combined to get the final sorted
list.
ALGORITHM Mergesort A, low, high)
//Sort using merge sort
//Input a: unsorted array with low and high as lower bound and upper bound
//output sorted array
Step 1: if (low< high)
Step 2: mid = (low +high)/2
Step 3: call mergesort (a,low,mid);
Step 4: call mergesort (a,mid+1,high);
Step 5 :call simplemerge (a, low, mid, high);
Step 6 : endif
Step 7:Exit

ALGORITHM Simplemerge(A,low,mid,high)
// Merge two sorted arrays
//input A is a sorted arrary from the index position low to mid
// A is a sorted array from index position mid+1 to high
//output A is a sorted from index low to high

Step 1: Set i=low


j=mid+1
k=low
Step 2: while (i<=mid and j<=high)
Step 3: if a[i] < a[j]
c[k]= a[i]
i=i+1
k=k+1
else
c[k]=a[j]
j=j+1
k=k+1
endif

Design and analysis of algorithms - A Beginners Guide Page 14


Unit II – Divide and Conquer

end while
Step 4: while(i<=mid)
c[k]=a[i]
k=k+1
i=i+1
endwhile
Step 5: while(j<=high)
c[k]=a[j]
k=k+1
j=j+1
endwhile
Step 6: for i=low to high
a[i]=c[i]
endfor
Step 7: return

Analysis – Merge sort


The recurrence relation for merge sort is

T(n)= a if n=1
T(n/2) +T(n/2) +c.n otherwise
Time required to merge n elements
Time required to sort right sub array and right subarray

Time required to sort left sub array and right subarray

Where a and c are constants.

T(n) = T(n/2) +T(n/2) +c.n


=2T(n/2)+c.n
Where c.n is the time required to merge n elements , t(n/2) is the time to sort left
subarary and t(n/2) is the time to sort right subarary.

Let n = 2k
T(2k)=T(2k) +T(2k) +c.2k
2 2

=T(2k-1) +T(2k-1) + c.2k

T(2k) =2T(2k-1) + c.2k ---------------eq.(1)


Put k=k-1 in eq.(1)

T(2k-1) = 2T(2k-1-1) + c.2k-1 --------------------eq.(2)

T(2k-1) = 2T(2k-2) + c.2k-1


Similarly find T(2k-2) by substituting k=k-2

Design and analysis of algorithms - A Beginners Guide Page 15


Unit II – Divide and Conquer

T(2k-2) = 2T(2k-2-1) + c.2k-2

T(2k-2) = 2T(2k-3) + c.2k-2 --------------------eq.(3)

Substituting T(2k-1) in eq.(1),we get

T(2k) = 2T(2k-1) + c.2k


=2[ 2T(2k-2) + c.2k-1] + c.2k
= 4T(2k-2) +2 c.2k-1 + c.2k
= 4T(2k-2) +21 c.2k-1 + c.2k
=4T(2k-2) + c.2k-1+1 + c.2k
T(2 ) = 4T(2k-2) + c.2k + c.2k
k

Similarly substituting for T(2k-2) from eq.(3) we get,

T(2k) = 4T(2k-2) + c.2k + c.2k


T(2k) = 22 [2T(2k-3) + c.2k-2] + 2 c.2k

= 8. 2T(2k-3) +4. c.2k-2 + 2 c.2k

= 8. 2T(2k-3) +4. c.2k-2 + 2 c.2k

= 8. 2T(2k-3) +4. c.2k-2 + 2 c.2k

= 23 2T(2k-3) +22. c.2k-2 + 2 c.2k

=23 2T(2k-3) + c.2k-2+2 + 2 c.2k

=23 2T(2k-3) + c.2k + 2 c.2k

T(2k) =23 2T(2k-3) + 3.c.2k

and so on…

Considering k=3 and generalizing it we get,

T(2k) = 2k 2T(2k-k) + k.c.2k

= 2k 2T(20) + k.c.2k

T(2k) = 2k 2T(1) + k.c.2k

As n=2k and k=log2n

T(n)= n.a + log2n.c.n

Design and analysis of algorithms - A Beginners Guide Page 16


Unit II – Divide and Conquer

T(n)= n.a + c.n.log2n

Ignoring the constant T(n) =O(n log2n )


Therefore time complexity of merge sort is given by
T (n) = O (n log2n)

Tracing of Merge Sort:


60,50,25,10,35,25,75,30

60,50,25,10 35,25,75,30
Divide
60,50 25,10 35,25 75,30

60 50 25 10 35 25 75 30
---------------------------------------------------------------------------

Conquer 50, 60 10, 25 25, 35 30, 75

10,25,50,65 25,30,35,75

10, 25, 25, 30, 35, 50, 60, 75

2.6.2 Quick Sort


It sorts by partitioning the array into two pieces separated by a single element that is greater
than all the elements on the left side and smaller than all the elements in the right side. This
single element called as the key element is in its correct position in the sorted list. Then the
process is repeated to the two pieces separately.

Quick Sort (Partition Exchange sort)

ALGORITHM QUICKSORT (A,low,high)


//purpose sort the elements of the array b/w the lower and upper bound

//input A is an unsorted array with low and high as lower and upper bound
//output A is a sorted array

if( low < high)


mid←partition (a,low,high)
quick_sort(a,low,mid-1)
quick_sort(a,mid+1,high)
endif

Design and analysis of algorithms - A Beginners Guide Page 17


Unit II – Divide and Conquer

Algorithm partition(A,low,high)
//purpose: Partition the array into parts such that elements towards left than
the key element and elements towards right of key are greater
than key element.

//input: A is unsorted from the index position low to high


//output : A is divided into two parts such that items towards left key
element are less than key element and elements towards right of
the key element are greater than the key element

key←a[low]
i←low;
j←high+1

while ( i<=j)
do
i←i+1
while(key >=a[i])
do
j←j-1
while(key <=a[j])
if(i<j) exhange(a[i],a[j])
endwhile
exchange(a[j],a[low])
return j

Analysis – Quick sort

Best Case:
Occurs when the key element is placed in middle and there are 2 subarrays (half
size of the original on both the sides)

The recurrence relation for quick sort is

T(n)= a if n=1
T(n/2) +T(n/2) +cn otherwise
Time required to merge n elements
Time required to sort right sub array

Time required to sort left sub array


Where a and c are constants.

T(n) = T(n/2) +T(n/2) +c.n


=2T(n/2)+c.n

Design and analysis of algorithms - A Beginners Guide Page 18


Unit II – Divide and Conquer

Let n = 2k
T(2k)=T(2k) +T(2k) +c.2k
2 2

=T(2k-1) +T(2k-1) + c.2k

T(2k) =2T(2k-1) + c.2k ---------------eq.(1)


Put k=k-1 in eq.(1)

T(2k-1) = 2T(2k-1-1) + c.2k-1 --------------------eq.(2)

T(2k-1) = 2T(2k-2) + c.2k-1


Similarly find T(2k-2) by substituting k=k-2

T(2k-2) = 2T(2k-2-1) + c.2k-2

T(2k-2) = 2T(2k-3) + c.2k-2 --------------------eq.(3)

Substituting T(2k-1) in eq.(1),we get

T(2k) = 2T(2k-1) + c.2k


=2[ 2T(2k-2) + c.2k-1] + c.2k
= 4T(2k-2) +2 c.2k-1 + c.2k
= 4T(2k-2) +21 c.2k-1 + c.2k
=4T(2k-2) + c.2k-1+1 + c.2k
T(2k) = 4T(2k-2) + c.2k + c.2k

Similarly substituting for T(2k-2) from eq.(3) we get,

T(2k) = 4T(2k-2) + c.2k + c.2k


T(2k) = 22 [2T(2k-3) + c.2k-2] + 2 c.2k

= 8. 2T(2k-3) +4. c.2k-2 + 2 c.2k

= 8. 2T(2k-3) +4. c.2k-2 + 2 c.2k

= 8. 2T(2k-3) +4. c.2k-2 + 2 c.2k

= 23 2T(2k-3) +22. c.2k-2 + 2 c.2k

=23 2T(2k-3) + c.2k-2+2 + 2 c.2k

=23 2T(2k-3) + c.2k + 2 c.2k

T(2k) =23 2T(2k-3) + 3.c.2k


and so on…

Design and analysis of algorithms - A Beginners Guide Page 19


Unit II – Divide and Conquer

Considering k=3 and generalizing it we get,

T(2k) = 2k 2T(2k-k) + k.c.2k

= 2k 2T(20) + k.c.2k

T(2k) = 2k 2T(1) + k.c.2k

As n=2k and k=log2n

T(n)= n.a + log2n.c.n

T(n)= n.a + c.n.log2n

Ignoring the constant T(n) = ꭥ (n log2n )


Therefore time complexity of quick sort -best case is given by
T (n) = ꭥ (n log2n )

Worst case : T(n)=O(n2)


This happens when all elements are in sorted order and after partitioning all elements
are on one side of the key element
Example
20, 33,45,67,88
Left sub array is empty all elements are in right subarray

Therefore T(N)=T(N-1) + c.N


T(N-1) is the Cost to sort N-1 elements
C. N is the cost to partition
T(N)=T(N-1) + c.N -------------eq.(1)

Put N=N-1 in eq.(1)


T(N-1)=T(N-1-1)+c.N-1
T(N-1)=T(N-2)+c.N-1

Put N=N-2 in eq.(1)


T(N-2)=T(N-2-1)+c. N-2
T(N-2)=T(N-3)+c. N-2

Replace T(N-1) in eq.(1)


T(N)=T(N-2)+c.N-1+c.N
Now replace T(N-2 in above euation,
T(N)=T(N-3)+c. N-2+c.N-1+c.N -------->eq.(2)

and so on…

Design and analysis of algorithms - A Beginners Guide Page 20


Unit II – Divide and Conquer

Put N=3 in eq.(2)


T(N)=T(N-N)+c(1)+c(2)+……c(N)
T(N)=T(0)+c[1+2+…+N]
T(N)=0 + c.N(N+1)
2

T(N)=c. N2+N = c (N2+N)


2 2

T(n)=O(n2)

Therefore time complexity of quick sort -best case is given by O(n2)

Average case :

Let K be the position of split


1 2 ---- n-1 n
K
K-1 N-K
Let Number of comparisons made by partition on the first cell is N+1.
Each possible position or split point has the probability of 1/N
Therefore T(N) = (N+1) + 1 /N
T(N)=0 if N=0 OR 1
T(N) = (N+1) 1/N
Multiply with N in both side
N.T(N) = N(N+1) +[T(1-1) + T(2-1) + T (3-1) + T(N-1)] + [T(N-1) +T(N-2)+T(N-3)
+…..+ T(0) ]
N.T(N)=N(N+1) +[T(0) + T(1) +...+ T(N-1)] + [T(N-1) +T(N-2) + T(N-3)+…+T(1)
+ T(0) ]

N.T(N)=N(N+1) +2[T(0) +T(1)+…T(N-1)] -------  eq.(1)


Replace N by N-1 in eq.(1)
(N-1).T(N-1)=(N-1)(N-1+1)+2[T(0) + T(10+…T(N-1-1)]

(N-1).T(N-1)=(N-1_.(N)+2[T(0) +T(1) +….T(N-2)] -------- eq.(2)

Subtract eq.(1) – eq.(2)

N.T(N) - (N-1).T(N-1) = N2 + N=N2+N+2T(N-1)


N.T(N)=(N-1).T(N-1) + 2N+2T(N-1)
= T(N-1) [N-1+2]+2N
N.T(N)= T(N-1) [ N+1] +2N
Divide both sides by N(N+1) We get
/ N(N+1) = T(N-1) [N+1] +2N / N(N+1)

Design and analysis of algorithms - A Beginners Guide Page 21


Unit II – Divide and Conquer

T(N) / N+1 = T(N-1) /N + 2N/N+1 ------- eq.(3)

T(N)/ N+1 = T(N-1) /N + 2/N+1


Find T(N-1)/N from eq.(3) by putting N=N-1
T(N-1)/N-1+1 = T(N-1-1) / N-1 + 2/N-1+1
T(N-1)/2 = T(N-2) / N-1 + 2/N

Similarly , find T(N-2)/N-1 from eq.(3) putting N=N-2


T(N-2)/N-2+1 = T(N-2-1)/N-2 + 2/N-2+1
T(N-2)/N-1 = T(N-3)/N-2 + 2/N-1

Replace the value back in eq.(3)= T(N)/N+1 = T(N-2)/N-1 + 2/N + 2/N+1


= T(N-3)/N-2 + 2/N-1 + 2/N + 2/N+1
= T(N-N)/ N-(N-1) + [2/N-1 + 2/N + 2/N+1 + …..2/ N + (N-2)]
Take N=3
= T(0)/N-N+1 + [1/2 + 1/3 + 1/4 +…..1/N+1]
As T(0) = 0 We get

=
=2 1∫N 1/K dk
= 2 log e N – loge 1
T(N)/ N+1 = 2 loge N
T(N) = (N+1) .2 loge N
=2 N loge N > loge N
T(N) = θ (N log N)

Tracing of Quick Sort:


Consider the elements: 42 37 11 98 36 72 65 10 88 78

low i j high
→42 37 11 98 36 72 65 10 88 78 (42>37 so, increment i)
key
i
42 37 11 98 36 72 65 10 88 78 (42>11 so, increment i)
i
42 37 11 98 36 72 65 10 88 78 (42<98 stop incrementing i
and compare 42 with a[j]
which is 78)
i j
42 37 11 98 36 72 65 10 88 78 (42<78 so, increment i)

i j
42 37 11 98 36 72 65 10 88 78 (42<88 so, decrement j)

Design and analysis of algorithms - A Beginners Guide Page 22


Unit II – Divide and Conquer

i j
42 37 11 98 36 72 65 10 88 78 (since 42 < 10 is false
stop decrementing j)
since i<j exchange a[i] with a[j] and repeat the process

i j
42 37 11 10 36 72 65 98 88 78 (42>10 ,increment i)

j i
42 37 11 10 36 72 65 98 88 78(42 > 36,increment i)

i j
42 37 11 10 36 72 65 98 88 78(42 > 72,is false so stop
incrementing i and compare
42 with a[j] i.e.,98)
i j
42 37 11 10 36 72 65 98 88 78(42 < 98 decrement j)

i j
42 37 11 10 36 72 65 98 88 78(42 < 65,decrement j)

ij
42 37 11 10 36 72 65 98 88 78(42 <72,decrement j)

j i
42 37 11 10 36 72 65 98 88 78(42 <36,is false)

Since i exceeds j ,exchange a[low] and a[j] , now the array will be

36 37 11 10 42 72 65 98 88 78

Elements towards left of Elements towards right of


42 are less than 42 42 are greater than 42

Repeatedly sort the left sub array and sort the right sub array.

Advantages of divide and conquer:


1. Easy to solve difficult problems
2. Sorting can be efficiently done as the cost will be O(n logn)
3. Parallelism can be achieved as the sub problems can be executed by multiple
processors.

Disadvantages:
1. Recursions are slow with divide and conquer.

Design and analysis of algorithms - A Beginners Guide Page 23


Unit II – Divide and Conquer

2. Implementing recursion with divide and conquer requires memory for


recursion stack.
3. Not suitable for small iterative problems.

Questions:
1. Write the control abstraction for divide and conquer method.
2. Write an algorithm for finding maximum and minimum using straight forward
approach and find the time complexity.
3. Write an algorithm for finding maximum and minimum using divide and
conquer approach and find the time complexity.
4. Explain the process of binary search on a set of elements{23,56,64,73,82,88,90}
with the search element=208
5. Write an algorithm for merge sort using divide and conquer method and find its
time complexity.
6. Trace the following set of elements using merge sort.
4, 17, 8, 44, 109, 435, 16
7. Write an algorithm for quick sort using divide and conquer method and find its
time complexity.
8. Write a recursive binary search algorithm and analyse its time complexity.
9. Write the advantages of divide and conquer technique.
10. Trace the following set of elements using quick sort.
42, 37, 88, 22, 13, 45, 6

******************

Design and analysis of algorithms - A Beginners Guide Page 24

You might also like