14 Solving Recurrence Relations MASTER THEOREM. Let a ≥ 1 and b > 1 be integers and c ≥ 0 and d > 0 real numbers. Let T (n) be defined Solving recurrence relations is a difficult business and for integers that are powers of b by there is no catch all method. However, many relations aris- aT ( n )+ nc if n> 1 ing in practice are simple and can be solved with moderate T (n) = b effort. d if n =1. Then we have the following: A few functions. A solution to a recurrence relation c is generally given in terms of a function, eg. f(n) = • T (n)=Θ(n ) if logb a<c; n log n, or a class of similar functions, eg. T (n) = c 2 • T (n)=Θ(n log n) if logb a = c; O(n log n). It is thereforeuseful to get a feeling for some 2 log a of the most common functions that occur. By plotting the • T (n)=Θ(n b ) if logb a>c. graphs, as in Figure 13, we get an initial picture. Here we see a sequence of progressively faster growing functions: This behavior can be explained by recalling the formula n 0 n−1 1−r constant, logarithmic, linear, and exponential. However, for a geometric series, (r + . + r )A = 1−r A, and focusing on the magnitude of the constant factor, r. For 0 <r< 1, the sum is roughly A, the first term, for r =1, the sum is n, the number of terms, and for r > , the sum 8 1 is roughly rn−1A, the last term. 7 Let us consider again the recursion tree and, in partic- 6 ular, the total work at its i-th level, starting with i = 0 at 5 the root. There are ai nodes and the work at each node is n c 4 ( bi ) . The work at the i-th level is therefore 3 c i i n c a 2 a i = n ic . b b 1 There are 1 +logb n levels, and the total work is the sum 0 over the levels. This sum is a geometric series, with factor 0 1 2 3 4 5 6 7 8 a r = bc . It is therefore dominated by the first term if r< 1, all terms are the same if r = 0, and it is dominated by Figure 13: The graphs of a small set of functions, f(x) = 1, x the last term if r > 1. To distinguish between the three f(x) = log2 x, f(x)= x, f(x) = 2 . cases, we take the logarithm of r, which is negative, zero, positive if r< 1, r =1, r> 1. It is convenient to take the such plots can be confusing because they depend on the logarithm to the basis b. This way we get scale. For example, the exponential function, f(x)=2x, grows a lot faster than the quadratic function, f(x)= x2, a c logb = logb a − logb b but this would not be obvious if we only look at a small bc portion of the plane like in Figure 13. = logb a − c. We have r < 1 iff logb a<c, In which case the dom- Three regimes. In a recurrence relation, we distinguish inating term in the series is nc. We have r = 1 iff c between the homogeneous part, the recursive terms, and logb a = c, in which case the total work is n logb n. We the inhomogeneous part, the work that occurs. The solu- have r > 1 iff logb a>c, in which case the dominating n a tion of depends on the relative size of the two, exhibiting term is d · alogb = d · nlogb . This explains the three qualitatively different behavior if one dominates the other cases in the theorem. or the two are in balance. Recurrence relations that exhibit There are extensions of this result that discuss the cases this three-regime behavior are so common that it seems in which n is not a lower of b, we have floors and ceilings worthwhile to study this behavior in more detail. We sum- in the relation, a and b are not integers, etc. The general marize the findings. behavior of the solution remains the same. 39 Using induction. Once we know (or feel) what the solu- STEP 3. Split the set into S, the items smaller than the tion to a recurrence relation is, we can often use induction median of the medians, and L, the items larger than to verify. Here is a particular relation defined for integers the median of the medians. that are powers of 4: STEP 4. Let s = |S|. If s < k − 1 then return the (k − s)- T ( n )+ T ( n )+ n if n> 1 smallest item in L. If s = k − 1 then return the T (n) = 2 4 median of the medians. if s > k − 1 then return the 1 if n =1. k-smallest item in S. To get a feeling for the solution, we group nodes with n n equal work together. We get n once, 2 once, 4 twice, The algorithm is recursive, computing the median of n n n 8 three times, 16 five times, etc. These are the Fibonacci roughly 5 medians in Step 2, and then computing an item numbers, which grow exponentially, with basis equal to either in L or in S. To prove that the algorithm terminates, the golden ratio, which is roughly 1.6. On the other hand, we need to show that the sets considered recursively get the work shrinks exponentially, with basis 2. Hence, we strictly smaller. This is easy as long as n is large but tricky have a geometric series with factor roughly 0.8, which is for small n. We ignore these difficulties. less than one. The dominating term is therefore the first, and we would guess that the solution is some constant times n. We can prove this by induction. CLAIM. There exists a positive constant c such that T (n) ≤ cn. PROOF. For n = 1, we have T (1) = 1. Hence, the claimed inequality is true provided c ≥ 1. Using the Figure 14: The upper left shaded region consists of items smaller strong form of Mathematical Induction, we get than the median of the medians. Symmetrically, the lower right n n shaded region consists of items larger than the median of the T (n) = T + T + n medians. Both contain about three tenth of all items. 2 4 n n = c + c + n To get a handleon the runningtime, we need to estimate 2 4 how much smaller than n the sets S and L are. Consider 3c = +1 n. Figure 14. In one iteration of the algorithm, we eliminate 4 either all items smaller or all items larger than the median 3c of the medians. The number of such items is at least the This is at most cn provided 4 +1 ≤ c or, equivalently, c . number in one of the two shaded regions, each containing ≥ 4 3n roughly 10 items. Hence, the recurrence relation describ- The inductive proof not only verified what we thought ing the running time of the algorithm is might be the case, but it also gave us the smallest constant, c =4, for which T (n) ≤ cn is true. T ( 7n )+ T ( n )+ n if n>n T (n) = 10 5 0 n0 if n ≤ n0, Finding the median. Similar recurrence relations arise 7 1 for some large enough constant n0. Since 10 + 5 is strictly in practice. A classic example is an algorithm for finding less than one, we guess that the solution to this recurrence the k-smallest of an unsorted set of n items. We assume relation is again O(n). This can be verified by induction. the items are all different. A particularly interesting case is the middle item, which is called the median. For odd n+1 n, this is the k-smallest with k = 2 . For even n, we n+1 set k equal to either the floor or the ceiling of 2 . The algorithm takes four steps to find the k-smallest item. STEP 1. Partition the set into groups of size 5 and find the median in each group. STEP 2. Find the median of the medians. 40.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages2 Page
-
File Size-