











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!












(^1) Note: These are informal notes that are to be used only as a supporting mate-
rial. Use them at your own risk. Please report any errors and typographical mis- takes to Ajay Gupta ([email protected]) or Don Nelson ([email protected]).
1 Math Preliminaries
Below you will find some formulae that are useful for analysis of algorithms. The list is by no means exhaustive, rather it is designed to give you a flavor of different ideas we may need.
Conventions
f (i) = 0 if x > y.
∑^ x i=x
f (i) = f (x).
Logarithms
∑^ y i=x
(f (i) + g(i)) = ∑^ y i=x
f (i) + ∑^ y i=x
g(i) for some functions f and g.
(Pause: Is ∑^ y i=x
f (i) + ∑^ y k=x
g(k) = ∑^ y i=x
(f (i) + g(i))?)
(Long Pause: Is ∑^ y i=x
(f (i) × g(i)) = ( ∑^ y i=x
f (i)) × ( ∑^ y i=x
g(i))?)
∑^ y i=x
(g(j) × f (i)) = g(j) × ∑^ y i=x
f (i) whenever g(j) is not a function of the running variable i.
f (i) = ∑^ z i=x
f (i) + ∑^ y k=z+
f (k); ∀z such that x ≤ z ≤ y.
Let’s consider the series of the form ∑^ n i=
ik^ for some positive constant k, and
try to establish “closed forms” represented by these. We know that ∑^ n i=
1 = n
(why?). How about ∑^ n i=
i? This series represents sum of the first n natural
numbers. Let’s first consider the case when n is an even integer. If we add the first and last number we get (1 + n), add the second and the second-last number we get (2 + (n − 1)) = (1 + n) and add the third and the third-last number we get (3 + (n − 2)) = (1 + n). Continuing in this fashion, it is easy to see that we need to add a total of n/2 pairs each one resulting in a sum
of (1 + n). Hence, one can say that ∑^ n i=
i = n(n^2 + 1) for even n.
(Pause: What happens when we use a similar strategy but n is odd?)
Now, suppose that we claim ∑^ n i=
i = n(n^2 + 1) for all n > 0. How can we
be sure that in fact our claim is correct? One way to make sure is to use mathematical induction. So, let’s try it.
For n = 1, 2 we can easily verify that our claim is true and hence our claim holds for the base case. Now by induction hypothesis (IH) assume that ∑^ k
i=
i = k(k^2 + 1) for all values of k < n. We need to prove that our claim
holds for n k = n. We know that ∑ i=
i =
n∑− 1 i=
i + n = (n−1)((n 2 −1)+1)+ n use IH with k = n − 1 = n(n 2 +1) Let us now consider the series ∑^ n i=
i^2. In order to find a closed form for
this series, we illustrate a method which is also applicable for finding closed forms for the series ∑^ n i=
ik^ for k > 2. It is easy (why?) to verify that
∑^ n i=
(i + 1)^3 − ∑^ n i=
i^3 = (n + 1)^3 − 1 (1)
We first expand the left-hand-side (LHS) of equation 1 so that terms involving only i^2 , i and 1 remain. We thus have
∑^ n i=
(i + 1)^3 − ∑^ n i=
i^3 = ∑^ n i=
(i^3 + 3i^2 + 3i + 1) − ∑^ n i=
i^3
= ∑^ n i=
i^3 + 3 ∑^ n i=
i^2 + 3 ∑^ n i=
i + ∑^ n i=
∑^ n i=
i^3
= 3 ∑^ n i=
i^2 + 3 ∑^ n i=
i + ∑^ n i=
Now, using equation 2 and the right-hand-side (RHS) of equation 1, we obtain
3 ∑^ n i=
i^2 + 3 ∑^ n i=
i + ∑^ n i=
1 = (n + 1)^3 − 1 (3)
∑^ n i=
i^2 = (n + 1)^3 − 1 − 3 ∑^ n i=
i − ∑^ n i=
the form ∑^ n i=
(ik^ × ci) which looks similar to a combination of an arithmetic-
like and a geometric series? We now describe a method which helps us in finding a closed form when k = 1; i.e., closed form for the series
∑^ n i=
ici. (7)
We can first use a strategy which is similar to the one for geometric series and then strategy similar to the one for arithmetic-like series. Let S again denote the closed form for the series 7. We have:
S = ∑^ n i=
ici^ (8)
c × S = c × ∑^ n i=
ici
= ∑^ n i=
ici+
c × S =
n∑+ i=
(i − 1)ci^ (9)
Subtracting equation 8 from equation 9, we obtain
(c − 1)S =
n∑+ i=
(i − 1)ci^ − ∑^ n i=
ici = (ncn+1^ + ∑^ n i=
(i − 1)ci) − ( ∑^ n i=
ici^ + c) = ncn+1^ − c + ( ∑^ n i=
(i − 1)ci^ − ∑^ n i=
ici)
= ncn+1^ − c + ∑^ n i=
(i − 1 − i)ci = ncn+1^ − c − ∑^ n i=
ci = ncn+1^ − c − (cn+1^ − c^2 )/(c − 1) =⇒ S = (ncn+1^ − c)/(c − 1) − (cn+1^ − c^2 )/(c − 1)^2
Long Break: Try finding closed form for ∑^ n i=
i^2 ci.
3 Recurrence Relations
Suppose that T (n) represents the cost of performing an algorithm on a data set of size n. Often T (n), when originally constructed, will not be in some “closed form” representing a function of n, rather, it will be given in terms of one or more values of T (k) for values of k smaller than n. Consider the following examples:
T (n) =
1 if n = 0, T (n − 1) + T (n − 2) if n > 1
T (n) =
1 if n = 0, 1 T (n/2) + n if n > 1
T (n) =
c if n = 1 c +
n∑− 1 i=
T (i) if n > 1
Equation 10 is given in terms of the previous two terms; equation 11 is given in terms of the value of T at n/2; and equation 12 is given in terms of all the preceding values of T. The last equation is referred to as a full history recurrence relation. Whenever such recurrence relations represent the cost of performing an algorithm, it becomes important to establish a bound on T as a function of n, the size of the problem. For example, can we establish a bound on T (n) if T is given by equation 10? It is easy to show using mathematical induction that 2n^ is a bound. First observe that T (0) ≤ 20 and T (1) ≤ 21. Thus, we
= 2 2(k+1)
Again, notice in the inequalities 14 and 15 that extremely coarse over- estimates were made. As was the case with the estimate for equation 10, this might lead one to suspect that the bound we are considering is much too large. Let’s try a smaller bound. Can we show that T (n) ≤ 2 n − 1. It is certainly true for n = 1 = 2^0. Assume that it is true for n = 2k. Then
T (2k+1) = T (2k) + 2k+ ≤ 2 × 2 k^ − 1 + 2k+ = 2 × 2 k+1^ − 1 As a final observation about this last bound, note that it is easy to show that T (n) = 2n − 1; i.e., we can find an exact solution (for powers of 2) instead of merely finding an upper bound. In each of the examples that we have considered, an upper bound was proposed, and then shown to work. Other guesses can also be made, but it may be difficult or impossible to establish that the proposed bound is actually a bound. For example, could you show for equation 11 that T (n) ≤ c×log 2 n?
Establishing whether or not a given function represents a bound for a re- currence relation is one thing, but coming up with a reasonable bound is another. In the following material we will use the method of expanding a recurrence relation in order to arrive at a solution or a bound. To see how this method works, let’s consider again the recurrence relation given in 11. Suppose that n = 2p^ for some integer p. To expand the relation, we will start
with n and work our way down, step by step, until a pattern can be seen.
T (n) = T (n/2) + n = T (n/ 22 ) + n/2 + n = T (n/ 23 ) + n/ 22 + n/2 + n ...
= T (n/ 2 p) + n
p∑− 1 i=
1 / 2 i
= 1 + n
p∑− 1 i=
1 / 2 i
= 1 + n^1 −^ (1/2)
p 1 − 1 / 2 = 1 + 2n(1 − (1/n)) = 1 + 2n − 2 = 2 n − 1 Next we will illustrate another expansion to solve the following recurrence relation.
T (n) =
c if n = 1 2 T (n/2) + cn^2 if n > 1
As was the case with the first expansion above, we will assume that n = 2p for some integer p. Then the expansion proceeds as follows.
T (n) = 2 T (n/2) + cn^2 = 2(2T (n/ 22 ) + c(n/2)^2 ) + cn^2 = 22 T (n/ 22 ) + cn^2 (1/4 + 1) = 22 (2T (n/ 23 ) + c(n/ 22 )^2 ) + cn^2 (1/4 + 1)
This relation is similar to those expanded above; however, we observe that it is expressed in terms of the general parameters, a, b, c and k. We will proceed to expand this in the same way; however, we will obtain only upper bounds rather than exact solutions. At one point in the expansion, we will find it necessary to consider cases which depend on the parameters a, b, and k. Let
b^ np^ = 1 for some^ p >^ 0.
T (n) = aT (n/b) + cnk = a^2 T (n/b^2 ) + acnk/bk^ + cnk = a^2 (T (n/b^2 ) + cnk(a/bk^ + 1) = a^2 (aT (n/b^3 ) + cnk/b^2 k) + cnk(a/bk^ + 1) = a^3 T (n/b^3 ) + cnk((a/bk)^2 + a/bk^ + 1) ...
= apT (1) + cnk
p∑− 1 i=
(a/bk)i
= cap^ + cnk
p∑− 1 i=
(a/bk)i
The sum in the last equation above represents a geometric series, so its growth is dependent on the ratio (a/bk). To estimate the growth of T (n) we will consider three cases for the ratio.
In this case
p∑− 1 i=
(a/bk)i^ is bounded by (^1) −a/b^1 k , which we will denote by G.
Thus we can write
T (n) ≤ cap^ + Gcnk = calogb(n)^ + Gcnk = cnlogb(a)^ + Gcnk < cnk^ + Gcnk = O(nk)
In this case
p∑− 1 i=
(a/bk)i^ = p = logb(n) , and we can write
T (n) = cap^ + cnk^ logb(n) = cnlogb(a)^ + cnk^ logb(n) = cnk^ + cnk^ logb(n) = O(nk^ logb(n))
In this case (^) a/b^1 k (^) − 1 is a positive constant, which we will denote by H. Then
p∑− 1 i=
(a/bk)i^ = (a/b
k)p^ − 1 a/bk^ − 1
and hence:
Subtracting equation 19 from equation 20 and solving for T (n + 1) yields:
(n + 1)T (n + 1) − nT (n) = 2 n + 2T (n) T (n + 1) = (^) n^2 + 1 n + n n^ + 2+ 1T (n) (21)
Notice that Equation 21 now gives T (n + 1) in terms of T (n). As before, we will attempt to expand based on 21, only the method will be somewhat different than that used for the previous recurrence relations. We first make an observation about the term 2n/(n+1) in equation 21. It is bounded above by 2, and as n becomes large, it is very close to 2. By replacing 2n/(n + 1) with 2, we can convert the equation to an inequality and have a system that is much simpler to solve. Since our main concern will ultimately be to find a bound on the growth of T (n), this substitution will not defeat our goal.
T (n + 1) < n n^ + 2+ 1T (n) + 2 = n n^ + 2+ 1(n^ + 1 n T (n − 1) + 2) + 2 = n^ + 2 n T (n − 1) + 2( nn + 1^ + 2) + 2 = n^ + 2 n ( (^) n −n 1 T (n − 2) + 2) + 2( nn + 1+ 2) + 2
= n n^ + 2− 1 T (n − 2) + 2(n + 2) ∑^2 i=
n + 2 − i = n n^ + 2− 1 (n n^ −−^12 T (n − 3) + 2) + 2(n + 2) ∑^2 i=
n + 2 − i = n n^ + 2− 2 T (n − 3) + 2(n + 2) ∑^3 i=
n + 2 − i ...
= (^) n n−^ + 2p + 1T (n − p) + 2(n + 2) ∑^ p i=
n + 2 − i
Letting p = n − 1 we obtain
T (n + 1) ≤ n^ + 2 2 T (1) + 2(n + 2)
n∑− 1 i=
n + 2 − i = 2(n + 2)
n∑− 1 i=
n + 2 − i = 2(n + 2)
n + 1 +^
n + 2
)
= O((n + 2) log 2 (n + 2)) = Θ((n + 1) log 2 (n + 1))
T (n) =
c if n = 1 3 T (n 2 ) + cn if n ≥ 2
T (n) =
c if n = 1 2 T (n 2 ) + cn log n if n ≥ 2
T (n) =
c if n = 1 3 T (n 2 ) + cn log n if n ≥ 2
T (n) =
c if n = 1 T (n 4 ) + cn log n if n ≥ 2
T (n) =
c if n = 1, 2 , 3 , 4 T (n 2 + T (n 4 ) + cn if n ≥ 5