0% found this document useful (0 votes)
17 views5 pages

CS 5381 Analysis of Algorithms Solutions To Homework 1: Fall 2022

This document contains solutions to 9 problems related to analysis of algorithms. The problems cover topics like asymptotic analysis, recursion trees, the master theorem and probability. Mathematical expressions and reasoning are provided as part of the solutions.

Uploaded by

nettemnarendra27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

CS 5381 Analysis of Algorithms Solutions To Homework 1: Fall 2022

This document contains solutions to 9 problems related to analysis of algorithms. The problems cover topics like asymptotic analysis, recursion trees, the master theorem and probability. Mathematical expressions and reasoning are provided as part of the solutions.

Uploaded by

nettemnarendra27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CS 5381 Analysis of Algorithms

Solutions to Homework 1
Fall 2022

1. Define the function h(n) = max(f (n), g(n)), so that


(
f (n) if f (n) ≥ g(n)
h(n) =
g(n) if f (n) < g(n).

Since f (n) and g(n) are asymptotically nonnegative, there exists a n0


such that f (n) ≥ 0 and g(n) ≥ 0 for all n ≥ n0 .

Thus, for n ≥ n0 , f (n) + g(n) ≥ f (n) ≥ 0 and f (n) + g(n) ≥ g(n) ≥


0. Since for any particular n, h(n) is either f (n) or g(n), we have
f (n) + g(n) ≥ h(n) ≥ 0. This shows that h(n) = max(f (n), g(n)) ≤
c2 (f (n) + g(n)) for all n ≥ n0 , where c2 = 1.

Similarly, we have for all n ≥ n0 , 0 ≤ f (n) ≤ h(n) and 0 ≤ g(n) ≤ h(n).


Adding these two inequalities yields 0 ≤ (f (n) + g(n))/2 ≤ h(n). This
shows that h(n) = max(f (n), g(n)) ≥ c1 (f (n) + g(n)) for all n ≥ n0 ,
where c1 = 1/2.

2. We need to find constants c1 , c2 , n0 > 0 such that

0 ≤ c1 nb ≤ (n + a)b ≤ c2 nb , ∀n ≥ n0 .

1
Observe that when |a| ≤ n, one has

n + a ≤ n + |a| ≤ 2n

and when |a| ≤ n/2, one has

n + a ≥ n − |a| ≥ n/2.

Thus, when n ≥ 2|a|, it holds

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

3. We conjecture that T (n) ≤ cn2 for some constant c > 0. We have

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

T (n) = T (n/2) + T (n/4) + T (n/8) + n


≤ cn/2 + cn/4 + cn/8 + n
= (1 + 7c/8)n
≤ cn if c ≥ 8.

Thus, T (n) = O(n). Showing that T (n) = Ω(n) is easy:

T (n) = T (n/2) + T (n/4) + T (n/8) + n ≥ n.

Therefore, we have T (n) = Θ(n).

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

6. We have a = 2, b = 2, and f (n) = n3 , and so nlogb a = nlog2 2 =


n. Since f (n) is polynomially larger than nlogb a , case 3 of the master
theorem applies, and T (n) = Θ(n3 ), where the regularity condition can
also be easily verified.

7. We have a = 3, b = 2, and f (n) = nlgn, and so nlogb a = nlg3 ≈ n1.58 .


Since nlgn = O(nlg3− ) for any 0 <  ≤ 0.58, case 1 of the master
theorem applies, and T (n) = Θ(nlg3 ).

8. Since HIRE-ASSISTANT always hires candidate 1, it hires exactly


once if and only if no candidates other than candidate 1 are hired. This
event occurs when candidate 1 is the best candidate of the n, which oc-
curs with probability 1/n.

HIRE-ASSISTANT hires n times if each candidate is better than all those


who were interviewed (and hired) before. This event occurs precisely
when the candidates come in strictly increasing order of quality, which
occurs with probability 1/n!.

9. Define a random variable X that equals the number of customers


that get back their own hat, so that we want to compute E[X]. For
i = 1, 2, . . . , n, define the indicator random variable
Xi = I{customer i gets back his or her own hat}.
Then X = X1 + X2 + · · · + Xn .

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.

You might also like