



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Fundamentals of Algorithm, The Iteration Method for Solving Recurrence Relations, Visualizing Recurrences Using the Recursion Tree, A Messier Example, Selection Problem are the key points in this study notes file.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Floor and ceilings are a pain to deal with. If n is assumed to be a power of 2 ( 2 k = n), this will simplify the recurrence to
The iteration method turns the recurrence into a summation. Let’s see how it works. Let’s expand the recurrence:
T(n) = 2T(n/2) + n = 2(2T(n/4) + n/2) + n = 4T(n/4) + n + n = 4(2T(n/8) + n/4) + n + n = 8T(n/8) + n + n + n = 8(2T(n/16) + n/8) + n + n + n = 16T(n/16) + n + n + n + n
If n is a power of 2 then let n = 2k^ or k = log n.
Iteration is a very powerful technique for solving recurrences. But, it is easy to get lost in all the symbolic manipulations and lose sight of what is going on. Here is a nice way to visualize what is going on in iteration. We can describe any recurrence in terms of a tree, where each expansion of the recurrence takes us one level deeper in the tree. Recall that the recurrence for MergeSort (which we simplified by assuming that n is a power of 2, and hence could drop the floors and ceilings)
Suppose that we draw the recursion tree for MergeSort, but each time we merge two lists, we label that node of the tree with the time it takes to perform the associated (no recursive) merge. Recall that to 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.
Figure 3.3: Merge sort Recurrence Tree
The merge sort recurrence was too easy. Let’s try a messier recurrence.
Assume n to be a power of 4, i.e., n = 4k^ 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 =... = 3k^ T(n/4k) +3k-1^ (n/4k-1) + …. + 9(n/16) + 3(n/4) + 4
With log 4 3 ≈ 0.79, we finally have the result!
T(n) = 4n - 3nlog 43 ≈ 4n - 3n0.79^2 ∈ (n)
3.2 Selection Problem Suppose we are given a set of n numbers. Define the rank of an element to be one plus the number of elements that are smaller. Thus, the rank of an element is its final position if the set is sorted. The minimum is of rank 1 and the maximum is of rank n. Consider the set: {5, 7, 2, 10, 8, 15, 21, 37, 41}. The rank of each number is its position in the sorted order.
Position 1 2 3 4 5 6 7 8 9 Position 2 5 7 8 10 15 21 37 41
For example, rank of 8 is 4, one plus the number of elements smaller than 8 which is 3. The selection problem is stated as follows:
Given a set A of n distinct numbers and an integer k, 1 _ k _ n, output the element of A of rank k.
Of particular interest in statistics is the median. If n is odd then the median is defined to be element of rank (n + 1)/2. When n is even, there are two choices: n/2 and (n + 1)/2. In statistics, it is common to return the average of the two elements.
Medians are useful as measures of the central tendency of a set especially when the distribution of values is highly skewed. For example, the median income in a community is a more meaningful measure than average. Suppose 7 households have monthly incomes 5000, 7000, 2000, 10000, 8000, 15000 and 16000. In sorted order, the incomes are 2000, 5000, 7000, 8000, 10000, 15000, 16000. The median income is 8000; median is element with rank 4: (7 + 1)/2 = 4. The average income is 9000. Suppose the income 16000 goes up to 450,000. The median is still 8000 but the average goes up to 71,000. Clearly, the average is not a good representative of the majority income levels. The selection problem can be easily solved by simply sorting the numbers of A and returning A[k]. Sorting, however, requires _(n log n) time. The question is: can we do better than that? In particular, is it possible to solve the selections problem in _(n) time? The answer is yes. However, the solution is far from obvious.