Problem Setup
Pizza Order |
Preparation Time () |
Baking Time () |
Pizza 1 |
5 minutes |
15 minutes |
Pizza 2 |
8 minutes |
10 minutes |
Pizza 3 |
3 minutes |
20 minutes |
Pizza 4 |
6 minutes |
5 minutes |
Optimal Order
Solution Approach
- Each of the preparations happen one after the other
- Does the order impact preparation time?
- What about the baking time?
- The pizza that takes the longest to bake should be put in first
Greedy Solution
- Sort orders by non-increasing baking time
- This ensures the pizzas with the longest baking times go into the oven first
Proof of Optimality
- Consider any non-optimal permutation.
- Identify consecutive pizzas and
- Current order: then where
Proof of Optimality
-
Case Analysis:
- Pizza Finish Time :
- Pizza Finish Time :
-
What If We Swap?:
- Pizza Finish Time :
- Pizza Finish Time :
Cache Eviction Scheduling
- Cache holds data pieces, where is the total memory
- We can store some data into the cache, which is fast
- If we want to store data, but the cache is full, we have to evict something
- aka Cache Miss
- Suppose we have a sequence of requests
- We would like to minimize cache misses
Farthest-in-Future Eviction Strategy
- Policy: Evict the data item needed farthest in the future
- Intuition: Keeps most immediately needed items in cache
- Not realistically possible, but pretend we have such a machine
- Goal: Show this is the best we can do to minimize cache misses
Reduced Schedules
- Definition: Reduced schedule performs evictions only on cache misses
- Prove the following:
- For any non-reduced schedule , there’s a reduced with
the same or fewer evictions
Reduced Schedules
- Proof is very simple!
- Instead of evicting without a cache miss, just wait until there is one!
- No negative impacts on procrastinating
Proving Optimality of Farthest-in-Future
- Suppose we have i.e. schedule for farthest in future eviction
- Consider some schedule with the fewest cache misses
- Prove that any can be converted into without more cache misses
Proving Optimality of Farthest-in-Future
- Let us consider a conversion
- We show:
- will have the same cache contents as
- and have the same number of evictions
Proving Optimality of Farthest-in-Future
- Proof is through induction, suppose the statement holds for
- At the eviction between and may differ
- Suppose is being requested and and are evicted
- Have evict instead of
- Now they are the same, but does still have the same cache misses?
- Look at the next time they differ: both instances can be resolved