Download 3 - Collegeguide

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
-Fundamentals of Algorithms
Lecture No.09
Lecture No. 9
3.1.2 The Iteration Method for Solving Recurrence Relations
Floor and ceilings are a pain to deal with. If n is assumed to be a power of 2 (2k = 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.
3.1.3 Visualizing Recurrences Using the Recursion Tree
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)
Page 1
of 4
-Fundamentals of Algorithms
Lecture No.09
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
3.1.4 A Messier Example
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 = log4n
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 n = 4k and T(1) = 1
Page 2
of 4
-Fundamentals of Algorithms
Lecture No.09
= 3log4 nT(1) +
We used the formula alogb n = nlogb a. n remains constant throughout the sum and
3i/4i = (3/4)i; we thus have
The sum is a geometric series; recall that for x  1
In this case x = 3/4 and m = log4n - 1. We get
Applying the log identity once more
If we plug this back, we get
Page 3
of 4
-Fundamentals of Algorithms
Lecture No.09
With log4 3  0.79, we finally have the result!
T(n) = 4n - 3nlog4 3  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
Position 2
2
5
3
7
4
8
5
10
6
15
7
21
8
37
9
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.
Page 4
of 4