Guest
Sign in to keep your graphs and progress.
Select an algorithm and generate steps to begin visualization
Earliest finish first, then the fewest rooms
Picks the most non-overlapping intervals by earliest finish time, then fits them all into the fewest rooms: an independent set and a colouring of the interval graph
11 intervals, named A, B, C... in this order
Each interval [start, end) becomes a vertex placed on a time axis. Intervals that overlap are joined by an edge.
Select an algorithm and generate steps to begin visualization
Interval scheduling starts from a list of time intervals, such as talks, bookings or jobs, each with a start and an end. Two questions follow. Which is the largest set of intervals that can run in one room without overlapping? And how many rooms are needed to run all of them? As a graph, each interval is a vertex and each overlap an edge, an interval graph. The first question asks for a maximum independent set and the second for a colouring with the fewest colours, both NP-hard in general graphs but easy for intervals.
Part 1 sorts the intervals by end time and takes each one that starts after the last chosen one ends. Ending first leaves the most room for the rest: any best schedule can swap its first interval for the earliest-ending one without losing anything, and the argument repeats. Part 2 sorts by start time and gives each interval a room whose last interval has ended, opening a new room only when all are busy. The number of rooms equals the largest number of intervals running at one moment, which no plan can beat. Both parts take O(n log n). Intervals here are half-open, [start, end), so one meeting may start the moment another ends. The visualizer places the intervals on a time axis, marks the chosen ones green and the skipped ones grey with the overlap that rules them out, then colours the rooms.
The two parts are the activity selection problem of textbooks and the "meeting rooms" question of interviews. They schedule lecture halls, operating theatres, rental cars and network bandwidth, and in compilers the same room count bounds how many registers a set of live variables needs. When intervals carry different values, the greedy rule no longer works and weighted interval scheduling uses dynamic programming instead. Interval graphs are also the textbook example of perfect graphs, where the largest clique equals the number of colours.
Two greedy passes, one in order of end time and one in order of start time.
MaxSchedule(I): // part 1
sort I by end time
chosen = [], free_from = -infinity
for each [s, e) in I:
if s >= free_from:
add [s, e) to chosen, free_from = e
return chosen
Rooms(I): // part 2
sort I by start time
heap = empty // end time of each open room
for each [s, e) in I:
if heap is not empty and min(heap) <= s:
reuse that room: replace its end time by e
else:
open a new room with end time e
return the number of roomsBoth tests compare with <= and >=, because the intervals are half-open: a meeting may start the moment another ends. In part 2 a heap of end times answers "is any room free?" in O(log n). The visualizer takes the lowest-numbered free room instead, which needs the same number of rooms and keeps the colours stable.
The eleven activities of the classic CLRS example, loaded on this page.
Example graph: A [1, 4), B [3, 5), C [0, 6), D [5, 7), E [3, 9), F [5, 9), G [6, 10), H [8, 11), I [8, 12), J [2, 14), K [12, 16). Each overlap is an edge, 31 in all.
At most 4 intervals fit in one room (A, D, H, K), and all 11 need 6 rooms. At time 8 the six intervals E, F, G, H, I and J run together, a clique of size 6 in the interval graph, so no plan can use fewer rooms.
Time: O(n log n) · Space: O(n)
Sorting dominates part 1, the scan after it is linear. Part 2 also sorts, then does one heap operation of O(log k) per interval, k the number of rooms, so O(n log n) in all. The visualizer scans the rooms instead of keeping a heap, which costs O(n·k) and is easy to follow on small examples.
Close relatives and when they are the right tool.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Weighted interval scheduling | Intervals carry different values and the most total value is wanted, not the most intervals. Sort by end time and use dynamic programming with binary search. | O(n log n) |
| General graph colouring (DSatur, Welsh–Powell) | The conflicts do not come from time intervals, so the graph is not an interval graph and the start-time order is no longer optimal. | O(n²) heuristics, exact colouring is NP-hard |
| Single machine with deadlines | Jobs have a length and a deadline instead of a fixed start, so the schedule decides when they run. Earliest deadline first or Moore–Hodgson apply. | O(n log n) |
Read the full article: The Graph Coloring Problem
Read the full article: Cliques, Independent Sets and Vertex Covers
Related algorithms: Graph Coloring, DSatur Coloring, Chordality Check, Maximal Clique, Critical Path Method (CPM)