Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Solutions · 8. Greedy algorithms: exchange arguments, optimal caching and Bélády’s anomaly

School of Engineering, Nazarbayev University

Worked solutions to the three exercises of Lecture 8. Each solution is computed here and checked with assert statements against an exact identity or an independent calculation. The first code cell sets up the notebook and runs Demonstration 1 of the lecture without showing its output, because some of the solutions use its variables.

Exercise 1

Problem. Across 200 random 14-interval schedules, verify that (a) interval_scheduling_greedy matches an exact DP oracle on 100% of trials, and (b) against every compatible schedule (o1,…,om)(o_1, \dots, o_m) sorted by finish time, greedy’s sorted selections (g1,…,gk)(g_1, \dots, g_k) obey f(gr)≤f(or)f(g_r) \leq f(o_r) for all 1≤r≤m1 \leq r \leq m.


Exercise 1: 200-trial greedy stay-ahead invariant and exact DP verification
===========================================================================
Exact EFT == DP cardinality verified on 200/200 random 14-interval instances
Stay-Ahead inequality f(g_r) <= f(o_r) verified across 9,032 individual rank comparisons

Explanation. Because f(gr)≤f(or)f(g_r) \leq f(o_r) holds inductively at every rank rr, greedy always frees up the shared resource at least as early as any competing valid schedule—leaving a superset of remaining future intervals available after step rr.


Exercise 2

Problem. Verify ValueError guards on interval_scheduling_greedy and belady_optimal_cache, audit greedy coin-change suboptimality on [1, 3, 4] (A=6A = 6) and [1, 7, 10] (A=14A = 14), and prove numerically why cyclic scan [0, 1, 2, 3] * 15 in a cache_size = 3 buffer causes 100% misses (60/60) under LRU/FIFO vs. 22 misses under Bélády.


Exercise 2: Failure-mode audit — guards, coin-change traps and cyclic scan thrashing
====================================================================================
Coin trap [1, 3, 4] at A=6   : Greedy = 3 coins vs. Optimal DP = 2 coins
Coin trap [1, 7, 10] at A=14 : Greedy = 5 coins vs. Optimal DP = 2 coins
Cyclic scan (M=4 in k=3)     : LRU = 60/60 misses (100% thrashing) vs. Belady = 22/60 misses (63.3% hits)

Explanation. On a cyclic table scan 0, 1, 2, 3, 0, 1, 2, 3, ... with cache size k=3<4k = 3 < 4, when 3 arrives at {0, 1, 2}, the least recently used item is 0—which is the exact item needed on the very next step. Thus LRU evicts the worst possible page on 100% of accesses (k-competitive worst case), whereas Bélády evicts 2 (needed 3 steps away), keeping two slots pinned and hitting 2/32/3 of steady-state requests.


Exercise 3

Problem. Implement weighted_interval_scheduling_dp(intervals_with_weights) in O(Nlog⁡N)\mathcal{O}(N \log N) time using finish-time sorting, binary search (bisect_right) for predecessor p(j)p(j), and Bellman DP with backpointer subset reconstruction. Verify exact optimality against 2N2^N brute-force enumeration across 40 random weighted instances (N = 12) and verify that greedy EFT fails when weights vary.


Exercise 3: Implement it yourself — O(N log N) weighted interval scheduling DP
==============================================================================
Weighted trap : EFT Greedy = 20.0 vs. Weighted Bellman DP = 95.0 (selected [1])
Verified O(N log N) Weighted Interval DP against 2^N brute-force oracle across 40/40 random instances

Summary. Adding arbitrary weights wiw_i breaks the greedy Stay-Ahead exchange argument (here the greedy choice of two jobs of weight 10 blocks one job of weight 95), yet sorting by finish time still provides the topological DAG order needed for binary-search Bellman DP to solve the weighted problem exactly in O(Nlog⁡N)\mathcal{O}(N \log N) time.