Sie sind auf Seite 1von 3

CS3230 Design & Analysis of Algorithms

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.

(b) f (n) + g(n) = Θ (max (f (n), g(n)))


Solution. Always true. max(f (n), g(n)) ≤ f (n) + g(n) ≤ 2 max(f (n), g(n)).

(c) f (n) + g(n) = Θ(f (n)), where g(n) = O(f (n))


Solution. Always true. Since g(n) = O(f (n)), there exist positive constants C and N such that
g(n) ≤ Cf (n) for all n ≥ N . Then f (n) ≤ f (n) + g(n) ≤ (1 + C)f (n) for n ≥ N .

f (n)
(d) f (n) = Ω(g(n)) and limn→∞ g(n)
=0

Solution. Never true. If limn→∞ fg(n)(n)


= 0, then for any given ε > 0, there exists N such that
for all n ≥ N , f (n)/g(n) < ε, in other words, f (n) < εg(n). This contradicts the definition of
f (n) = Ω(g(n)).

(e) f (n) 6= O(g(n)) and g(n) 6= O(f (n))


Solution. Sometimes true. For f (n) = 1 and g(n) = |n sin(n)|, it is true. For any f (n) =
O(g(n)), e.g., f (n) = g(n) = 1, it is not true.

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.

(b) T (n) = 4T (n/2) + n2


Solution. T (n) = Θ(n2 lg n). Use the Master Theorem for the aymptotic tight bound, Case 2.

(c) T (n) = 2n T (n/2) + nn


Solution. Master Theorem cannot be applied because a is not a constant.

(d) T (n) = 16T (n/4) + n


Solution. T (n) = Θ(n2 ). Use the Master Theorem for the aymptotic tight bound, Case 3.

2010-11 Semester 2, David Hsu


CS3230 Design & Analysis of Algorithms

(e) T (n) = 4T (n/3) + n lg n


Solution. T (n) = Θ(nlog3 4 ). Use the Master Theorem for the aymptotic upper bound and the
asymptotic lower bound, Case 3.

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.

4. J&S, 25, 26, and 27, pp. 67

Solution. If n is even, exp2 is called recursively, followed by 1 multiplication in line 6. If n is


odd, exp2 is called recursively, followed by 2 multiplication in line 6 and line 10.

C1 = 0, C2 = 1, C3 = 2, and C4 = 2.

Cn = lg n if n = 2k for some integer k ≥ 0.

5. J&S, 6.13, pp. 269


Give an algorithm that computes the union of two sets of integers. The input consists of two
integer arrays of size m and n, respectively, and each array represents a set. The output is a new
array representing the union of the two input sets. Note that each element of a set appears exactly
once. Make your algorithm as fast as possible and give a tight asymptotic upper bound on its
worst-case running time.

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)).

6. An array A[1 : n] is unimodal if it consists of an increasing sequence followed by a decreasing


sequence. More precisely, there is an index m ∈ {1, 2, . . . , n} such that

• A[i] < A[i + 1] for all 1 ≤ i < m, and


• A[i] > A[i + 1] for all m ≤ i < 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].

2010-11 Semester 2, David Hsu


CS3230 Design & Analysis of Algorithms

(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.

2010-11 Semester 2, David Hsu

Das könnte Ihnen auch gefallen