Sunteți pe pagina 1din 3

Introduction to Algorithms, Spring 2010 Homework 3 solutions

9.3-8
1: function FIND -MEDIAN (X , Y, n) 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:

k = n/2 if k 2 then Merge X and Y , and return the median of the merged array. if n is odd then if X [k] == Y [k] then Return X [k] else if X [k] < Y [k] then Return FIND -MEDIAN(X [k..n], Y [1..k], k) else Return FIND -MEDIAN(X [1..k], Y [k..n], k) else if X [k] == Y [k + 1] then Return X [k] else if X [k] < Y [k + 1] then Return FIND -MEDIAN(X [k + 1..n], Y [1..k], k) else Return FIND -MEDIAN(X [1..k], Y [k + 1..n], k) This algorithm is similar to binary search. The time complexity is T (n) = T (n/2) + O(1) = T (n) = O(lg n).

9.3-9
We can use the linear time algorithm to nd the median of the y-coordinates of the wells. If n is even, place the main pipeline on anywhere between the lower median and upper median. If n is odd, place the main pipeline on the median.

9-2
a. Suppose x m is the (lower) median, where m = (n + 1)/2. Verify wi =
x i <x m

1 n 1 n

(n + 1) 2 n 2

<

1 2 1 2

, and .

wi =
x i >x m

(n + 1)

b. First, sort the n elements into increasing order by their weights. Then scan from the smallest element and accumulate the weight. The last element, whose weight caused the sum greater than or equal to 1/2, is the weighted median. We can sort the n elements in O(n lg n) time by using merge sort or heap sort. Scanning and accumulate the weights take O(n) time. Hence the total running time is O(n lg n). c. 1: function WEIGHTED -MEDIAN(X ) 2: // X is the array of the n elements. 3: if leng th(X ) 2 then 4: Return the median directly. 5: Find the median x k of X. Let WL = x i <x k w i and WH = 6: if WL < 1/2 and WH 1/2 then 7: Return x k . 8: else if WL > 1/2 then 9: w k = w k + WH 10: Return WEIGHTED -MEDIAN(X[1..k]). 11: else 12: w k = w k + WL 13: Return WEIGHTED -MEDIAN(X[k..n]).

x i >x k

wi .

The recurrence of the running time is T (n) = T (n/2 + 1) + (n) since the linear-time median algorithm is used. By master method, we have T (n) = (n).

11.3-4
h(61) = 700 h(62) = 318 h(63) = 936 h(64) = 554 h(65) = 172

15.2-1
(A1 A2 )((A3 A4 )(A5 A6 )) 2

15.2-3
Guess P(n)
n1 k=1 2n . 4

For n 2, we have P(n) = 1

2n . 4

For n > 2, we have P(n) =


22n1 4

P(k)P(n k) = P(1)P(n 1) + + P(n 1)P(1) 2P(1)P(n 1) =

2n . 4

15.3-6
For c1 , . . . , cn = 0, let the best sequence be 1 = x 1 , . . . , x m = n, i.e., we trade currency x 1 for x 2 , x 2 for x 3 , and so on. For any i [m], x 1 , . . . , x i must be the best sequence for exchanging currency 1 to x i . Assume the contrary, there is a better sequence x 1 , y1 , . . . , y j , x i to exchange currency 1 to x i . This implies x 1 , y1 , . . . , y j , x i , x i+1 , . . . , x m is a better sequence for the original problem, since ck = 0 for k [n]. A contradiction. Similarly, we can prove x i , x i+1 , . . . , x m is the best sequence to exchange currency x i into n. The best sequence exhibits optimal substructure. Let ri, j = 1 for i, j [n]. Dene ck as the following. 1, k = 1 1, k is even ck = 0, otherwise

The length of best sequence must be odd and greater than 2, but we cannot break such sequence into two subsequences of odd length. In other words, at least one subsequence is not best. In this case, the best sequence does not exhibit optimal substructure.

15-1
Let the weight of edge (u, v) be d(u, v). Let the weight of the longest weighted simple path from s to u be w(v). We have w(v) = 0, v=s max (w(u) + d(u, v)), v = s

u:(u,v)E

The subproblem graph looks like G and we can compute w in topological order of G in O(|V | + |E|) time.

15-2
On input s = x 1 . . . x n , let sR = x n . . . x 1 be the reverse of s. Compute the longest common subsequence of s and sR , then output the result. The running time is (n2 ).

S-ar putea să vă placă și