- Hybrid strategies use large jumps to get to right "hill"
- Then use greedy hill-climbing to get to the top
![Saddle point between maxima, [Wikimedia](https://commons.wikimedia.org/wiki/File%3ASaddle_Point_between_maxima.svg)](static/img/Saddle_Point_between_maxima.svg)
---
## Problem-solving outline
+ Describe optimal **substructure** (e.g., via *recurrence*)
+ Convert to naive **recursive** solution
+ Could then convert to *dynamic programming*
+ Use **greedy choice** to simplify recurrence
+ So only *one* subproblem remains
+ Don't have to *iterate* through all subproblems
+ Need to **prove** greedy choice yields *global* optimum
+ Convert to **recursive** greedy solution
+ Convert to **iterative** (bottom-up) greedy solution
---
## Outline for today
+ Greedy algorithms
+ **Activity selection problem**
+ **Optimal substructure**
+ Proof of optimality of greedy
+ Greedy solution
+ List merging problem
+ Proof of optimality of greedy
+ Huffman coding
+ Knapsack problem: fractional and 0-1
+ Optimal offline caching
---
## Example: activity selection
+ **Activities** \`S = {a\_i}\_1^n\`:
This section thanks to Kevin Wayne and the textbook by Kleinberg + Tardos, "Algorithm Design"
---
## Greedy offline caching
+ Assume entire request sequence is *known* in advance
+ **LIFO** / **FIFO**: evict *most* (*least*) recently added item
+ **LRU**: evict item whose most *recent* access is the earliest
+ **LFU**: evict item least *frequently* accessed
| request | a | d | a | b | c | e | *g* |
|---------|-------|-------|-------|-------|-------|-------|-------|
| cache1 | **a** | a | a | a | a | a | *?* |
| cache2 | w | w | w | **b** | b | b | *?* |
| cache3 | x | x | x | x | **c** | c | *?* |
| cache4 | y | **d** | d | d | d | d | *?* |
| cache5 | z | z | z | z | z | **e** | *?* |
---
## Farthest-in-future
+ Since this is **offline**, we can **peek** into the future
+ **Farthest-in-future** algorithm ("clairvoyant"):
+ Evict item whose *next* request is the **farthest** in the future
+ **Provably** optimal offline caching strategy