Some Document I Need To Upload
Some Document I Need To Upload
Some Document I Need To Upload
Θ(nlog2 7 ) ∼ Θ(n2.807 ).
This is a much better result over the simple algorithms from exercises 9 and 10.
1
CSE103: TD 3 Solutions
Time: 7:25 - 8:55
Solution.
1. True. Proof: Set c = 1 and consider the function ϕ(n) = n3 −n2 −10n−6.
Notice that ϕ′′ (n) = 6n − 2 ≥ 0 for n ≥ 1/3 and ϕ′ (n) = 3n2 − 2n − 10
is positive for n = 3 (we have ϕ′ (3) = 11 > 0). Then ϕ′ (n) is positive
for all n ≥ 3 and ϕ(4) = 2 > 0, so ∀n > 4, ϕ(n) > 0. We deduce that
∀n > m = 4, n2 + 10n + 6 < n3 , so n2 + 10n + 6 = O(n3 ).
5. True. Proof: Apply the logarithm to the definition of f to get log f (n) =
log n + O(n) log 2. Since log n = O(n) we
# conclude
$ that log f (n) = O(n).
Example: n27n+3 then log f (n) = log n27n+3 = log n + (7n + 3) log 2,
which is indeed O(n)
6. False. Proof: n log n is Ω(n) and is not Θ(n). Suppose there existed c and
n log n
m satisfying 2 2 < c2n , for all n > m. Then we would have, for all
n > m, the inequality log 2n−2 n < log2 c, which would mean that log 2n−2 n
is bounded from above by a constant, which is trivially false because it
asymptotically tends to +∞, yielding a contradiction.
2
Solution.
Let us examine when the general term is maximal: consider f (n) = k + 2nk ,
such that f ′ (k) = 1 − kn2−k−1 = 0 when kn = 2k+1 , the solution of
which is (unfortunately) a W -function. Thus we use a looser worst-case
k
estimation, 2k · 2n/2 < 2n+1 . As such, we get T (n) = O(2n log n) .
3
Solution.
4
1. def weirdSort(l):
O(1) if O(1): -> O(1) elif O(1): -> O(1) -> r1 = T(2n/3), O(n)
-> r2 = O(n), T(2n/3) r = T(2n/3), O(n) O(1)
5
Solution.
1. With our current cost model, let us analyse the resulting complexity:
def mul(x, y, s=0):
-> if O(1):
-----> O(n)
-> if O(1):
-----> O(n)
-> T(n-1)
The Master Theorem is inapplicable. The algorithm follows the recurrence
T (n) = T (n − 1) + Θ(n). By the recursion tree method, we obtain the
(n
upper bound k=1 k = 12 n(n + 1), which is O(n2 ) .
2. In this case the code only performs the addition of 0 and n, which is simply
O(1), independent of n.
3. why not write the explicit recursion to check Still O(n2 ) because even-
tually s and y cannot be added with less than O(n) operations.
4. If x = 0 then the above algorithm results in an infinite recursion, which
crashes once the system recursion limit is reached. If y = 0, unless x =
0 (for which it fails as described above), the algorithm correctly gives 0
because s and y never change from the value 0.