



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
[Week 7] Dynamic Programming, Weighted Interval Scheduling, Knapsack
Typology: Exercises
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Algorithms 9007 Tutorial questions 2019 Semester 1 Tutorial 6
The University of Sydney School of Computer Science
Do you know the basic concepts of this week's lecture content? These questions are only to test yourself. They will not be explicitly discussed in the tutorial, and no solutions will be given to them.
(a) What are the three main ingredients in developing a dynamic programming algorithm?
(a) What are the subproblems? (b) What is the recurrence? Do you understand the recurrence? (c) What are the base cases?
(a) What are the subproblems? (b) What is the recurrence? Do you understand the recurrence? (c) What are the base cases?
Problem 1 Consider the following function computing the Fibonacci number
Algorithm 1 Fibonacci 1: function Fib(n) 2: if i == 0 or i == 1 then 3: return i 4: else 5: return Fib(i โ 1) + Fib(i โ 2); 6: end if 7: end function
Draw the tree illustrating the recursive calls for Fibonacci(5). It turns out that the number of leafs in a call tree for Fibonacci is equal to the Fibonacci number itself, which is roughly 1: 618 n. Write an iterative implementation of Fibonacci(n) that only uses O(n) time and space.
Solution:
Algorithm 2 Fibonacci 1: function Fib(n) 2: initialise an array M [0::n] 3: M [0] โ 0 4: M [1] โ 1 5: for i = 2 to n do 6: M [i] โ M [i โ 1] + M [i โ 2] 7: end for 8: return M [n] 9: end function
Problem 2 Show all intermediate steps of the dynamic programming algorithm for the weighted interval scheduling problem, for the following input.
item 1 2 3 4 5 6 7 8 9 10 start 0 1 0 3 2 4 6 2 7 6 nish 2 3 4 5 6 7 8 9 10 11 weight 2 9 6 5 7 11 8 10 4 6
Solution: The DP recurrence for the weighted interval scheduling problem is:
M [j] = max (wj + M [p(j)]; M [j โ 1]) ;
with M [0] = 0 as the base case. Recall that M [j] is the optimum value of the subinstance restricted to intervals I 1 ; : : : ; Ij. Let Oj be the optimum set of items that gives the value M [j].
j 1 2 3 4 5 6 7 8 9 10 sj 0 1 0 3 2 4 6 2 7 6 fj 2 3 4 5 6 7 8 9 10 11 wj 2 9 6 5 7 11 8 10 4 6 p(j) 0 0 0 2 1 3 5 1 6 5 M [j] 2 9 9 14 14 20 22 22 24 24 Oj { 1 } { 2 } { 2 } { 2 , 4 } { 2 , 4 } { 2 , 6 } { 2 , 4 , 7 } { 2 , 4 , 7 } { 2 , 6 , 9 } { 2 , 6 , 9 }
The solution is at M [10] = 24 and corresponds to Oj = { 2 ; 6 ; 9 }.
Problem 3 Consider the following variant of the Interval Scheduling problem we saw in class. The input is de ned by a set of intervals I 1 ; : : : ; In. We say that interval Ii = (si; fi) has length fi โ si. We would like to pick a set of non-intersecting intervals that use as much as possible of the common resource; that is, we want to maximize the sum of the lengths of the scheduled intervals. Design an efficient algorithm for this problem using dynamic programming.
Solution: Number all the locations along the trail. We start at location 0 and want to go to location n + 1; there are n campsites along the way. Let D(0; i) be the distance from 0 to location i; to compute the distance between two locations j < i we use D(j; i) = D(0; i) โ D(0; j). The journey takes k days, and we stop at k โ 1 places along the way. Let M (i; โ) to be cost of the optimal solution to reach campsite i in โ days. To derive a recurrence for this quantity we need to consider which campsite was used before the โth, suppose it was j then M (i; โ) = max{M (j; โ โ 1); D(j; i)}. For the base case, if we want to reach location i in one day we must walk from 0 to i, thus 8 < :
M (i; โ) = min j<i
max{M (j; โ โ 1); D(j; i)}
M (i; 1) = D(0; i):
The answer will be in M (n + 1; k). The running time is O(n^2 k) since the table has size O(nk) and computing each entry takes O(n) time.
Problem 6 Consider a post office that sells stamps in three different denominations, 1c, 7c, and 10c. Design a dynamic programming algorithm that will nd the minimum number of stamps necessary for a postage value of n cents. (Note that a greedy type of algorithm wont necessarily give you the correct answer, and you should be able to nd an example to show that such a greedy algorithm doesnt work.) What is the running time of your algorithm?
Solution: Let S[n] denote the minimum number of stamps needed to make postage for n cents. We clearly then have S[0] = 0 S[1] = 1 S[2] = 2 S[3] = 3 S[4] = 4 S[5] = 5 S[6] = 6 S[7] = 1 S[8] = 2 S[9] = 3 S[10] = 1: These cases above should be considered our base cases and we then work from these to compute values of S[n] for higher values of n. The main idea, then is to consider what happens if we use a stamp of a particular value. For example, if we want postage of n cent, then we can clearly get it by taking one stamp of 1c, and S[n โ 1] stamps (of appropriate values) to make up the remaining postage of (n โ 1)c. Or if we take a 7c stamp, then we need S[n โ 7] stamps (of the right values) to make up the rest, and similarly if we take a 10c stamp. This idea is essentially the heart of the dynamic programming algorithm that we use. Set up the array S starting as above (with the values S[i] for i = 0; : : : ; 10). If n > 10 then S[j] = 1 + min{S[j โ 1]; S[j โ 7]; S[j โ 10]}. Thus, nding S[n] takes O(n). Now we can nd S[n] easily as above, but knowing this does not tell us the exact nature of the stamps we need. It would be nice to know that we can make 45c postage with six stamps, and that we need one 1c, two 7c, and three 10c stamps to do so. (For some values of n there could certainly be more than one way to do this. For example, we can get 63c with nine 7c stamps, or with six 10c and three 1c stamps.) Well, we can easily do this too with another array called, say, P. Then P [n] will be a vector of length 3 that will tell us what stamps we need to make postage for nc. For example, we would have P [45] = (1; 2 ; 3), meaning that we need one 1c, two 7c, and three 10c stamps. In general, if we have P [n] = (a; b; c), then we take a 1c stamps, b 7c stamps, and c 10c stamps to make nc postage. The calculation for S[n] doesnt 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 running time is still O(n).
Problem 7 Let T (n) be the number of different binary search trees on n keys. For example, T (1) = 1 and T (2) = 2. Come up with a recurrence for T (n). Use dynamic programming to compute T (n) for a given n. Run some experiments to determine the asymptotic growth of T (n).