Solution to CS 515 Assignment 2 - Classes
Solution to CS 515 Assignment 2 - Classes
Solution to CS 515 Assignment 2 - Classes
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
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