Algorithms, Fall 2012 Homework #3 Sample Solution November 15, 2012
Transcription
Algorithms, Fall 2012 Homework #3 Sample Solution November 15, 2012
Algorithms, Fall 2012 Homework #3 Sample Solution November 15, 2012 16.2-5 Let X = {x1 , x2 , ..., x3 }. First we sort X by the value of x. Second, pick the smallest value xi from X, and generate an unit-length interval [xi , xi +1] to the answer collection. Third, remove all points in X ∩ [xi , xi + 1]. Repeat the second and third steps until X = {}. Observe that each point can be removed from X once, so the time complexity is O(n log n) (due to the sorting step). Greedy choice property: We claim the interval [x, x + 1] is the interval in one of optimal solutions (where x is the smallest value in X in some step). Assume I ∗ is one of the optimal solutions, and an interval [x − α, x − α + 1] in I ∗ covers x, then we can get another optimal solution by replacing [x − α, x − α + 1] with [x, x + 1]. This is because there is no point in [x − α, x]. Optimal substructure: Consider the optimal solution I ∗ , and we remove one interval i, and I\i is the optimal solution of probelm of X\{x|x ∈ i} ,which is subproblem of X. 16.2-6 First we make a list C[1..n], where n is the number of items. Let C[i] = vi /wi , vi is the value of item xi and wi is the weight of item xi . We can find the median m of C by O(n) algorithms of Sec.9.2 and 9.3. We divide all items into three class: H = {xi |C[i] > m}, F P maximum loading W is smaller than P = {xi |C[i] = m}, and L = {xi |C[i] < m}. If the i , we pick all the items in H xi ∈H∪F wP xi ∈H wi , then we do recursively on H. If W < and pick some items from F until the bag is full. If W > xi ∈H∪F wi , wePpick all the items from H and F , and do recursively on L with maximum loading W − xi ∈H∪F wi . Because we reduce the half of problem size for each recursion, T (N ) = T ( N2 ) + O(N ), the time complexity of this algorithm is O(N ). 16.2-7 Sort A and B by decreasing order can solve it. Greedy choice property: We claim ab00 is a part of multiplication sequence in the optimal solution, where a0 is the largest number of A and b0 is the largest number of B. To prove that ab00 is a part of the optimal solution, we argue by way of contradiction. Suppose ab00 is not a part of multiplication sequence in the optimum solution, then there exist b b b ab0i , i 6= 0 and abj0 , j 6= 0 in the optimal solution. Compare ab00 ai j with a0j a0j : we have b b log(ab00 ai j ) − log(abi 0 a0j ) = (b0 − bj )(log a0 − log ai ) > 0, which is a contradiction. Optimal substructure: Remove ab0i from multiplication sequence in optimal solution. This sequence is the optimal solution for two sets A\a0 and B\b0 which is a subproblem of the original problem. 16.4-2 1. S is finite. 2. Prove hereditary property. 1 B is a linearly independent set. A ⊆ B, then A is linearly indepentdent too. A ∈ I, 3. Prove exchange property For |A| < |B|. We have rank(B) = |B| > rank(A) = |A|. (Both of them are linearly independent). There must exists an element x ∈ B − A such that A ∪ {x} is still linearly independent. We show the existence of x. Proof by contradiction: If x doesn’t exists, rank(A) is the possible maximum value for B. That is, rank(B) ≤ rank(A) Contradiction. ( rank(B) > rank(A) ) Therefore, we have a x such that A ∪ {x} is still linearly independent A ∪ {x} ∈ I 16.4-4 1. S is a finite set by the definition of the problem. 2.Prove hereditary property. A ⊆ B and B ∈ I, Because A is a subset of B, |A ∩ Si | ≤ |B ∩ Si | ≤ 1. A ∈ I. 3.Prove exchange property For |A| < |B|, we could find a Si such that |A ∩ Si | = 0 and |B ∩ Si | = 1. Proof by contradiction: If no such Si exists, then |A ∩ Si | = 1 for all i such that |B ∩ Si | = 1. → |B| ≤ |A| Contradiction (|B| > |A|) Because S1 , ..., Sk are disjoint subsets. Adding the element x in ( B ∩ Si ) to A will not affect |A ∩ Sj | for i 6= j. (x ∈ B) Because |A ∩ Si | = 0, x ∈ B − A. |(A ∪ {x}) ∩ Si | = 1 after adding x to A. A ∪ {x} ∈ I. 16-1 a. solve(n) coinV alue[4] = (25, 10, 5, 1) // from high value to low value coinU sed[4] = (0, 0, 0, 0) For i = 0 to 3 coinU sed[i] = n/coinV alue[i]; n− = coinU sed[i] ∗ coinV alue[i]; We notice that the solution generated by the algorithm coinU sed[4] = (a1 , a2 , a3 , a4 ) satisfy that a1 ∗ 25 + a2 ∗ 10 + a3 ∗ 5 + a4 = n and a2 ≤ 2, a3 ≤ 1 and a4 ≤ 4. Proof by contradiction If we have an optimal answer A that (a1 , a2 , a3 , a4 ) doesn’t satisfy a2 ≤ 2, a3 ≤ 1 2 and a4 ≤ 4. Case 1: a2 > 2 We could replace every 50 cents by 2 quarters. (replace 5 coins by 2 coins). Then discuss (1) a2 mod 5 = 3 We replace 30 cents by 1 quarter and 1 nickel. (replace 3 coins by 2 coins). (2) a2 mod 5 = 4 We replace 40 cents by 1 quarter, 1 dimes and 1 nickel. (replace 4 coins by 4 coins). By this way, we get a solution B that is better than A. (B is the result of our algorithm) Case 2: a3 > 1 We could replace every 10 cents by 1 dime. (replace 2 coins by 1 coin). By this way, we get a solution B that is better than A. (B is the result of our algorithm) Case 3: a4 > 4 We could replace every 5 cents by 1 dime. (replace 5 coins by 1 coin). By this way, we get a solution B that is better than A. (B is the result of our algorithm) We could get a solution B, which is generated by the algorithm. And B is better than A in all cases. Conlcusion : There cannot be such optimal solution A. The algorithm generate optimal solution. b. We use the algorithm similar as part a. (Just extend the array to size k) Notice that the solution generated by the algorithm coinU sed[] = (a1 , ..., ak ) satisfy that a1 ∗ ck + a2 ∗ ck−1 + ... + ak = n and every ai ≤ c − 1 for i ≥ 2. Proof by contradiction If we have an optimal answer A that (a1 , ..., ak ) doesn’t satisfy ai ≤ c − 1 for i ≥ 2. Then we replace the every c coins of value ci by one ci+1 coin. (Replace c coins by 1 coin) 3 By this way, we get a solution B that is better than A. (B is the result of our algorithm) We could get a solution B, which is generated by the algorithm. And B is better than A in all cases. Conlcusion : There cannot be such optimal solution A. The algorithm generate optimal solution. c. coinV alue[3] = (12, 7, 1) n = 14 The greedy algorithm gives 12cents and 2 pennies. However, the answer is two 7cents. d. We solve this by Dynamic Programming The meaning of minCoinU sed is the minimum coins that we have to use for n cents. solve(n) coinV alue[] = (c1 , c2 , ....) // arbitrary order minCoinU sed[] = (∞) // final answer, and initialize to infinity minCoinU sed[0] = 0 //initialization For i = 0 to k For j = ci to n minCoinU sed[j] = min(minCoinU sed[j], minCoinU sed[j − ci ] + 1) Obviously, the algorithm runs in O(nk) 7 Since ci = i for i = 2k and ci = 1 for i 6= 2k . We have n X log2 n ci ≤ · · · ≤ n + i=1 In conclusion, the amortized cost is X 2k ≤ · · · ≤ 3n. k=1 3n n = 3. 8 Consider an array as the data structure of S. For a new element x and an array S, the operation INSERT(S, x) just insert x in the end of A. This makes the cost of each INSERT(S, x) is O(1). Here we assume O(1) = k. The operation DELETE-LARGERHALF(S) first finds the median Me (S) of S by applying the SELECT algorithm of Sec.9.3, then assign new S 0 by leaving the elements that are smaller than Me (S) in the original S. The details of the SELECT algorithm will not be state here, but note that its running time is O(|S|) in the worst case. Furthermore, the cost of deleting larger half elements in S is also O(|S|). By assumption there are total m operations.PEach DELETE-LARGER-HALF(S) costs 2 O(|S|) = O(m), so total cost seems to be O( m k=1 k) = O(m ) in the worst case. In fact, 4 a careful analysis can show that total cost is only O(m). Here we prove this claim by the potential method. Let ci be the actual cost, and cˆi be the amortized cost. Let Ai be the array after the ith operation. Denote the size of Ai as |Ai |. Define a potential function of Ai as Φ(Ai ) = 2k|Ai | (where k is defined in above). Then we have cˆi = ci + Φ(Ai ) − Φ(Ai−1 ). Insert: Previous setting makes ci = k = O(1) and cˆi = ci + Φ(Ai ) − Φ(Ai−1 ) = k + 2k(|Ai−1 | + 1) − 2k|Ai−1 | = · · · = O(1). Delete: Previous setting makes ci = k|Ai−1 | = O(|Ai−1 |) and cˆi = ci + Φ(Ai ) − Φ(Ai−1 ) = k|Ai−1 | + 2k(|Ai−1 |/2) − 2k|Ai−1 | = · · · = O(1). These P analysis shows that the amortized cost of each operation is O(1), so the total cost is m i=1 O(1) = O(m). Finally, it takes O(|S|) = O(m) time to output all elements in S directly. 5