






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







The ancient Roman politicians understood an important principle of good algorithm design (although they were probably not thinking about algorithms at the time). You divide your enemies (by getting them to distrust each other) and then conquer them piece by piece. This is called divide-and-conquer. In algorithm design, the idea is to take a problem on a large input, break the input into smaller pieces, solve the problem on each of the small pieces, and then combine the piecewise solutions into a global solution. But once you have broken the problem into pieces, how do you solve these pieces? The answer is to apply divide-and-conquer to them, thus further breaking them down. The process ends when you are left with such tiny pieces remaining (e.g. one or two items) that it is trivial to solve them. Summarizing, the main elements to a divide-and-conquer solution are
Divide: the problem into a small number of pieces
Conquer: solve each piece by applying divide and conquer to it recursively
Combine: the pieces together into a global solution.
Divide and conquer strategy is applicable in a huge number of computational problems. The first example of divide and conquer algorithm we will discuss is a simple and efficient sorting procedure called We are given a sequence of n numbers A, which we will assume are stored in an array A[1..n]. The objective is to output a permutation of this sequence sorted in increasing order. This is normally done by permuting the elements within the array A. Here is how the merge sort algorithm works:
sub-array. We move the smaller element into the next position of B (indicated by index k) and then increment the corresponding index (either i or j). When we run out of elements in one array, then we just copy the rest of the other array into B. Finally, we copy the entire contents of B back into A. (The use of the temporary array is a bit unpleasant, but this is impossible to overcome entirely. It is one of the shortcomings of MergeSort, compared to some of the other efficient sorting algorithms.)
Here is the merge algorithm:
MERGE( array A, int p, int q int r) 1 int B[p..r]; int i โ k โ p; int j โ q + 1 2 while (i โค q) and (j โค r) 3 do if (A[i] โค A[j]) 4 then B[k++ ] โ A[i++ ] 5 else B[k++ ] โ A[j++ ] 6 while (i โค q) 7 do B[k++ ] โ A[i++ ] 8 while (j โค r) 9 do B[k++ ] โ A[j++ ] 10 for i โ p to r 11 do A[i] โ B[i]
Figure 3.2: Merge sort: combine phase
First let us consider the running time of the procedure Merge(A, p, q, r). Let n = r โ p + 1 denote the total length of both the left and right sub-arrays. What is the running time of Merge as a function of n? The algorithm contains four loops (none nested in the other). It is easy to see that each loop can be executed at most n times. (If you are a bit more careful you can actually see that all the while-loops
together can only be executed n times in total, because each execution copies one new element to the array B, and B only has space for n elements.) Thus the running time to Merge n items is ฮ(n). Let us write this without the asymptotic notation, simply as n. (Weโll see later why we do this.)
Now, how do we describe the running time of the entire MergeSort algorithm? We will do this through the use of a recurrence, that is, a function that is defined recursively in terms of itself. To avoid circularity, the recurrence for a given value of n is defined in terms of values that are strictly smaller than n. Finally, a recurrence has some basis values (e.g. for n = 1 ), which are defined explicitly.
Let T (n) denote the worst case running time of MergeSort on an array of length n. If we call MergeSort with an array containing a single item (n = 1 ) then the running time is constant. We can just write T (n) = 1 , ignoring all constants. For n > 1, MergeSort splits into two halves, sorts the two and then merges them together. The left half is of size dn/2e and the right half is bn/2c. How long does it take to sort elements in sub array of size dn/2e? We do not know this but because dn/2e < n for n > 1, we can express this as T (dn/2e). Similarly the time taken to sort right sub array is expressed as T (bn/2c). In conclusion we have
T (n) =
1 if n = 1 , T (dn/2e) + T (bn/2c) + n otherwise
This is called recurrence relation , i.e., a recursively defined function. Divide-and-conquer is an important design technique, and it naturally gives rise to recursive algorithms. It is thus important to develop mathematical techniques for solving recurrences, either exactly or asymptotically.
Letโs expand the terms.
T ( 1 ) = 1 T ( 2 ) = T ( 1 ) + T ( 1 ) + 2 = 1 + 1 + 2 = 4 T ( 3 ) = T ( 2 ) + T ( 1 ) + 3 = 4 + 1 + 3 = 8 T ( 4 ) = T ( 2 ) + T ( 2 ) + 4 = 8 + 8 + 4 = 12 T ( 5 ) = T ( 3 ) + T ( 2 ) + 5 = 8 + 4 + 5 = 17
... T ( 8 ) = T ( 4 ) + T ( 4 ) + 8 = 12 + 12 + 8 = 32 ... T ( 16 ) = T ( 8 ) + T ( 8 ) + 16 = 32 + 32 + 16 = 80 ... T ( 32 ) = T ( 16 ) + T ( 16 ) + 32 = 80 + 80 + 32 = 192
What is the pattern here? Letโs consider the ratios T (n)/n for powers of 2 :
T ( 1 )/1 = 1 T ( 8 )/8 = 4 T ( 2 )/2 = 2 T ( 16 )/16 = 5 T ( 4 )/4 = 3 T ( 32 )/32 = 6
This suggests T (n)/n = log n + 1 Or, T (n) = n log n + n which is ฮ(n log n) (using the limit rule).
merge two lists of size m/2 to a list of size m takes ฮ(m) time, which we will just write as m. Below is an illustration of the resulting recursion tree.
= n
2(n/2) = n
4(n/4) = n
8(n/8) = n
n(n/n) = n
log(n)+ levels
n(log(n)+1)
n
n/
n/4 n/
n/8 n/8 n/8 n/
(^1 1 1 1) ...... 1
n/
n/4 n/
n/8 n/8 n/8 n/
(^1 1 1 1) ...... 1
time to merge
Figure 3.3: Merge sort Recurrence Tree
The merge sort recurrence was too easy. Letโs try a messier recurrence.
T (n) =
1 if n = 1 , 3T (n/4) + n otherwise
Assume n to be a power of 4, i.e., n = 4 k^ and k = log 4 n
T (n) = 3T (n/4) + n = 3 (3T (n/16) + (n/4) + n = 9T (n/16) + 3 (n/4) + n = 27T (n/64) + 9 (n/16) + 3 (n/4) + n =...
= 3 kT (
n 4 k^
) + 3 kโ^1 (n/4kโ^1 )
= 3 kT (
n 4 k^
โ^ kโ^1
i= 0
3 i 4 i^
n
With n = 4 k^ and T ( 1 ) = 1
T (n) = 3 kT (
n 4 k^
โ^ kโ^1
i= 0
3 i 4 i^
n
= 3 log^4 nT ( 1 ) +
(log โ 4 n)โ 1
i= 0
3 i 4 i^
n
= nlog^4 3 +
(log โ 4 n)โ 1
i= 0
3 i 4 i^
n
We used the formula alogb^ n^ = nlogb^ a. n remains constant throughout the sum and 3 i/4i^ = (3/4)i; we thus have
T (n) = nlog^4 3 + n
(log โ 4 n)โ 1
i= 0
)i
The sum is a geometric series; recall that for x 6 = 1
โ^ m
i= 0
xi =
xm+^1 โ 1 x โ 1
In this case x = 3/4 and m = log 4 n โ 1. We get
T (n) = nlog^4 3 + n
(3/4)log^4 n+^1 โ 1 (3/4) โ 1
Applying the log identity once more
(3/4)log^4 n^ = nlog^4 (3/4)^ = nlog^4 3 โlog^4
= nlog^4 3 โ^1 =
nlog^4 n
If we plug this back, we get
T (n) = nlog^4 3 + n
nlog^4 n โ^1 (3/4) โ 1
= nlog^4 3 +
nlog^4 3 โ n โ1/ = nlog^4 3 + 4 (n โ nlog^4 3 ) = 4n โ 3nlog^4
With log 4 3 โ 0.79, we finally have the result!
T (n) = 4n โ 3nlog^4 3 โ 4n โ 3n0.79^ โ ฮ(n)
know what the desired the item is, but we can identify a large enough number of elements that cannot be the desired value, and can be eliminated from further consideration. In particular โlarge enoughโ means that the number of items is at least some fixed constant fraction of n (e.g. n/2, n/3). Then we solve the problem recursively on whatever items remain. Each of the resulting recursive solutions then do the same thing, eliminating a constant fraction of the remaining set.
To see more concretely how the sieve technique works, let us apply it to the selection problem. We will begin with the given array A[1..n]. We will pick an item from the array, called the pivot element which we will denote by x. We will talk about how an item is chosen as the pivot later; for now just think of it as a random element of A.
We then partition A into three parts.
Within each sub array, the items may appear in any order. The following figure shows a partitioned array:
Figure 3.4: A[p..r] partitioned about the pivot x
It is easy to see that the rank of the The rank of the pivot x is q โ p + 1 in A[p..r]. Let rank x = q โ p + 1. If k = rank x then the pivot is kth^ smallest. If k < rank x then search A[p..q โ 1 ] recursively. If k > rank x then search A[q + 1..r] recursively. Find element of rank (k โ q) because we eliminated q smaller elements in A.
SELECT( array A, int p, int r, int k)
1 if (p = r) 2 then return A[p] 3 else x โ CHOOSE PIVOT(A, p, r) 4 q โ PARTITION(A, p, r, x) 5 rank x โ q โ p + 1 6 if k = rank x 7 then return x 8 if k < rank x 9 then return SELECT(A, p, q โ 1, k) 10 else return SELECT(A, q + 1, r, k โ q)
Example: select the 6 th^ smallest element of the set {5, 9, 2, 6, 4, 1, 3, 7}
4
5
1
9 2
3 7
6 6
3
9
1 2
5 7
4
initial k=
partition pivot=
rank x = 4
6 9 5 7 recurse k=(6-4)=
rank x = 3
6
9
5 7
partition pivot=
rank x = 2
6 5
recurse k=
6
5
DONE!!
partition pivot=
pivot
pivot
pivot
Figure 3.5: Sieve example: select 6 th^ smallest element
We will discuss how to choose a pivot and the partitioning later. For the moment, we will assume that they both take ฮ(n) time. How many elements do we eliminate in each time? If x is the largest or the smallest then we may only succeed in eliminating one element.
5, 9, 2, 6, 4, 1 , 3, 7 pivot is 1 1 , 5, 9, 2, 6, 4, 3, 7 after partition
Ideally, x should have a rank that is neither too large or too small.
Suppose we are able to choose a pivot that causes exactly half of the array to be eliminated in each phase.