



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!




601.433/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/2/
Throughout the course we will use O(·), Ω(·), and Θ(·) notation in order to “hide” constants. This is called asymptotic notation – you should have seen it in data structures (and possibly discrete math), but we’ll do a quick refresher to make sure that everyone is on the same page. In addition to making bounds simpler and easier to compare, asymptotic notation and analysis also forces us to focus on how algorithms scale. While for small inputs easy algorithms with bad bounds might be reasonable, at scale it is not the constants that matter, it is the asymptotics. This is particularly true now that we are in the “big data” era – when considering these huge problems, the constants are completely dominated by the asymptotics.
The following definition is the most basic and important:
Definition 2.1.1 g(n) ∈ O(f (n)) if there exist constants c, n 0 > 0 such that g(n) ≤ c · f (n) for all n > n 0.
While technically O(f (n)) is a set (hence the ∈ notation), we will usually say that “g(n) is O(f (n))” or that g(n) = O(f (n)).
Examples:
Let’s see an example proof of the first of these examples.
Theorem 2.1.2 2 n^2 + 27 = O(n^2 )
Proof: Let’s set c = 3. Now suppose that 2n^2 + 27 > cn^2 = 3n^2. Then simple algebra implies that n^2 < 27, and thus n < 6. So 2n^2 + 27 ≤ 3 n^2 for all n ≥ 6. So if we set n 0 = 6, we have proved that 2n^2 + 27 ≤ cn^2 for all n > n 0. Thus we have proved that 2n^2 + 27 = O(n^2 ).
This notation is particularly useful for giving upper bounds on the running times of algorithms, but note that it’s notation about functions, not about running times – we can also use it to give upper bounds on space usage, approximation ratio, etc. The important thing to remember is that it expresses an upper bound.
The natural complement is Ω(·) notation:
Definition 2.1.3 g(n) ∈ Ω(f (n)) if there exist constants c, n 0 > 0 such that g(n) ≥ c · f (n) for all n > n 0.
This notation lets us provide a lower bound on functions as they grow. Examples:
While most of this class is about upper bounds, we will occasionally discuss lower bounds as well so this is very useful notation to know.
The combination of these two is Θ(·) notation:
Definition 2.1.4 g(n) ∈ Θ(f (n)) if g(n) ∈ O(f (n)) and g(n) ∈ Ω(f (n)).
Note that it is not necessary for the proof that g(n) = O(f (n)) to use the same constants n 0 , c as the proof that g(n) = Ω(f (n))!
Two more useful pieces of notation are the strict versions of O and Ω:
Definition 2.1.5 g(n) ∈ o(f (n)) if for every constant c > 0 there exists a constant n 0 > 0 such that g(n) < c · f (n) for all n > n 0.
Definition 2.1.6 g(n) ∈ ω(f (n)) if for every constant c > 0 there exists a constant n 0 > 0 such that g(n) > c · f (n) for all n > n 0.
Examples:
A large fraction of the algorithms we will see are recursive, in which case it is very natural to express their running time through a recurrence relation. We saw this last lecture with Karatsuba’s algorithm for multiplication and Strassen’s algorithm for matrix multiplication. A more familiar set of examples might be from sorting:
Technically we also need to have a base case. When n is constant it is almost always obvious (and is certainly true of all of our examples) that a trivial brute-force algorithm takes constant time. So
This is the technique I always use, and is essentially a generalization of unrolling. It consists of drawing out the recursion tree, and analyzing it level by level. Let’s do two examples. First, let’s analyze the mergesort recursion T (n) = 2T (n/2) + cn. Let’s draw the tree:
cn
cn/2 cn/
cn/4 cn/4 cn/4 cn/
cn/8 cn/8 cn/8 cn/8 cn/8 cn/8 cn/8 cn/
The total value of T (n) is the sum of the value of every node in the tree. We can analyze it level by level. The first level clearly contributed cn. The second level contributes 2c(n/2) = cn. The third level contributes 4c(n/4) = cn. In general, level i contributes 2i−^1 c(n/ 2 i−^1 ) = cn. This bottoms out when i = log n + 1, since then we have T (1) = c. Since there are log n + 1 levels, the total value of T (n) is cn(log n + 1) = Θ(n log n).
Now let’s analyze the recurrence that we got last time for Strassen’s algorithm:
T (n) = 7T (n/2) + cn^2.
As before, we first draw the tree.
cn^2
c(n/2)^2 c(n/2)^2
c(n/2)^2 c(n/2)^2 c(n/2)^2 c(n/2)^2 c(n/2)^2
c(n/4)^2 c(n/4)^2 c(n/4)^2 c(n/4)^2 c(n/4)^2 c(n/4)^2 c(n/4)^2
Now the value of the first level is cn^2 , the value of the second level is 7c(n/2)^2 = 7cn^2 /4, the value of the third level is 7^2 c(n/ 22 )^2 , etc. So the value of level i is 7i−^1 c(n/ 2 i−^1 )^2 = (7/4)i−^1 cn^2. When we sum over all log n + 1 levels, we get a total cost of
T (n) =
log∑ n+
i=
)i− 1 cn^2 = cn^2
log∑ n+
i=
)i− 1
This is dominated by the last term (if you don’t see why, try pulling out a factor of (7/4)log^ n−^1 and testing whether the remaining sequence converges). Thus overall we get that
T (n) = O(n^2 (7/4)log^ n) = O(n^2 nlog(7/4)) = O(n^2 nlog 7−^2 ) = O(nlog 7)
Let’s use the recursion tree to prove a more general theorem. Suppose we have a recursion of the form
T (n) = aT (n/b) + cnk T (1) = c
where a, b, c, and k are all constants with a ≥ 1, b > 1, c > 0, and k ≥ 0. When we draw the recursion tree, we get the following:
We can put these together to get the following theorem, which is sometimes called the “master theorem”.
Theorem 2.2.1 The recurrence
T (n) = aT (n/b) + cnk T (1) = c
where a, b, c, and k are constants with a ≥ 1 , b > 1 , c > 0 , and k ≥ 0 , is equal to
T (n) = Θ(nk) if a < bk, T (n) = Θ(nk^ log n) if a = bk, T (n) = Θ(nlogb^ a) if a > bk.