Ps 1 Sol
Ps 1 Sol
Ps 1 Sol
(a) Sometimes true: For f (n) = n it is true, while for f (n) = 1/n it is not true. (The
statement is always true for f (n) = Ω(1), and hence for most functions with which
we will be working in this course, and in particular all time and space complexity
functions).
(b) Sometimes true: For f (n) = 1 and g(n) = kn ∗ sin(n)k it is true, while for any
f (n) = O(g(n)), e.g. f (n) = g(n) = 1, it is not true.
(c) Always true: max(f (n), g(n)) ≤ f (n) + g(n) ≤ 2 max(f (n), g(n)).
(d) Always true: Consider f (n) + g(n) where g(n) = o(f (n)) and let c be a constant
such that g(n) < cf (n) for large enough n. Then f (n) ≤ f (n) + g(n) ≤ (1 + c)f (n)
for large enough n.
(e) Never true: If f (n) = Ω(g(n)) then there exists positive constant cΩ and nΩ
such that for all n > nΩ , cg(n) ≤ f (n). But if f (n) = o(g(n)), then for any
positive constant c, there exists no (c) such that for all n > no (c), f (n) < cg(n). If
f (n) = Ω(g(n)) and f (n) = o(g(n)), we would have that for n > max(nΩ , no (cΩ )) it
should be that f (n) < cΩ g(n) ≤ f (n) which cannot be.
(a) Insertion sort takes O(k 2 ) time per k-element list. Therefore, sorting n/k such k-
element lists, takes O(k 2 n/k) = O(nk) time.
(b) Merging requires O(n) work at each level, since we are still working on n elements,
even if they are partitioned among sublists. The number of levels, starting with n/k
k-element lists and finishing with 1 n-element list, is dlg(n/k)e. Therefore, the total
running time for the merging is O(n lg(n/k)).
(c) The largest asymptotic value of k, for which the modified algorithm has the same
asymptotic running time as standard merge sort, is k = O(lg n). The combined
running time is O(n lg n + n lg n − n lg lg n), which is O(n lg n). For larger values of
k the term nk would be larger than O(n lg n).
(d) In practice, k should be chosen such that it minimizes the running time of the
combined algorithm, which is cnk + dn lg(n/k), where c and d are some positive
constants that determine the actual running time of the two phases. The value
k = d/c minimizes the above expression, and thus the best choice of k does not
depend on n, but rather on the ratio between the constants d and c.
Handout 8: Problem Set 1 Solutions 3
textbook).
(e) T (n) = T (n/2 + 5) + n2 =⇒ T (n) = Θ(n2 ) It is reasonable to guess that T (n) has
the same solution of S(n) = S(n/2) + n2 as the difference between n/2 and n/2 + 5
becomes negligible for n → ∞. By case 3 of the Master Theorem, we have that
S(n) = Θ(n2 ).
Thus we guess T (n) = Ω(n2 ) and we prove it by substitution. Assume that T (m) ≥
cm2 for an appropriate constant c and for all m < n. Then we have that