0% found this document useful (0 votes)
42 views

Ada 5

The document provides solutions to three algorithm problems: 1. The "Save Jatin" problem is solved using dynamic programming with a 2D table where an entry saveJatin(i,j) represents the optimal solution from night i to n with energy j. 2. The inventory management problem is solved using dynamic programming with a table where an entry OPT(i,s) represents the optimal solution from months i to n with s trucks at the start of month i. 3. The knapsack cover problem is solved using dynamic programming with a table where an entry B(i,P) represents the minimum size to obtain a value of at least P using the first i items.

Uploaded by

Tarushi Gandhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Ada 5

The document provides solutions to three algorithm problems: 1. The "Save Jatin" problem is solved using dynamic programming with a 2D table where an entry saveJatin(i,j) represents the optimal solution from night i to n with energy j. 2. The inventory management problem is solved using dynamic programming with a table where an entry OPT(i,s) represents the optimal solution from months i to n with s trucks at the start of month i. 3. The knapsack cover problem is solved using dynamic programming with a table where an entry B(i,P) represents the minimum size to obtain a value of at least P using the first i items.

Uploaded by

Tarushi Gandhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ADA 2021 Tutorial 5

Jatin, Syamantak

1. Define the subproblems clearly

2. Write a recursion using the above definition

3. Argue properly about why the recursion is correct (this is the optimal
substructure property)

4. Implement using tables and argue runtime.

1 Jatin turns DJ Again.

(From Midsem ADA 2021) Fed up with the accidental question paper leak,
Jatin decides to turn DJ once again. Now, he has n club nights he can possi-
bly perform at, for simplicity the ith club night is on night i. If he performs
on night i, he earns money vi and this costs him energy wi . Now, every day
he eats food (regardless of whether he has a performance that night) which
gives him exactly 1 unit of energy. Jatin starts with an initial energy of 0, so
just before the first night, his energy is exactly 1 (from the food that day).
Jatin needs to make sure that his energy never goes below zero else he
won’t be able to teach ADA ever again. In other words, if Jatin performs at
a subset S of nights then S should satisfy

i− ∑ w j ≥ 0 for all 1 ≤ i ≤ n
j∈S:j≤i

Give a polynomial-time (in n) algorithm to find the subset of club nights


Jatin can perform at to make the most money, while ensuring that his en-
ergy always stays non-negative. Assume that wi , vi are integers.
Solution.

1
Subproblems. Define saveJatin(i, j) to be the optimal solution to the sub-
problem with the nights under consideration being i, i + 1, · · · n and the
energy before the performance on night i equal to j, ∀1 ≤ i ≤ n + 1, j =
0, 1, 2, · · · n. (Note that since Jatin is getting 1 unit of energy every night,
the total energy cannot exceed n). [+2 for subproblems]
Recurrence.
 
saveJatin(i + 1, j + 1)
saveJatin(i, j) = max , ∀1 ≤ i ≤ n, wi ≤ j ≤ n
saveJatin(i + 1, j − wi + 1) + vi

saveJatin(i, j) = saveJatin(i + 1, j + 1), ∀1 ≤ i ≤ n, j < wi

saveJatin(n + 1, j) = 0∀ j

Final Solution. is given by OPT[1][1]

Algorithm 1 DP for Save Jatin


procedure F IND O PT B ALLS(v[1 : n], w[1 : n])
saveJatin[1 : n + 1][0 : n] : Array of size n + 1 × n + 1
for j = 0 to n do
saveJatin[n + 1][ j] ← 0
end for
for i = n down to 1 do
for j = 1 to n do
if j < wi then
saveJatin[i ][ j] ← saveJatin[i + 1][ j + 1]
else
saveJatin[i ][ j] ← max{saveJatin[i + 1][ j − wi + 1] +
vi , saveJatin[i + 1][ j + 1]}
end if
end for
end for
Return saveJatin[1][1]
end procedure

Runtime. We are filling n2 table entries and each of them takes constant
time. Hence the complexity is O(n2 ).

2
2 Inventory Management

Consider the following inventory problem. You are running a store that
sells some large product (let us assume you sell trucks), and predictions tell
you the quantity of sales to expect over the next n months. Let di denote the
number of sales you expect in month i. We will assume that all sales happen
at the beginning of the month, and trucks that are not sold are stored until
the beginning of the next month. You can store at most S trucks, and it costs
C to store a single truck for a month. You receive shipments of trucks by
placing orders for them, and there is a fixed ordering fee of K each time you
place an order (regardless of the number of trucks you order). You start out
with no trucks. The problem is to design an algorithm that decides how to
place orders so that you satisfy all the demands di , and minimize the costs.
In summary: There are two parts to the cost.

