






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Dynamic Programming (Solutions)
Solution: We proceed with this solution as outlined in the notes. We define B[k, w] to be the optimal solution that can be obtained using only the first k items, with maximum allowed total weight of w. Here k ranges from 1 to 4, while w ranges from 0 to 10. We also define B[0, w] = 0 for all w. Then, as we did in class, we have to fill in the table, one row at a time, to find the optimal solution, using the recurrence type of equation that can be found in the class notes. This table will end up having these values: 0 1 2 3 4 5 6 7 8 9 10 1 0 0 0 0 0 0 0 25 25 25 25 2 0 0 15 15 15 15 15 25 25 40 40 3 0 0 15 20 20 35 35 35 35 40 45 4 0 0 15 20 20 35 36 36 51 56 56 So the maximum benefit obtainable is 56. Since this example is small, we can easily figure out that we want to take items 3 and 4 to obtain this benefit. Otherwise, as also mentioned in class, we can “trace back” through the table to find this out. (b) For comparison, what is the solution to the corresponding Fractional Knapsack problem with the same value of Wmax (use the greedy method we discussed for this “Fractional” version)?
Solution: Recall that we compute the value indices and take items in order of the value indices to fill up the knapsack. So I have added another line to the table below for the value indices. i 1 2 3 4 bi 25 15 20 36 wi 7 2 3 6 vi 3 47 7 12 6 23 6 So we select items in the order 2,3,4,1. Doing so will lead us to the Fractional Knapsack solution consisting of items 2, 3, and 56 of item 4, for a total benefit of 15 + 20 + 56 (36) = 65.
Here’s the table you would get for the { 0 , 1 } version of this problem: 1 2 3 4 5 6 7 8 9 10 11 1 0 0 0 14 14 14 14 14 14 14 14 2 0 0 12 14 14 14 26 26 26 26 26 3 0 0 12 14 14 14 26 26 26 26 27 4 0 0 12 14 14 14 26 26 26 32 34 5 0 0 16 16 16 28 30 30 30 42 42 From this table, you can see that the maximum benefit for the { 0 , 1 } Knapsack Problem for this set of items is 42. What is the set of items that achieves this benefit? You can find that by tracing backwards through the table. Since B[5, 11] > B[4, 11] we see that item 5 must be used in the solution. Then we want to compare B[4, 8] and B[3, 8]. Since they are the same, we conclude that item 4 is not used in the best solution. Continuing in this way, we can determine that the best solution uses items 5, 2, and 1.
For the Fractional case, we again compute the value indices. i 1 2 3 4 5 bi 14 12 15 20 16 wi 4 3 8 7 3 vi (^3 23 4 1 78 2 67 5 ) So we select items (greedily) in the order 5,2,1,4,3. This will give us the greedy solution for the Fractional case consisting of items 5,2,1 and 17 of item 4, for a total benefit of 42 + 17 (20) = 44 67.
Solution: Here I give quite a detailed solution to this problem. Note that I don’t necessarily expect you to provide pseudo-code for your proposed solution. Your description should be sufficiently detailed to to give some argument as to why your solution is correct.
Let S[n] denote the minimum number of stamps needed to make postage for n pence. We clearly then have
S[0] = 0 S[1] = 1 S[2] = 2 S[3] = 3 S[4] = 4
to make np postage. So, for example P [7] = (0, 1 , 0), P [10] = (0, 0 , 1), and P [25] = (1, 2 , 1). (As I said earlier, P [n] isn’t really uniquely defined, but that’s not important for us. We just want some way, using the minimum number of stamps, to get n pence.) The calculation for S[n] doesn’t change. All we do is determine which denomination of postage we use and add it to the appropriate value of P [n − 1], P [n − 7], or P [n − 10]. The modified pseudo-code is given below. The addition of vectors in lines 9, 11, and 13 is done component-wise in the usual way, e.g. (3, 4 , 1) + (0, 1 , 0) = (3, 5 , 1). The running time is still O(n).
Stamps(n) We assume here that n ≥ 0. This procedure will determine the minimum number of stamps of values 1p, 7p, and 10p to make up total postage of n pence. We also return a vector, P [n] which will tell us how many of each stamp is necessary. Base cases 1 Set up the array S starting as above (with the values S[i] for i = 0,... , 10). 2 Also set up an array P [i] for i = 0,... , 10. That is P [0] = (0, 0 , 0), P [1] = (1, 0 , 0), P [2] = (2, 0 , 0),... , P [7] = (0, 1 , 0), etc.
3 if n ≤ 10 then 4 return S[n] and P [n] 5 else 6 for j ← 11 to n do 7 S[j] = 1 + min{S[j − 1], S[j − 7], S[j − 10]} Now set P [j] based on the value of S[j]. In other words, which stamp did we use? 8 if S[j] == (1 + S[j − 1]) then 9 P [j] ← P [j − 1] + (1, 0 , 0) 10 elseif S[j] == (1 + S[j − 7]) then 11 P [j] ← P [j − 7] + (0, 1 , 0) 12 else 13 P [j] ← P [j − 10] + (0, 0 , 1) 14 return S[n] and P [n]
American coins come in five different values, namely 50-cent, 25-cent, 10- cent, 5-cent, and 1-cent coins. Therefore, if we wanted change for 11 cents (or 11¢), we can do this with one 10¢ coin and one 1¢ coin, or two 5¢ coins and one 1¢, or one 5¢ and 6 1¢ coins, or with eleven 1¢ coins. So there are four ways to give change for 11¢. How many ways are there to make change for N ¢? (And how can you compute this number efficiently?)
We consider the coins in order 1, 5, 10, 25, 50. We compute the number of ways of making change, first using only 1¢ coins, then using both 1¢
and 5¢ coins, and so forth. Doing the computations in this order, we’re using the previously computed information to save us some work (in the form of repeated computations). So if you want, we can consider defining a 2-dimensional array (table) of the form A[i, n] for i = 1,... , 5 and n = 1... , N. The index i = 3, for example, tells us that we’re using the first three coins to make change (i.e. the 1¢, 5¢, and 10¢ coins). Obviously the value n is the amount for which we’re making change. We want to find A[5, N ]. First of all, we note that A[1, n] = 1 for all n ≤ N. Why? There’s only one way to make change for n¢using only pennies. How many ways are there to make change using 1¢ and 5¢ coins? We can find this using the following bit of pseudo-code.
1 for j ← 0 to N Do 2 A[2, j] = bj/ 5 c + 1
Verify that this number is correct? (Hint: The +1 comes from the fact that you can use no 5¢ coins.) Then we can find the values of A[3, n] in this fashion
1 for j ← 0 to N Do 2 A[3, j] = A[2, j] 3 for k ← 10 to N by 10 Do (Increment counter by 10.) 4 if (j − k >= 0) Then 5 A[3, j] = A[3, j] + A[2, j − k]
Because this pseudo-code has two “for” loops in it, you can see that it runs in time O(N 2 ). You should also verify that it correctly computes the number of ways of giving change using 1¢, 5¢ and 10¢ coins. Adding on 25¢ and 50¢ coins is similar to the above pseudo-code, we just increment the counter in the second “for” loop by 25 or 50 as appropriate (and use the appropriate index i in the values of A[i, n]). Overall, this will let us compute the value of A[5, N ], the value we want, in time O(N 2 ).
The current class of students at Sandhurst has an annual picnic (ok, maybe they don’t really do this, but for the purposes of this question they do...). This year it happens that the picnic has fallen on a rainy day and the ranking officer decides to postpone the picnic and must notify everyone by phone. Here is the mechanism for doing this. Each person except for the ranking officer reports to a unique superior officer. Thus, the reporting hierarchy can be described as a tree T , rooted at the ranking officer, in which each other node v has a parent node u equal to his or her superior officer. Conversely, we can call v a direct subordinate of u. See the picture below, where A is the ranking officer, B and D are direct subordinates of A, and C is a direct subordinate of B.
where r(w) = rmax (i.e. w has the maximum r value amongst all of v’s children). Then v needs at least 1 + rmax rounds to inform w and all of the descendants of w. (Why?) The recursive means to compute these r values is as follows: Assign r = 0 to all of the leaves of the tree. For a node v where all of the r values of its children have been computed already, we can find that
r(v) = max{#children of v, 1 + max{r(c) where c is a child of v}}.
Think about why this is true. (Do you agree with this? Am I correct? Don’t take my word for it. Convince yourself.)
I will let you think about how you could get a schedule that would match the smallest number of rounds needed.
j sj fj vj p(j) 1 0 6 2 0 2 2 10 4 0 3 9 15 6 1 4 7 18 7 1
Solve this instance of the weighted interval scheduling problem, i.e. find a set of (non-conflicting) intervals with maximum total weight.
Solution: Note that the intervals are already sorted by their finish time, and I have added the value p(j) for each interval. Recall that the value p(j) is the largest index of an interval which is compatible with interval j, i.e. interval p(j) could be scheduled together with interval j, but not interval p(j)+1 as that would conflict with interval j. (Of course, p(j) = 0 if there is no such interval.) Now recall that Opt(j) was defined to be the best solution that is obtain- able using intervals 1,... , j, where we define Opt(0) = 0. Then we have that Opt(j) = max {Opt(j − 1), vj + Opt(p(j))}. Therefore, we have
Opt(0) = 0 Opt(1) = max {Opt(0), v 1 + Opt(p(1))} = 2 Opt(2) = max {Opt(1), v 2 + Opt(p(2))} = max{ 2 , 4 + 0} = 4 Opt(3) = max {Opt(2), v 3 + Opt(p(3))} = max{ 4 , 6 + 2} = 8 Opt(4) = max {Opt(3), v 4 + Opt(p(4))} = max{ 8 , 7 + 2} = 9
So there is a schedule that gives us a weight (or value) of 9. It’s easy enough to see, in this case, that we schedule intervals 1 and 4 to get this value.
In general, if you want to reconstruct the actual schedule that will achieve the maximum value, then you start with the value of Opt(n) (for n inter- vals) and determine if Opt(n) = Opt(n − 1) or if Opt(n) = vn + Opt(p(n)). In the first case, interval n isn’t used and then we proceed to the value Opt(n − 1) and do the same to continue finding the schedule. In the second case, interval n is scheduled, and then we proceed to examine task p(n) (and the value of Opt(p(n))) to determine the next (i.e. previous) interval to schedule.
j sj fj vj p(j) 1 1 4 4 0 2 2 6 2 0 3 3 9 1 0 4 5 10 6 1 5 7 14 3 2 6 12 16 1 4 7 9 18 5 3 8 15 20 3 5
Solution: Again, I have added in the values of p(j) for each interval in the table, as the tasks are already sorted by their finishing times. In this case we have these values:
Opt(0) = 0 Opt(1) = max {Opt(0), v 1 + Opt(p(1))} = max{ 0 , 4 + 0} = 4 Opt(2) = max {Opt(1), v 2 + Opt(p(2))} = max{ 4 , 2 + 0} = 4 Opt(3) = max {Opt(2), v 3 + Opt(p(3))} = max{ 4 , 1 + 0} = 4 Opt(4) = max {Opt(3), v 4 + Opt(p(4))} = max{ 4 , 6 + 4} = 10 Opt(5) = max {Opt(4), v 5 + Opt(p(5))} = max{ 10 , 3 + 4} = 10 Opt(6) = max {Opt(5), v 6 + Opt(p(6))} = max{ 10 , 1 + 10} = 11 Opt(7) = max {Opt(6), v 7 + Opt(p(7))} = max{ 11 , 5 + 4} = 11 Opt(8) = max {Opt(7), v 8 + Opt(p(8))} = max{ 11 , 3 + 10} = 13
So the maximum value obtainable is 13, which comes from scheduling intervals 8, 4, and 1.
Hint: Define L(j) to be the length of the longest increasing subsequence that ends at (and includes) position j. How can you find L(j) in terms of the values L(i) for i < j? Once you know the L(j) values, what’s the answer to the original problem? And how long does it take to compute all of the L(j) values?
Solution: As suggested above, we define L(j) to be the length of longest increasing subsequence that ends at (and includes) position j. If we are able to compute these L(j) values, then the solution to our problem is equal to L = max{L(1), L(2),... , L(n)}. Why? Because the longest increasing subsequence must end at (and include) one of the positions 1 , 2 ,... , n. We note that L(1) = 1, since the single value A 1 is an “increasing” sub- sequence. Also, L(2) = 1 or 2, depending upon whether A 1 ≥ A 2 or A 1 < A 2 (recall that the sequence must be strictly increasing). If we have computed L(1), L(2),... , L(i) for some i, how do we use these values to find L(i + 1)? We compute
L(i+1) = 1+max{L(j) : Aj < Ai+1}, or possibly L(i+1) = 1 (see below).
In other words, check all Aj , find those that are less than Ai+1. We do this because if Aj < Ai+1, then we can increase the longest subsequence that ends at Aj by one more value (namely, adding Ai+1 to that subsequence). Note that if there are no values Aj that are less than Ai+1, then we define L(i + 1) = 1 (as for L(1)). Computing L(i + 1) requires us to check all of the values A 1 ,... , Ai, i.e. O(i) time. So, in total, computing all of the L(i) values takes O(1+2+3+ · · ·+n) = O(n^2 ) time. Then finding the value L = max{L(1), L(2),... , L(n)} takes O(n) time. So finding the value L takes O(n^2 ) time.
For i = 1, we have V (1, a 1 ) = 1 and V (1, j) = 0 for j 6 = a 1.
For i ≥ 2, how can we find V (i, j) given that we have all of previously computed values (i.e. all of the V (1, j), V (2, j),... , V (i − 1 , j) values for all j)?
First of all, we have V (i, j) = 1 if V (i − 1 , j) = 1 or if V (i − 1 , j − ai) = 1. (Why?) Otherwise, V (i, j) = 0.
So after computing all of the V (i, j) values, what do we do?
Let T = (^12)
∑n i=1 ai. Then^ T^ is half of the sum of all the values in the set A. If T is an integer, we can first check to see if V (n, T ) = 1. If this is the case, then there is a partition S 1 , S 2 such that |sum(S 1 ) − sum(S 2 )| = 0. (Why?)
If T is not an integer, or if T is an integer but V (n, T ) = 0, then what do we do? For each value j with V (n, j) = 1, this means that there are two sets S 1 , S 2 such that sum(S 1 ) = j and sum(S 2 ) = 2T − j. For those particular sets, we have |sum(S 1 ) − sum(S 2 )| = |j − (2T − j)| = | 2 j − 2 T | = | 2 T − 2 j|.
We’re trying to minimize that value, so we compute
min{| 2 T − 2 j| : V (n, j) = 1 and j ≤ T }
to find this minimum value. (We need only consider the values j ≤ T in the minimum because of the symmetry of the problem, i.e. there is a set S 1 with sum(S 1 ) = j if and only if there is a set S 2 with sum(S 2 ) = 2T − j. Furthermore, we would likely consider the values V (n, j) in decreasing order of j starting from T (or the closest integer to T if it is a fraction) since we want to minimize the difference.)
This last part of computing the minimum takes time O(T ) ∈ O(n^2 K), so overall the whole algorithm can run in time O(n^2 K).