Professional Documents
Culture Documents
Divide and Conquer: Analysis of Algorithms
Divide and Conquer: Analysis of Algorithms
Divide and Conquer: Analysis of Algorithms
Chapter 2
Divide and Conquer
The General Method
Binary search
Maximum and Minimum
Merge Sort
Quick Sort
Selection Sort
There are many ways to design algorithms. Insertion sort uses an incremental approach: having sorted
the sub array A[1 . . j −1], we insert the single element A[ j ] into its proper place, yielding the sorted sub array
A[1 . . j ]. In this section, we examine an alternative design approach, known as ―divide and- conquer.‖ We shall
use divide-and-conquer to design a sorting algorithm whose worst-case running time is much less than that of
insertion sort. One advantage of divide-and-conquer algorithms is that their running times are often easily
determined using techniques.
The Divide and Conquer approach:
Many useful algorithms are recursive in structure: to solve a given problem, they call themselves
recursively one or more times to deal with closely related sub problems. These algorithms typically follow a
divide-and-conquer approach: they break the problem into several sub problems that are similar to the original
problem but smaller in size, solve the sub problems recursively, and then combine these solutions to create a
solution to the original problem.
The divide-and-conquer paradigm involves three steps at each level of the recursion:
Divide the problem into a number of sub problems.
Conquer the sub problems by solving them recursively. If the sub problem sizes are small enough,
however, just solve the sub problems in a straightforward manner.
Combine the solutions to the sub problems into the solution for the original problem.
To be more precise, suppose we consider the divide-and-conquer strategy when it splits the input into
two sub problems of the same kind as the original problem. DAndC algorithm is initially invoked as DAndC(P),
where P is the problem to be solved.
Where T(n) is the time for DAndC on any input of size ‗n‘ and g(n) is the time to compute the answer directly
for small inputs. The function f(n) is the time for dividing P and combining the solutions to subproblems.
Fordivideand-conquer-based algorithms that produce subproblems of the same type as the original problem, it is
very natural to first describe such algorithms using recursion.
The complexity of many divide-and-conquer algorithms is given by recurrences of the form
Where ‗a‘ and ‗b‘ are known constants. We assume that T(l) 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 substitution for each occurrence of the function T in the right-hand side until all such occurrences
disappear.
Example: Consider the case in which a = 2 and b = 2. Let T(l) and f(n) = n. We have
T(n) = 2T(n/2)+ n
= 2[2T(n/4)+ n/2] + n
= 4T(n/4) +2n
= 4[2T(n/8) + n/4] + 2n
= 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) = 2lgnT(n/2lgn)+nlog2n,
corresponding to the choice of i = log2n. Thus, T(n) = nT(l)+ n log2n = n log2n + 2n.
Thus Time complexity = O(nlgn).
Binary Search
Let ai, 1< i <n, be a list of elements that are sorted in non-decreasing order. Consider the problem of
determining whether a given element ‗x‘ is present in the list. If x is present, we are to determine a value j such
that aj = x. If ‗x‘ is not in the list, then j is to be set to zero. Let P = (n, ai,…..al , x) denote an arbitrary instance of
this search problem(n is the number of elements in the list, ai,…..al is the list of elements and x is the element
searched for).
Divide-and-conquer can be used to solve this problem. Let Small(P) be true if n = 1. In this case, S(P)
will take the value i if x = ai, otherwise it will take the value 0. Then g(l) = Θ(1). If P has more than one element,
it can be divided (or reduced) into a new sub problem as follows. Pick an index q (in the range [i,l]) and
compare x with aq. There are three possibilities: (1) x = aq: In this case the problem P is immediately solved.
(2) x< aq: In this case x has to be searched for only in the sub list ai,ai+1,..,aq-1. Therefore, P reduces to
(q-1, ai,….,aq-1,x). (3) x > aq: In this case the sublist to be searched is aq+1,…..al. P reduces to (l-q, aq+1,…..al, x).
In this example, any given problem P gets divided (reduced) into one new subproblem. This division
takes only Θ(1) time. After a comparison with aq, the instance remaining to be solved (if any) can be solved by
using this divide-and-conquer scheme again. If q is always chosen such that aq is the middle element (that is,q =
2 Prepared by: Mujeeb Rahman
floor((n + l)/2)), then the resulting search algorithm is known as binary search. Note that the answer to the new
sub problem is also the answer to the original problem P; there is no need for any combining. Algorithm given
below describes this binary search method, where BinSrch has four inputs a[ ], i, l and x. It is initially invoked
as BinSrch(a,1,n,x).
Example:
Pass 1: Pass 2:
Pass 3: Pass 4:
Best Case: Best case occurs, when we are searching the middle element itself. In this case, the total number of
comparisons required is 1. Therefore, Best Case time complexity is O(1).
Worst Case: Let
– T(n) – cost involved to search n elements
– T(n/2) – cost involved to search either left part or right part
3 Prepared by: Mujeeb Rahman
The recurrence relation is given by
T(n) = T(n/2)+b
= T(n/22)+2b
= T(n/23)+3b
.
.
= T(n/2k)+kb if n/2k =1, k = lg n
= a + lg n . b
= lg n ( by neglecting a & b)
= O(lg n)
A good way of keeping track of recursive calls is to build a tree by adding a node each time a new call is made.
For this algorithm each node has four items of information: i,j, max, and min. On the array a[ ] above, the tree of
Figure is produced.
Examining Figure, we see that the root nodecontains1and 9 as the values of i and j corresponding to the
initial call to MaxMin. This execution produces two new calls to MaxMin, where i and j have the values 1,5 and
6,9 respectively, and thus split the set into two subsets of approximately the same size. From the tree we can
immediately see that the maximum depth of recursion is four (including the first call). The circled numbers in
the upper left corner of each node represent the orders in which max and min are assigned values.
Time Complexity:
Now what is the number of element comparisons needed for MaxMin? If T(n) represents this number,
then the resulting recurrence relation is
T(n) = 2T(n/2)+2
= 2(2T(n/4)+2)+2
= 4T(n/4) +4+2
15 i ← i + 1
16 else A[k] ← R[ j ]
17 j ← j + 1
We can now use the MERGE procedure as a subroutine in the merge sort algorithm. The procedure MERGE-
SORT(A, p, r) sorts the elements in the subarray A[p . . r]. If p ≥ r, the subarray has at most one element and is
therefore already sorted. Otherwise, the divide step simply computes an index q that partitions A[p . . r] into two
subarrays: A[p . . q], containing ceil(n/2) elements, and A[q + 1 . . r], containing floor(n/2) elements.
MERGE-SORT(A, p, r) To sort the entire sequence A = <A[1], A[2], . . . ,
1 if p < r A[n]>, we make the initial call MERGE-SORT(A, 1,
2 then q ← _(p + r)/2_ length[A]), where once again length[A] = n.
3 MERGE-SORT(A, p, q)
4 MERGE-SORT(A, q + 1, r)
5 MERGE(A, p, q, r)
6 Prepared by: Mujeeb Rahman
Merge: Merging means combining two sorted lists
into one sorted list. For this, the elements from both
the sorted lists are compared. The smaller of both the
elements is then stored in the third array. The sorting
is complete when all the elements from both the lists
are placed in the third list.
T(n) = 2T(n/2)+ n
= 2[2T(n/4)+ n/2] + n
= 4T(n/4) +2n
= 4[2T(n/8) + n/4] + 2n
= 23T(n/23)+ 3n
.
.
In general, we see that T(n) = 2iT(n/2i) + in, for any log2n > i > 1.In particular, then, T(n) = 2lgnT(n/2lgn)+nlog2n,
corresponding to the choice of i = log2n. Thus, T(n) = nT(l)+ n log2n = n log2n + 2n.
Thus Time complexity = O(nlgn).
Conquer
Apply the same algorithm to each half
= 2[2T(n/4)+ n/2] + n
= 4T(n/4) +2n
= 4[2T(n/8) + n/4] + 2n
= 23T(n/23)+ 3n
.
.
In general, we see that T(n) = 2iT(n/2i) + in, for any log2n > i > 1.In particular, then, T(n) = 2lgnT(n/2lgn)+nlog2n,
corresponding to the choice of i = log2n. Thus, T(n) = nT(l)+ n log2n = n log2n + 2n.
Thus Time complexity = O(nlgn).
Selection
The Partition operation of quick sort can also be used to obtain an efficient solution for the selection
problem. In this problem, we are given n elements a[1 : n] and are required to determine the kth-smallest element.
If the partitioning element (pivot element) x is positioned at a[q], then (q – 1) elements are less than or equal to
a[q] and (n - q) elements are greater than or equal to a[q]. Hence if k < q. then the kth-smallest element is in a[1 :
q — 1]; if k = q, then a[q] is the kth-smallest element; and if k > q, then the kth-smallest element is (k-q)th-smallest
element in a[q + 1 : n].
20 10 30 40 70 50 60 80 20 10 30 40 50 60 70 80
20 10 30 40 50 60 70 80
Worst case time complexity and best case time complexity of the above algorithm is O(n2) and O(n)
respectively.
***