Ada 5
Ada 5
Jatin, Syamantak
3. Argue properly about why the recursion is correct (this is the optimal
substructure property)
(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
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(n + 1, j) = 0∀ j
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 ).
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
2. (s > di ) : The proof is very similar. Please figure it out yourself. You
need to practice writing proofs
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,
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).
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 ).