



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!




Recurrence relations naturally appear when we solve a problem by dividing it up in smaller prob- lems, solve these smaller problems, and then get the solution for the original problem by somehow putting together the solutions for the smallest problems. We started the class by describing the Merge Sort algorithm. The algorithm follows the “Divide and conquer” paradigm where a problem is split in subproblems which are solved individually; the solutions of the smaller problmes are then combined to obtain a solution for the original problem. The idea of Merge Sort is to split the input array to be sorted, say A in two smaller arrays of half the size of A and sort these smaller arrays. The solution for the original problem is obtained by “merging” the ordered versions of the two halfs of A. Because the resulting algorithm is recursive it is difficult to immediately tell what is its running time. An important observation at this point is that the running time the algorithm can be described by a recurrence relation: if we write T (n) for the running time of Merge sort on arrays of size n, then the structure of the algorithm tells us that
T (n) = 2T ( n 2
) + n
To figure out what is the function T that this recurrence relation describes we built up a recurrence tree. Think about this tree as a picture that capture the time the algorithm takes throughout the execution. Each node of the tree corresponds to some part of the algorithm executed on some data of a specific size: each leaf correspond to some end of the recursive calls, and each internal node corresponds to executing the merging part of Mergesort. The total time taken by the algorithm can be recovered as the sum of the time spent in each node. To do this we figure out:
T (n) = 3T (n/4) + cn^2
This relation is analyzed in detail at pages 68-69 of [CLRS].
I explained why the recurrence tree method (or the telescoping sums method) are not fully precise: there are issues that we disregard like weather n is divisible by 2i, and we often use.. .. The substitution method is a way to formally prove upper and lower bounds of functions described by recurrence relations. Here is a (simpler) example that those covered in class. Example 1. You have already study the binary search algorithm. This algorithm searches for the position on which some value e occurs in an array A sorted in increasing order. The algorithm comparess e with the mid-element of A (say A[n/2]). If e = A[n/2] then we’re done. Otherwise, if e is less than A[n/2] the algorithm recursively searches e in the first half of the array A[1..n/2]. Otherwise, the algorithm recursively searches e in the second half of the array, i.e. A[n/ 2 ..n]. If T (n) is the worst case running time of the algorithm for inputs of size n, then according to the above description we have that:
T (n) = T (n/2) + k (1)
which expresses the idea that to solve the problem for an array of size n, we solve it for an array of size n/2 and do some other, constant time operations. We also have that T (1) = 1 (if the size of the array is 1 the result is simply given by the comparison). We now aim to figure out how fast does T (n) grow, that is, an upper bound on T (n). Usually, if you don’t know and can’t guess the class to which T (n) belongs to you have to figure it out somehow. Below, I’ll use essentially recurrence trees, but I’m doing it in the form of telescoping sums (as you’ll see there isn’t much to sum up here). Notice that from (1) we have that
T (n/2) = T (n/4) + k (2)
which, replacing back in (1) gives T (n) = T (n/4) + 2k
Continuing this way we get that: T (n) = T (n/ 2 l) + lk
We set l = log n and get: T (n) = T (n/ 2 log^ n) + k log n
Since 2log^ n^ = n we get: T (n) = T (1) + k log n
so T (n) ∈ O(log n). To rigorously prove that T (n) ∈ O(log n) we proceed using the substitution method. The idea is the following. We want to prove that T (n) ∈ O(log n), which formally means that we want to prove that there exists some c and some n 0 such that:
(∀n ≥ n 0 ) : T (n) ≤ c log n.
We proceed by (strong) induction. We need to show the two steps specific to induction:
Next, we find n 0 for which the base case works. If n 0 = 1 then, as in the previous example, we cannot set c such that T (1) ≤ c1 log 1. If n 0 = 2 then T (n 0 ) = 2 · T (1) + 2d = 2 + 2d. For the base case to go through it is sufficient to set c such that 1 + 2d ≤ c · 2 · log 2, that is c ≥ 2 d 2 +2 = d + 1 Example 3. Please study the recurrence T (n) = T (n/3) + T (2n/3) + n which is in [CLRS] pages 70-72 (the difference is in the second term). This is an example of a recurrence relation that cannot be solved by the Master Theorem (described bellow), and only the substitution method works.
The master theorem is a handy tool to deal with recurrences of the form T (n) = aT (n/b) + f (n) when a ≥ 1 , b > 1. To apply the theorem you have to compare the functions f (n) and nlogb^ a. There are three cases:
Here are a couple of examples:
Example 1 T (n) = 9T (n/3) + n: here a = 9, b = 3 and f (n) = n. Since nlogb^ a^ = nlog^3 9 = n^2 it’s clear that n ∈ O(n^2 −) for some small , so we are in case 1 of the theorem. We conclude that T (n) = Θ(n^2 ).
Example 2 T (n) = 3T (n/4) + n log n: here a = 3, b = 4 and f (n) = n log n. We have nlogb^ a^ = nlog^4 3 so f (n) ∈ Ω(nlog^4 3 + ) (since 4 < 3 it is the case that log 4 3 < 1 so it’s easy to see that f (n) is lowerbounded by nlog^4 3+^ for some epsilon). We’re in case 3, if we also prove the regularity condition which in this case is:
af (n/b) = 3(n/4) log n/ 4 ≤ cn log n
for some c < 1. That’s easy to see since 3n/4 log n/ 4 < 3 / 4 n log n ≤ cn log n for any c ∈ [3/ 4 , 1). We are in case 3, and therefore have that T (n) = Θ(n log n).
Throughout the part regarding asymptotic analysis, and more genearlly, throughout this course we have ignored constants and only compared algorithms in terms of their growth. This is ok, but you should be aware of the whole story: In practice constants are very, very, very important and researchers spend a lot of effort to bring the constants down. The difference between an algorithm with running time n and one 2n is that on inputs on which the first one takes one year, the second one takes two years. Furthermore, a quadratic algorithm that runs in time n^2 will beat a linear
algorithm that runs in time 10^9 · n for all inputs of size up to 10^9 (and these may be the inputs that one actually cares about).