Recitation Notes W12
Recitation Notes W12
1 Problem 1
You are running the end-of-day processing for a financial i n stitution. Y ou h a ve n d i fferent da ta base tasks
that have to be performed. Each task, t, consists of two parts; the first p a rt i s a d a tabase u p date that
takes expected time wt minutes, followed by post-processing that takes expected time pt minutes. Only
one task may update the database at a time, and tasks may not be interrupted once started. However, the
post-processing can occur in parallel. You may not leave until all the tasks are complete. Given a list, T , of
n tasks, devise an algorithm to output the fastest time it will take the tasks to complete, and the order of
the tasks.
1.1 Example
Input: T = [{4, 3}, {2, 5}, {3, 6}, {1, 2}, {5, 4}]
Output:
• time = 17
• Order = [t3 , t2 , t4 , t5 , t1 ]
1
2 Problem 2
One Halloween tradition is to hand out candy to kids dressed in costumes. You purchased a large bag of
assorted candies to hand out. However, you did not purchase enough for the n kids who are coming to your
house! You rush to the store to get more, but all they have left is candy corn that (almost) nobody likes.
You get it anyway to make sure you have n pieces of candy, but now you have a problem. Who gets the
good candy and who gets the candy corn?
Specifically, you have n pieces of candy and n kids visiting. The candy is ranked so candy i has value vi where
vi > vj means candy i is preferred by all children to candy j (lucky for you, all kids in your neighborhood
rank the candy the same). However, some children are more picky than others. Each child k has a preference
pk such that if they receive a candy with a value greater or equal to pk they will be happy, but anything less
and they will be upset.
Each child has a parent with weight, wk , representing how much you like the parent. If wa > wb , then you
like the child a’s parent more than you like the child b’s parent. Your goal is to give out candy in order to not
upset too many of the children whose parents you like. Specifically, devise an algorithm to determine which
child gets which piece of candy so that you minimize the sum of the weights of the upset childrens’
parents.
2.1 Example
Input:
1. n = 5
2. Candy values v = 3, 1, 5, 2, 4
3. Preferences p = 2, 3, 5, 4, 3
4. Weights w = 7, 3, 5, 10, 2
Output:
1. Total upset weight = 2
2
3 Problem 1 Solution
Greedy Choice: There is an optimal ordering such that the tasks are kicked off in decreasing order of
post-processing time, i.e. ∃ O = {t1 , . . . , tn } s.t. p1 ≥ · · · ≥ pn .
Let S be an optimal ordering of tasks. If S is in decreasing order of post-processing time, then we are done.
Otherwise, there must be an instance where pi < pi+1 . Consider task i starting its database update at time
mi . Therefore, the ending time of task i + 1 can be represented as
The two situations represent either task i finishing last, or task i + 1 finishing last. Based on the assumption
that pi < pi+1 , we should that task i must always finish last with the timestamp given above. But what if
we were to substitute our greedy choice into solution S? That is, if we swap the order of tasks i and i + 1
to create solution S ′ then we observe the following
=⇒ mi+1 ≥ m′i+1
Since we assumed that S was an optimal ordering, and showed that the ending time of S ′ must be less than
or equal to S, then there must always exist an optimal solution with our greedy choice.
3
3.2 Pseudocode
4
4 Problem 2 Solution
Greedy Choice: Assign each child the minimum candy value that satisfies their preference p i if possible.
If a child cannot be satisfied (i.e., if v i ¡ p i for the candy they receive), give them the lowest candy value
remaining.
1. wi > wi+1 but S assigns a lower-value candy to child i than to child i + 1 (i.e., vi < vi+1 ), even though
this lower-value candy does not satisfy child i’s preference (i.e., vi < pi ).
2. wi+1 > wi , meaning child i + 1’s parent is more highly weighted, but in S, child i + 1 receives a candy
that does not meet their preference, while child i receives a candy that does.
For each case, we can show that rearranging the assignments to match our greedy strategy does not increase
the total upset weight and may possible even reduce it.
Case 1
Suppose wi > wi+1 and vi < vi+1 in S, with vi < pi . In this case, child i is upset because they did not
receive a candy that meets their preference. By swapping the assignments so that child i receives vi+1 (the
higher value candy), we ensure that child i is now satisfied.
In the new solution S ′ :
1. If child i + 1 remains satisfied with the lower-value candy vi , then the total upset weight decreases
because we have satisfied child i, whose parent has a higher weight.
2. If child i+1 becomes upset, the total upset weight remains the same or decreases because we prioritized
satisfying the child with the higher-weighted parent (child i).
Thus, S ′ is at least as optimal as S and may be better, contradicting the assumption that S was an optimal
solution with a lower upset weight than the greedy solution.
Case 2
Suppose wi+1 > wi and S assigns a candy to child i+1 that does not satisfy their preference (i.e., vi+1 < pi+1 ),
while child i is satisfied. By swapping the assignments so that child i + 1 receives a higher-value candy, we
prioritize satisfying the more important parent. In this new solution S ′ , either:
1. Child i + 1 becomes satisfied and child i remains satisfied, reducing the total upset weight,
2. Only child i is now upset, but the total upset weight does not increase because we have reduced the
upset weight for the higher-weighted parent.
By applying this argument to all remaining inversions(where an inversion is defined as an instance where
wi+1 > wi , we can transform S into an optimal solution that follows the greedy choice without increasing
the total upset weight.
Since the greedy choice does not increase and may decrease the total upset weight, our greedy approach is
optimal.
5
4.2 Pseudocode
Algorithm 2 AssignCandy
1: procedure AssignCandy(n, vi , pi , wi )
2: Sort the children by their parent weights wi in descending order and children respectively
3: Sort the candies by value in ascending order
4: Initialize an empty list assignments
5: for i in range 1 to n do
6: j←n
7: while vj < pi do
8: j ←j−1
9: end while
10: assignments[i] ← candy[j]
11: vj ← 0
12: end for
13: return assignments
14: end procedure