CS 5381 Analysis of Algorithms Solutions To Homework 1: Fall 2022
CS 5381 Analysis of Algorithms Solutions To Homework 1: Fall 2022
Solutions to Homework 1
Fall 2022
0 ≤ c1 nb ≤ (n + a)b ≤ c2 nb , ∀n ≥ n0 .
1
Observe that when |a| ≤ n, one has
n + a ≤ n + |a| ≤ 2n
n + a ≥ n − |a| ≥ n/2.
0 ≤ n/2 ≤ n + a ≤ 2n.
Since b > 0, the inequality still holds when all parts are raised to the
power of b as
0 ≤ (1/2)b nb ≤ (n + a)b ≤ 2b nb .
Thus, we obtain c1 = (1/2)b , c2 = 2b , and n0 = 2|a|.
T (n) = T (n − 1) + n
≤ c(n − 1)2 + n
= cn2 + c(1 − 2n) + n.
The last quantity is less than or equal to cn2 if c(1 − 2n) + n ≤ 0 or,
equivalently, c ≥ n/(2n − 1), which holds for all n ≥ 1 and c ≥ 1. Thus,
we can choose n0 = 1 and c = 1.
4. Using the recursion tree shown in Figure 1 on the next page, we get
a guess of T (n) = Θ(n).
2
Figure 1: Problem 4
We then use the substitution method to first prove that T (n) = O(n).
Our inductive hypothesis is that T (n) ≤ cn for some constant c > 0.
We have
3
5. We have a = 8, b = 2, and f (n) = Θ(n2 ), and so nlogb a = nlog2 8 = n3 .
Since n3 is polynomially larger than f (n), case 1 of the master theorem
applies, and T (n) = Θ(n3 ).
4
Since the ordering of hats is random, each customer has a probability of
1/n of getting back his or her own hat. That is E[X] = P r{Xi = 1} =
1/n. Therefore,
" n #
X
E[X] = E Xi
i
n
X
= E[Xi ]
i=1
n
X 1
= = 1,
i=1
n
and so we expect that exactly 1 customer gets back his or her own hat.