BITS Pilani
CS F364: Design and Analysis of Algorithms

Greedy: Interval Scheduling

Lecture 13 |2026-06-05

Greedy Algorithms

  • Greedy Algorithms
  • Greedy Choice
  • Algorithm & Correctness
  • Properties & Summary

Greedy Algorithm

  • A greedy algorithm always makes the choice that looks best at the moment.
    • Everyday example: playing cards, investing in stocks, choosing a university.
    • The hope: a locally optimal choice will lead to a globally optimal solution.
    • For some problems, it works.
  • Greedy algorithms tend to be easier to code and more efficient than DP.

Interval Scheduling

  • Input: A set of intervals I = \{I_1, I_2, \dots, I_n\}. Each interval I_i = [s_i, f_i) with start s_i and finish f_i.

  • Compatibility: Two intervals are compatible if they do not overlap (f_i \le s_j or f_j \le s_i).

  • Output: A maximum-size subset of mutually compatible intervals.

CLRS (T1) refers to this problem as Activity Selection. The name Interval Scheduling (Kleinberg & Tardos) is more common.

Example

Compatible Subsets:

  • \{I_3, I_9, I_{11}\} (Size 3)
  • \{I_1, I_4, I_8, I_{11}\} (Size 4 — Optimal)
  • \{I_2, I_4, I_9, I_{11}\} (Size 4 — Optimal)

Goal: find a subset of maximum size.

Interval Representation

  • Each activity is a half-open interval [s_i, f_i).
  • Goal: Select the maximum number of non-overlapping intervals.
  • A classic scheduling problem with applications in resource allocation, classroom scheduling, CPU task execution, etc.

Greedy Choice

  • Greedy Algorithms
  • Greedy Choice
  • Algorithm & Correctness
  • Properties & Summary

Optimal Substructure (DP View)

Sort intervals by finish time: f_1 \le f_2 \le \dots \le f_n.

Define S_{ij} = \{I_k : f_i \le s_k < f_k \le s_j\}.

Let c[i,j] be the maximum number of compatible intervals in S_{ij}:

c[i,j] = \begin{cases} 0 & \text{if } S_{ij} = \emptyset \\[4pt] \displaystyle\max_{i < k < j} \big(c[i,k] + c[k,j] + 1\big) & \text{if } S_{ij} \ne \emptyset \end{cases}

A DP solution would be O(n^3) — but a greedy approach is simpler and faster.

Greedy Choice

Intuition: Choose the interval that leaves the resource available for as many others as possible.

Which interval should we pick first?

  • The one that starts earliest? ❌ (might be very long)
  • The one that is shortest? ❌ (might overlap many)
  • The one that finishes earliest? ✅

Greedy choice: Always pick the interval with the earliest finish time among the remaining compatible intervals.

Failed Greedy Strategies

1. Earliest Start Time

Pick the interval that starts first.

Counterexample:

I_1: |-------------------------| (0 to 100)
I_2:   |---| (1 to 5)
I_3:         |---| (6 to 10)
  • Greedy: \{I_1\} (size 1)
  • Optimal: \{I_2, I_3\} (size 2)

2. Shortest Interval

Pick the interval with smallest duration f_i - s_i.

Counterexample:

I_1: |-------| (0 to 6)
I_2:      |---| (5 to 7 - shortest)
I_3:          |-------| (6 to 12)
  • Greedy: \{I_2\} (size 1)
  • Optimal: \{I_1, I_3\} (size 2)

Why Earliest Finish?

Among all intervals, one must finish first in any optimal solution.

Picking the earliest finish leaves the maximum remaining time for other intervals.

After sorting by finish time, the greedy choice picks I_1 first.

Algorithm & Correctness

  • Greedy Algorithms
  • Greedy Choice
  • Algorithm & Correctness
  • Properties & Summary

Greedy Algorithm

Greedy-Interval-Scheduler(s, f):
    // Assumes f[1..n] is sorted in increasing order
    n = length(s)
    A = {I_1}
    k = 1
    for m = 2 to n:
        if s[m] >= f[k]:
            A = A ∪ {I_m}
            k = m
    return A
  • Time: \Theta(n) after sorting (\Theta(n \log n) total with sort).
  • Space: O(1) auxiliary.

Trace

Trace

Trace

Trace

Trace

Trace

Trace

Correctness Proof

Two parts:

  1. Greedy Choice Property: There exists an optimal solution that includes the greedy choice.
  2. Optimal Substructure Property: After making the greedy choice, the remaining problem is a smaller instance of the same type.

Greedy Choice Property

Lemma

There exists an optimal solution A such that the greedy choice I_1 (earliest finish) is in A.

Proof. Let A be an optimal solution, ordered by finish time. Let k_1 be the first interval in A.

  • If k_1 = 1: done — A already includes the greedy choice.
  • If k_1 \ne 1: construct A' = (A \setminus \{k_1\}) \cup \{I_1\}.
Original A:  |--- k_1 ---|   |--- k_2 ---|   |--- k_3 ---|
Greedy:      |-- I_1 --|

Modified A': |-- I_1 --|   |--- k_2 ---|   |--- k_3 ---|

Since f_1 \le f_{k_1}, I_1 cannot overlap with k_2 (as s_{k_2} \ge f_{k_1} \ge f_1).

\therefore A' is mutually compatible, |A'| = |A|, so A' is also optimal. \square

Optimal Substructure Property

Note

Lemma: If A is an optimal solution, then A' = A \setminus \{I_1\} is an optimal solution to S' = \{I_i \in I : s_i \ge f_1\}.

Proof (Contradiction).

  • Suppose \exists B' for S' with |B'| > |A'|.
  • Let B = B' \cup \{I_1\}.
  • Since B' only contains intervals starting after f_1, I_1 is compatible with all in B'.
  • Then B is a valid solution for I with |B| = |B'| + 1 > |A'| + 1 = |A|, contradicting optimality of A. \square

Properties & Summary

  • Greedy Algorithms
  • Greedy Choice
  • Algorithm & Correctness
  • Properties & Summary

Properties of Greedy Algorithms

  1. Greedy Choice Property
    • A globally optimal solution can be arrived at by making a locally optimal (greedy) choice.
  2. Optimal Substructure Property
    • An optimal solution to the problem contains within it optimal solutions to subproblems.

These two properties must be proved for every greedy algorithm.

Summary

  • Interval Scheduling is a classic problem solvable by a greedy algorithm.
  • Greedy choice: pick the interval with the earliest finish time.
  • Complexity: \Theta(n \log n) for sorting, \Theta(n) for selection.
  • Correctness relies on the greedy choice and optimal substructure properties.
  • This pattern extends to many interval scheduling variants.