• First, storage: it costs C for every truck on hand that is not needed
that month. Second, ordering fees: it costs K for every order placed.

• Each month, you need enough trucks to satisfy the demand di , but the
amount left over after satisfying the demand for the month should
not exceed the inventory limit S.

Solution.
Subproblems. The subproblems are a bit subtle here. We define OPT(i, s)
to be the optimal solution for months i to n given that there are s trucks at
the beginning of month i (before placing any order).
Recurrence. Idea : Look at OPT(i, s). Now there could be two cases re-
garding what happens in month i

1. Suppose s > di . This is the easier case, in this case we do not order
any new truck in month i, pay a storage cost of C (s − di ). The residual
solution would be (at least we hope) OPT(i + 1, s − di ).

2. Suppose s ≤ di . In this case we need to order new trucks - note that


this cost is just K irrespective of the number of trucks ordered. Now
suppose you have 0 ≤ s0 ≤ S trucks remaining after meeting the
demand. Then you pay Cs0 for the storage. The remaining solution
(we hope) would be OPT(i + 1, s0 ). Hence, the recurrence is :

3
(
OPT(i + 1, s − di ) + C (s − di ) if s ≤ di
OPT(i, s) = min
min0≤s0 ≤S K + Cs0 + OPT(i + 1, s0 ) if s0 > di

What is the base case ?


Correctness. We are going to prove the correctness of the recurrence in two
different case.

1. (s ≤ di ): We claim that in this case OPT(i + 1, s − di ) + C (s − di ) =


OPT(i, s). To prove this, let us take an optimal solution for the months
i to n and s number of trucks being available at the start of month i.
Now let us consider this schedule and look at the beginning of month
i + 1. Clearly we remain with s − di trucks at the beginning of month
i + 1. Hence, the residual schedule is clearly a feasible solution for
i + 1 to n months when there are s − di trucks at the beginning of
month i + 1 and the cost of this solution is OPT(i, s) − C (s − di ). Now
suppose for contradiction this residual solution is not OPT(i + 1, s −
di ). Hence OPT(i + 1, s − di ) < OPT(i, s) − C (s − di ). Now consider
a schedule where you sell di trucks in month i and the rest of the
schedule is OPT(i + 1, s − di ). This schedule is a feasible schedule
for months i through n when we start with s trucks. But it has cost
OPT(i + 1, s − di ) + C (s − di ) < OPT(i, s) contradicting the optimal-
ity of OPT(i, s).

2. (s > di ) : The proof is very similar. Please figure it out yourself. You
need to practice writing proofs

Algorithm. Quite straightforward.

3 Knapsack cover

You are given items i with sizes wi and values vi , and a target value V. Find
a subset of items of minimum total size with total value at least V (or decide
that there is no such subset).
Solution.
Subproblem.

4
For 1 ≤ i ≤ n, 0 ≤ P ≤ V,

B(i, P) : minimum total size of items 1, 2, . . . , i whose value is at least P

Recurrence.
(
B(i − 1, P)
B(i, P) = min
B(i − 1, P − vi ) + wi if P ≥ vi

Base case:
(
0 for P = 0
B(0, P) =
∞ otherwise

Correctness.
Consider two cases about the optimum solution OPT for B(i, P).

1. i ∈ OPT: We show that OPT \ i is optimal for the subproblem for (i −


1, P − vi ). It has value P − vi and uses items 1, 2, . . . , i − 1 and hence
is feasible. To show that it has the optimal size, suppose for contra-
diction that, there is a solution S of smaller total size than OPT \ i.
Then, S ∪ i is a solution of value at least P − vi + vi = P using items
1, 2, 3, . . . i and hence is feasible for the subproblem for (i, P). But
then the total size of S ∪ i is size(S) + wi < size(OPT \ i ) + wi =
size(OPT ); which contradictions the definition of OPT.
Hence, OPT \ i has size B(i − 1, P − vi ) and OPT has size B(i − 1, P −
v i ) + wi .

2. i ∈
/ OPT: Write yourself, similar to the above.

Pseudocode.
Write yourself.
Time Complexity.
There are nV subproblems and each takes O(1) time. Hence, the time com-
plexity is O(nV ).

You might also like