Exercise Lover

March 30, 2007

Exercises 2.3-7

Filed under: 2.3 — yuhanlyu @ 6:42 am

Solution 1: Sort the set first. For any element y in S, use binary search to determine whether there is one element z in S-y such that y + z = x. Since the binary search can be done Θ(lg n), the overall running time is Θ(n lgn).

Solution 2: Sort the set first. Let A_1 \dots A_n be the sorted result. If A_1 + A_n > x, then the solution must be in A_1 \dots A_{n-1}. If A_1 + A_n < x, then the solution must be in A_2 \dots A_n. If A_1 + A_n = x, we are done. So we can solve the problem recursively. The overall running time is Θ(n lgn).

Exercises 2.3-6

Filed under: 2.3 — yuhanlyu @ 6:37 am

No. Although the comparisons are Θ(n lgn), but the data movements are still \Theta(n^2).

Exercises 2.3-5

Filed under: 2.3 — yuhanlyu @ 6:35 am

Worst case running time.
T(1) = 1
T(n) = T(n/2) + 1
T(n) = lg n.

Exercises 2.3-4

Filed under: 2.3 — yuhanlyu @ 6:34 am

Worst case running time.
T(1) = 1
T(n) = T(n-1) + n
T(n) = \Theta(n^2)

Exercises 2.3-3

Filed under: 2.3 — yuhanlyu @ 6:32 am

Base case: n = 2, T(n) = 2.
Induction step: Assume that T(n) = n lg n for all n = 2^c, c < k. Then for n = 2^k. T(n) = 22^{k-1}(k-1) + 2^k = k2^k = n lg n.

Exercises 2.3-2

Filed under: 2.3 — yuhanlyu @ 6:28 am

Eliminate step 8 and 9.
For step 12~17, add the condition that i \leq n_1 and j \leq n_2.

Exercises 2.3-1

Filed under: 2.3 — yuhanlyu @ 4:52 am

Blog at WordPress.com.