A Systematic Approach to Algorithms

Vijay K. Garg · The University of Texas at Austin

Chapter 7. Greedy Algorithms

Commit irrevocably to the locally-best choice; for problems with the right structure, the global optimum follows.

This page: Classical forms. View LLP forms »

This page collects the classical greedy algorithm implementations developed in this chapter. The lattice-linear (LLP) perspective and chapter setup live on the LLP companion page.

IntervalScheduling

Problem. Given $n$ intervals with start and finish times, choose a maximum-cardinality subset of pairwise non-overlapping intervals.

Algorithm. Sort by finish time and scan left to right; select an interval iff its start is at least the finish time of the last selected interval.

Time complexity: $O(n)$ once intervals are sorted by finish time, $O(n \log n)$ overall.

IntervalPartition

Problem. Schedule all $n$ intervals into the smallest number of rooms so that overlapping intervals never share a room.

Algorithm. Sort by start time; for each interval, reuse any room whose previous interval has finished, opening a new room only when none is free. Rooms used equals the maximum overlap depth.

Time complexity: $O(n \log n)$ via a priority queue keyed on room finish times.

MinMaxLateness

Problem. Schedule $n$ jobs (processing times $t_i$, deadlines $d_i$) on one machine to minimise $\max_i \max(0, C_i - d_i)$, the largest lateness.

Algorithm. Sort jobs by deadline and run them back-to-back in that order; the start time of job $i$ is the sum of the processing times of all earlier-deadline jobs.

Time complexity: $O(n \log n)$ for the sort, $O(n)$ for the schedule.

FractionalKnapsack

Problem. Given items with values $v_i$, weights $w_i$, and capacity $W$, choose fractions $x_i \in [0, 1]$ that maximise $\sum v_i x_i$ subject to $\sum w_i x_i \leq W$.

Algorithm. Sort items by value-density $v_i / w_i$ in non-increasing order; take each item fully until the next would exceed capacity, then take the largest fraction of that one item that still fits.

Time complexity: $O(n \log n)$, where $n$ is the number of items.

Looking ahead

The interval-scheduling and min-max-lateness ideas reappear in Chapter 8 (Shortest Paths) as the engine behind Dijkstra's algorithm: Dijkstra's "fix the closest unfixed vertex next" is exactly the greedy choice on the SSSP lattice. The exchange-argument template extends naturally to LLP correctness proofs.