Vous êtes sur la page 1sur 1

25 marks CS 218 Extra Quiz 8:30-9:25, 25/3/15

There is plenty of time; answers must be written exceedingly neatly, using full sentences.

Problem:[25 marks] Suppose there are n projects on which you can work. There are a total of
H hours you can spend on these projects. The returns you get on each project are given to you
as an n × H matrix R, where R[i, j] denotes the return (in rupees) you get if you spend j hours
on project i. The returns need not be linear, i.e. doubling the time spent on a project may not
double the return.
Give a pseudopolynomial time algorithm that takes as input integers n, H and the matrix R
and determines how many hours to allocate to each project so that the total return (the sum of
the returns on each project) is maximized.

Instance: R, H. Let R[i..n] denote rows i through n, i.e data for projects i through n.
Output: S[1..n], S[i] denotes the hours spent of project i.
Optimal substructure: S[2..n] is optimal for instance R[2..n], H − S[1].
Proof: Suppose some S 0 [2..n] has greater return X 0 for the instance R[2..n], H − S[1] than the
return X of S[2..n].
Now consider the solution S 00 [1..n] where S 00 [1] = S[1], and S 00 [i] = S 0 [i], for i = 2..n for the original
instance. Clearly, this has return R[1, S[1]] + X 0 > R[1, S[1]] + X. But the rhs is the return that was
claimed optimal. So we have a contradiction and hence S[2..n] must be optimal for R[2..n], H − S[1].
5 marks
Let G[i, h] = maximum return for projects i through n using h hours, where h ≤ H.
From optimal substructure we know that
G[i, h] = maxr∈[0,h] {R[i, r] + G[i + 1, h − r]} for i < n, 0 ≤ h ≤ H
G[n, h] = R[n, h] for 0 ≤ h ≤ H (Base case)
Thus G[i, h] can be calculated in order i = n to 0, in arbitrary order of j.
The optimal objective function value: G[1, H].
You get fewer marks if you write this as a function without mentioning memoization.
10 marks
2
Every entry takes time O(H) to fill. There are nH entries in all, so time O(nH ).
5 marks
The value of S[1..n]:
Let i = 1, h = H. Find h0 s.t. G[i, h] = R[i, h0 ] + G[i + 1, h − h0 ]. S[i] = h0 .
i = i + 1, h = h − h0 . Repeat if i < n. S[n] = h − h0 .
The time for this O(nH).
5 marks

Vous aimerez peut-être aussi