Beruflich Dokumente
Kultur Dokumente
Problem Set 1
Solutions
1. For each of the following statements, decide whether it is always true, never true, or sometimes
true for asymptotically nonnegative functions f and g. If it is always true or never true, explain
why. If it is sometimes true, give one example when it is true, and one example when it is false.
(a) f (n) = O(f (n)2 )
Solution. Sometimes true. For f (n) = n it is true. For f (n) = 1/n it is not true.
f (n)
(d) f (n) = Ω(g(n)) and limn→∞ g(n)
=0
2. Tutorial 2, Q1
Solve the following recurrence relations using the Master Theorem. Express your answers using
the Θ-notation and specify which case of the theorem applies. If the theorem cannot be used,
explain why not.
(a) T (n) = 3T (n/2) + n2
Solution. T (n) = Θ(n2 ). Use the Master Theorem for the aymptotic tight bound, Case 1.
3. Tutorial 1, Q3
(a) Prove 2n ≤ n! for n ≥ 4 by induction. A formal induction proof should consist of a base
case and an induction step.
Solution. The base case is true, because 24 = 16 and 4! = 24. Suppose that the inequality is true
for n. For the n + 1 case, 2n+1 = 2 · 2n ≤ 2 · n! ≤ (n + 1) · n! = (n + 1)!, and this verifies the
induction step.
(b) Does the result above imply 2n = O(n!)? Why or why not?
Hint. Use the definition of O-notation.
Solution. Yes. Set N = 4 and C = 1. Apply the definition of O-notation.
C1 = 0, C2 = 1, C3 = 2, and C4 = 2.
Solution. Copy the two input arrays into a new array A of size m + n. Sort A and scan A
sequentially to remove all duplicates. Copying and scanning takes time Θ(m + n). Sorting takes
time Θ((m + n) lg(m + n)). So the total runnging time is Θ((m + n) lg(m + n)).
Hence, A[m] is the globally maximum element. It is also the unique locally maximum element
surrounded by smaller elements A[m − 1] and A[m + 1].
(a) Give an algorithm that computes the maximum element of a unimodal input array A[1 : n] in
O(lg n) time.
(b) Show that your algorithm runs in O(lg n) time.
Solution. By the definition of unimodal arrays, for each 1 ≤ i < n, either A[i] < A[i + 1] or
A[i] > A[i + 1]. The main idea is to distinguish these two cases:
• If A[i] < A[i + 1], then the maximum ele ment of A[1..n] occurs in A[i + 1..n].
• If A[i] > A[i + 1], then the maximum element of A[1..n] oc curs in A[1..i].
This leads to the following algorithm, which is very similar to binary search:
1: a = 1, b = n.
2: while a < b do
3: mid = (a + b)/2.
4: if A[mid] < A[mid + 1] then
5: a = mid + 1.
6: if A[mid] > A[mid + 1] then
7: b = mid.
8: return A[a]
The running time of the algorithm satisfies the recurrence relation T (n) = T (n/2) + c, which has
the solution T (n) = Θ(lg n).
7. A polygon is convex if all of its internal angles are less than 180◦ . The figure below shows an
example. We represent a convex polygon as an array V [1 : n], in which each element represents
a vertex of the polygon in the form of a pair of coordinates (x, y). The first element of the
array, V [1], is the vertex with the minimum x coordinate. The vertices V [1 : n] are sorted in the
counterclockwise order, as shown in the figure below. We assume that the x coordinates of the
vertices are all distinct, and so are the y coordinates of the vertices.
V[5]
V[6]
V[4]
V[1]
V[2]
V[3]
Give an algorithm to find the vertex with the maximum y coordinate in O(lg n) time.
Hint. Use the solution to Problem 6.