learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Algorithm Selection

Select an algorithm and generate steps to begin visualization

Interval Scheduling and Meeting Rooms Visualizer

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

Time: O(n log n)
Space: O(n)
Use Case: Meeting rooms, activity selection, register allocation, bandwidth booking
Showing the example graph for this algorithm.

11 intervals, named A, B, C... in this order

Try:

Each interval [start, end) becomes a vertex placed on a time axis. Intervals that overlap are joined by an edge.

Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Interval Scheduling and Partitioning

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.

How it works

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.

Applications

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.

Pseudocode

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 rooms

Both 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.

Worked example, step by step

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.

  1. Sort by end time. The order is A (4), B (5), C (6), D (7), E (9), F (9), G (10), H (11), I (12), J (14), K (16).
  2. Choose A. A ends first, at 4, so it is chosen. B and C start before 4 and are skipped.
  3. Choose D. D starts at 5, after A ends, and is chosen. E, F and G start before D ends at 7.
  4. Choose H and K. H starts at 8 and is chosen, I and J overlap it, and K starts at 12, after H ends at 11. Part 1 ends with A, D, H and K, four intervals.
  5. Rooms by start time. Part 2 takes C, A, J, B and E in start order, and each finds every room busy: five rooms by time 3. Then D reuses room 2, freed when A ended at 4, F takes room 4, freed by B at 5, G takes room 1, freed by C at 6, and H takes room 2 again.
  6. The sixth room. At 8, I finds G, H, J, F and E running in rooms 1 to 5, so it opens room 6. K later reuses room 1.

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.

Complexity, and where it comes from

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.

When to use Interval Scheduling and Partitioning, and when not to

Close relatives and when they are the right tool.

AlternativePrefer it whenCost
Weighted interval schedulingIntervals 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 deadlinesJobs 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)

Common pitfalls

  • Sorting by start time or by length in part 1. Taking the interval that starts first can pick one long interval that blocks everything, like C [0, 6) here. Taking the shortest can pick one that overlaps two others which would both fit. Only the earliest end time is always optimal.
  • Closed versus half-open intervals. If one meeting ends at 5 and the next starts at 5, they share a room only with half-open intervals. Mixing the conventions changes the answer, so decide whether ends are included and use < or <= consistently.
  • Counting rooms by overlaps. The room count is the largest number of intervals running at one moment, not the largest number of intervals one interval overlaps. Here J overlaps 10 others, but only 6 ever run together.

Frequently asked questions

Why is earliest finish time optimal?
Take any best schedule. Its first interval ends no earlier than the interval that ends first overall, so swapping the two keeps the schedule valid and the same size. Repeating the argument on the rest of the time line shows that the greedy schedule is as large as the best one.
What is the meeting rooms problem?
Given meeting times, find the fewest rooms that hold them all. It is part 2 of this page: sort by start, reuse a room whose meeting has ended, open a new one otherwise. The answer equals the largest number of meetings in progress at any moment.
What is an interval graph?
A graph whose vertices are intervals on a line, with an edge whenever two intervals overlap. Interval graphs are chordal and perfect: the largest clique equals the chromatic number, and independent sets, cliques and colourings can all be found in polynomial time.

Related algorithms: Graph Coloring, DSatur Coloring, Chordality Check, Maximal Clique, Critical Path Method (CPM)

An example graph: you can change it, or start blank.

Zoom Controls

34%
Nodes: 11
Edges: 31