Docsity

Recurrence Relations: Merge Sort Algorithm and Analysis, Slides of Introduction to Computers

Toggle sidebar
bg1
COMS21103: Summary of Lectures 5 and 6
Recurrence relations: recurrence trees
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 Ain two smaller arrays of half
the size of Aand 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 Tthat 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:
1) the height hof the tree (for this we need to solve the equation 2h=n, i.e. h= log2n) 2) how
many leaves there are (for a complete binary tree of height hthere are 2hleaves). It turns out that
the sum of nodes at level iin this tree is n, so we get that the total time is 2h+ (h−1)n: the first
term in this sum represents the time spent in the leaves, and the next term corresponds the time
spent on all of the levels (bar the level of the leaves, that’s where the −1 comes from). In total we
thus get that T(n) = 2log2n+ (log2n−1)n∈O(nlog n).
We also drew the recurrence tree that corresponds to the relation:
T(n) = 3T(n/4) + cn2
This relation is analyzed in detail at pages 68-69 of [CLRS].
1
pf3
pf4
pf5

Partial preview of the text

Download Recurrence Relations: Merge Sort Algorithm and Analysis and more Slides Introduction to Computers in PDF only on Docsity!

COMS21103: Summary of Lectures 5 and 6

Recurrence relations: recurrence trees

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:

  1. the height h of the tree (for this we need to solve the equation 2h^ = n, i.e. h = log 2 n) 2) how many leaves there are (for a complete binary tree of height h there are 2h^ leaves). It turns out that the sum of nodes at level i in this tree is n, so we get that the total time is 2h^ + (h − 1)n: the first term in this sum represents the time spent in the leaves, and the next term corresponds the time spent on all of the levels (bar the level of the leaves, that’s where the −1 comes from). In total we thus get that T (n) = 2log^2 n^ + (log 2 n − 1)n ∈ O(n log n). We also drew the recurrence tree that corresponds to the relation:

T (n) = 3T (n/4) + cn^2

This relation is analyzed in detail at pages 68-69 of [CLRS].

Recurrence relations: the substitution method

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:

  1. Base case: T (n 0 ) ≤ c log n 0 and

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.

Recurrence relations: the Master Theorem

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:

  1. f (n) = O(nlogb^ a−) for some  > 0 in which case you directly conclude that T (n) = Θ(nlogb^ a)
  2. f (n) = Θ(nlogb^ a) in which case you directly conclude that T (n) = Θ(nlogb^ a^ log n)
  3. f (n) = Ω(nlogb^ a^ +) for some  > 0 and there exists some c ∈ (0, 1) such that for all sufficiently large n it holds that af (n/b) ≤ cf (n) (This is called the regularity condition). If these are satisfied, then you can conclude that T (n) ∈ Θ(f (n)).

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).

Disclaimer

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).

Further reading

  • Chapter 4, Section 4.1 of CLRS (for the substitution method)
  • There are plenty of problems to practice on pages 75 and 85.