Solution to CS 515 Assignment 2 - Classes
Solution to CS 515 Assignment 2 - Classes
Solution to CS 515 Assignment 2 - Classes
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Solution</strong> <strong>to</strong> <strong>CS</strong> <strong>515</strong> <strong>Assignment</strong> 2<br />
(With inputs from Fawaz Alazemi and MingChieh Chang)<br />
Ex. 2 To find n, solve f(n) = 60 × 60 × 10 10<br />
a) f(n) = n 2 =⇒ n = 6 × 10 6<br />
b) f(n) = n 3 =⇒ n ≈ 33019<br />
c) f(n) = 100n 2 =⇒ n = 6 × 10 5<br />
d) f(n) = nlog 10 (n)/nlog 2 (n) =⇒ n ≈ 2.88 × 10 12 /n ≈ 9 × 10 11<br />
e) f(n) = 2 n =⇒ n ≈ 45<br />
f) f(n) = 2 2n =⇒ n ≈ 5<br />
Ex. 4 The functions can be compared by taking the log 2 of each.<br />
g 1 (n) = 2√<br />
log(n)<br />
=⇒ log(g 1 (n)) = √ log(n)<br />
g 2 (n) = 2 n =⇒ log(g 2 (n)) = n<br />
g 3 (n) = n(log(n)) 3 =⇒ log(g 3 (n)) = 3log(log(n)) + log(n)<br />
g 4 (n) = n 4 3 =⇒ log(g 4 (n)) = 4 3 log(n)<br />
g 5 (n) = n log(n) =⇒ log(g 5 (n)) = log(n)log(n)<br />
g 6 (n) = 2 2n =⇒ log(g 6 (n)) = 2 n<br />
g 7 (n) = 2 n2 =⇒ log(g 7 (n)) = n 2<br />
Comparing the above, the following relationships are easy <strong>to</strong> deduce:<br />
g 1 < g 3 ?g 4 < g 5 < g 2 < g 7 < g 6<br />
For g 3 and g 4 :<br />
log(g 3 (n)) = 3log(log(n)) + log(n) and re-arranging R.H.S of log(g 4 (n)),<br />
log(g 4 (n)) = 1 3log(n) + log(n)<br />
∴ g 3 (n) < g 4 (n)<br />
Ex. 5<br />
a) False.<br />
If f(n) = c > 1 and g(n) = 1, f(n) = O(g(n)).<br />
But, log(f(n)) = log(c) > log(g(n)) = log(1) = 0 when c > 1.<br />
=⇒ log(f(n)) ≠ O(log(g(n))).<br />
b) False.<br />
Let c > 1, f(n) = c × n and g(n) = n. Then, f(n) = O(g(n)).<br />
But, 2 (f(n)) = 2 c×n = (2 c ) n > 2 (g(n)) = 2 n when c > 1.<br />
=⇒ 2 (f(n)) ≠ O(2 (g(n)) ).<br />
c) True.<br />
Since f(n) and g(n) describe run-times, they are considered positive. f(x) = x 2 is<br />
uniformly increasing for x > 0. So, for n > 0, f(n) = O(g(n)).<br />
=⇒ f(n) 2 = O(g(n) 2 )<br />
Ex. 6 a) Max # of operations inside the inner for loop (when i = 1)<br />
= 1 + 2 + 3 + · · · + (n − 2) + (n − 1)<br />
= n(n−1)<br />
2<br />
=⇒ For every iteration of the outer for loop, there are O(n 2 ) operations inside the<br />
inner for loop<br />
Since the outer loop runs n times, the algorithm is O(n 3 )<br />
b) Consider # of operations till i = n 4 .<br />
When i = 1, # of addition operations = 1 + 2 + · · · + (n − 1).<br />
1
When i = 2, # of addition operations = 1 + 2 + · · · + (n − 2).<br />
· · ·<br />
When i = n 4 , # of addition operations = 1 + 2 + · · · + 3n 4 .<br />
T (n) > T ( n 4 ) = n 4 × ( n(n−1)<br />
2<br />
+ (n−1)(n−2)<br />
2<br />
+ · · · + 9n2 −12n<br />
32<br />
) > 9n3<br />
=⇒ The algorithm is Ω(n 3 ) and n 3 is a tight bound.<br />
c) There are many ways of doing this, including the following:<br />
for i = 2 <strong>to</strong> n do<br />
A[i] ← A[i − 1] + A[i]<br />
for i = 1 <strong>to</strong> n do<br />
for j = i + 1 <strong>to</strong> n do<br />
if i > 1 then<br />
B[i, j] ← A[j] − A[i − 1]<br />
else<br />
B[i, j] ← A[j]<br />
The first for loop takes O(n) time. For each iteration of the inner for loop, only a<br />
single operation is performed. For a given value of i, this loop runs for a maximum<br />
of n − 1 times (1st iteration). The outer for loop runs n times. =⇒ The nested for<br />
loops take O(n 2 ) time.<br />
Running time of algorithm = O(n 2 )<br />
Ex. 7 Let the # of verses in the song be v.<br />
Given: # of words in each line is bounded by c and the <strong>to</strong>tal # of words is n.<br />
∴ c + 2c + 3c + · · · + v · c = n<br />
c(1 + 2 + 3 + · · · + v) = n<br />
i.e. v 2 + v − 2n c = 0<br />
√ c+8n−<br />
√ c<br />
2 √ c<br />
v = ±<br />
Since the # of unique lines is the same as the # of verses and the # of words required <strong>to</strong><br />
encode instructions for repetition can be shown <strong>to</strong> be negligible, the length of the script is<br />
O( √ n)<br />
128 .<br />
Ex. 8<br />
a) Let the rungs be numbered 1, 2, 3, · · · , n. If we have 2 jars, the strategy is <strong>to</strong> divide<br />
n rungs in<strong>to</strong> groups of = ⌈√ n⌉ rungs each and drop the first jar from rungs<br />
n<br />
⌊ √ n⌋<br />
numbered ⌊ √ n⌋, 2⌊ √ n⌋, 3⌊ √ n⌋, · · · , n − (n mod ⌊ √ n⌋) until the jar breaks.<br />
If the jar breaks on rung i⌊ √ n⌋, drop the other jar from rung (i − 1)⌊ √ n⌋ + 1 <strong>to</strong><br />
i⌊ √ n⌋ − 1 until that jar breaks.<br />
Max # of drops required in step 1 = max # of groups = √ n.<br />
Max # of drops required in step 2 = max # of rungs in any group = √ n.<br />
Hence, <strong>to</strong>tal number of drops required is O( √ n), which is clearly sub-linear.<br />
b) Similar <strong>to</strong> (a), when there are k > 2 jars, the strategy is <strong>to</strong> divide n rungs<br />
in<strong>to</strong> groups of = ⌈ k√ n⌉ rungs and drop the first jar from rungs numbered<br />
n<br />
⌊ k√ n⌋<br />
⌊ k√ n⌋, 2⌊ k√ n⌋, 3⌊ k√ n⌋, · · · , n − (n mod ⌊ k√ n⌋) until it breaks.<br />
If the jar breaks on rung i⌊ k√ n⌋, recursively divide the number of rungs in the interval<br />
((i − 1)⌊ k√ n⌋, i⌊ k√ n⌋) by their (k − 1) th root <strong>to</strong> get groups within that interval. Do<br />
this until only one jar is left. Then, drop that jar from each rung in the last group<br />
starting with the lowest.<br />
In each step, the max # of drops required = max possible # of groups in any interval =<br />
k√ n.<br />
Max # of drops required when only 1 jar remains = max possible rungs in any group<br />
= k√ n.<br />
Hence, <strong>to</strong>tal number of drops required is O( k√ n).<br />
Also, lim n→∞<br />
n k<br />
1<br />
n k−1<br />
1<br />
= 0<br />
2