Docsity

Dynamic Programming Exercises-Theory of Complexity and Algorithms-Assignment, Exercises of Advanced Algorithms

Toggle sidebar
bg1
Dynamic Programming
1. (a) Solve the following instance of the {0,1}Knapsack Problem with
four items where the maximum allowed weight is Wmax = 10.
i1 2 3 4
bi25 15 20 36
wi7 2 3 6
(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)?
2. Do the same as above (find solutions to both the {0,1}and Fractional
Knapsack problems) for this collection of items, where Wmax = 11.
i12345
bi14 12 15 20 16
wi43873
3. (This is one of the problems from the sheet I gave out on the first day of
class.) Consider a post office that sells stamps in three different denomi-
nations, 1p, 7p, and 10p. Design a dynamic programming algorithm that
will find the minimum number of stamps necessary for a postage value of
N pence. (Note that a greedy type of algorithm won’t necessarily give you
the correct answer, and you should be able to find an example to show
that such a greedy algorithm doesn’t work.)
What is the running time of your algorithm?
4. (This is also from the initial problem sheet that I gave to you.)
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?)
5. (Adapted from Algorithm Design by Jon Kleinberg & ´
Eva Tardos.)
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 vhas a parent node u
equal to his or her superior officer. Conversely, we can call vadirect
subordinate of u. See the picture below, where Ais the ranking officer, B
and Dare direct subordinates of A, and Cis a direct subordinate of B.
1
docsity.com
pf3
pf4

Partial preview of the text

Download Dynamic Programming Exercises-Theory of Complexity and Algorithms-Assignment and more Exercises Advanced Algorithms in PDF only on Docsity!

Dynamic Programming

  1. (a) Solve the following instance of the { 0 , 1 } Knapsack Problem with four items where the maximum allowed weight is Wmax = 10. i 1 2 3 4 bi 25 15 20 36 wi 7 2 3 6 (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)?
  2. Do the same as above (find solutions to both the { 0 , 1 } and Fractional Knapsack problems) for this collection of items, where Wmax = 11. i 1 2 3 4 5 bi 14 12 15 20 16 wi 4 3 8 7 3
  3. (This is one of the problems from the sheet I gave out on the first day of class.) Consider a post office that sells stamps in three different denomi- nations, 1p, 7p, and 10p. Design a dynamic programming algorithm that will find the minimum number of stamps necessary for a postage value of N pence. (Note that a greedy type of algorithm won’t necessarily give you the correct answer, and you should be able to find an example to show that such a greedy algorithm doesn’t work.) What is the running time of your algorithm?
  4. (This is also from the initial problem sheet that I gave to you.)

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

  1. (Adapted from Algorithm Design by Jon Kleinberg & ´Eva Tardos.)

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.

m

m m

m

J

J

J

 JJ^

A

B D

C

To notify everyone of the postponement, the ranking officer first calls each of her direct subordinates, one at a time. As soon as each subordinate gets the phone call, he or she must notify each of his or her direct subordinates, one at a time. The process continues in this was until everyone is notified. Note that each person in this process will only call direct subordinates, for example A would not be allowed to call C. We can picture this notification process as divided into rounds. In one round, each person who has already learned of the postponement can call one of his or her subordinates on the phone. The number of rounds it takes for everyone to be notified might depend upon the sequence in which each person calls their direct subordinates. In the given example, it will take only two rounds if A starts by calling B, but will take three rounds if A starts by calling D. Give an efficient algorithm that determines the minimum number of rounds needed for everyone to be notified for a rooted tree T as described above. How can you output a sequence of phone calls (i.e. a schedule for each person) that achieves this minimum number of rounds?

  1. Consider the set of weighted intervals given below, where si is the start time, fi is the finish time, and vi is the value of the interval.

j sj fj vj 1 0 6 2 2 2 10 4 3 9 15 6 4 7 18 7

Solve this instance of the weighted interval scheduling problem, i.e. find a set of (non-conflicting) intervals with maximum total weight.

(Note: For such a small set of intervals, it’s likely “easy” to see the solution without much work, but the intention is for you to understand how the algorithm we discussed in class works. So I would really expect you to find the p(j) values we defined, and the values Opt(j) that would be computed to find the solution.)

the subsequence consisting of 4, 5 , 10 is an increasing subsequence, as is − 3 , 0 , 3 , 5 , 10. Your task is to find a longest subsequence so that the values are strictly increasing. (There might be repeated numbers in the original sequence, but there can be no repeats in a longest subsequence since it must be strictly increasing.) 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?

  1. Balanced Partition. Suppose that you have a set, A = {a 1 ,... , an}, of n integers, each of which is between 0 and K (inclusive). Your goal is to find a partition of A into two sets S 1 and S 2 (so S 1 ∪ S 2 = A and S 1 ∩ S 2 = ∅) that minimizes |sum(S 1 ) − sum(S 2 )|, where sum(Si) equals the sum of the values in the set Si. Hint: Define V (i, j) = 1 if there is some subset of {a 1 ,... , ai} that sums up to the value j (i.e. some collection from the first i integers sums up to j), otherwise set V (i, j) = 0. How many different values of V (i, j) do you need to compute (in terms of n and K)? How can you compute the V (i, j) values if you know all of the V (i − 1 , j) values? How can you use the collection of V (i, j) values to answer the original question (and find the minimum value of |sum(S 1 ) − sum(S 2 )|)?