Interval scheduling
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
One room, many talks: pick as many as possible with no two overlapping. The rule "earliest start first" fails, but "earliest finish first" is always optimal. Watch six talks get sorted by finish time and scanned once, with a fence marking where the room becomes free, then see why it works: swap greedy's first pick into any best answer and repeat on what is left. The proof uses the exchange argument from "Greedy choices: the exchange argument and when greedy fails", the sort is the one from "Sorting", and the step from one pick to the rest is an induction, as in "Proofs and induction".
One room, many talks
Each bar is a talk: it starts at one hour and ends at another. There is one room, so two talks clash if their bars overlap. The goal: fit in as many talks as possible, with no clashes.
talks, in the order they arrive (start, finish): (5, 7) (0, 6) (8, 11) (1, 4) (6, 10) (3, 5) a talk is the half-open interval [s, f): [1, 4) and [4, 6) do not clash best possible here: 3 talks (checked over all 64 subsets)
اقرأ الدرس خطوة بخطوة
One room, many talks
Each bar is a talk: it starts at one hour and ends at another. There is one room, so two talks clash if their bars overlap. The goal: fit in as many talks as possible, with no clashes.
talks, in the order they arrive (start, finish): (5, 7) (0, 6) (8, 11) (1, 4) (6, 10) (3, 5) a talk is the half-open interval [s, f): [1, 4) and [4, 6) do not clash best possible here: 3 talks (checked over all 64 subsets)A tempting rule fails
Rule: take the talk that starts first. That is [0, 6), and it holds the room until 6. After it only [6, 10) fits. 2 talks, but 3 are possible. A greedy algorithm needs the right rule.
earliest start first: take [0, 6) room busy until 6 [1, 4) [3, 5) [5, 7) start before 6: clash take [6, 10) room busy until 10 [8, 11) starts at 8: clash result: 2 talks (best is 3)Sort by finish time
The rule that works: earliest finish first. Line 2 sorts the talks by finish time, so the rows now run from the talk that ends first to the one that ends last. Ending early leaves the most room for the rest.
talks.sort(key=lambda t: t[1]) key = the finish time f (1, 4) (3, 5) (0, 6) (5, 7) (6, 10) (8, 11) finish times: 4 ≤ 5 ≤ 6 ≤ 7 ≤ 10 ≤ 11Take the first to finish
last starts at 0. The first talk, [1, 4), starts at 1, and 1 ≥ 0, so it fits: add it and set last to its finish, 4. The fence marks last: a talk fits only if it starts at or after it.
last = 0 (1, 4): 1 >= 0 → take it out = [(1, 4)] last = 4Skip what clashes
[3, 5) starts at 3, before the fence at 4: it would overlap [1, 4), so skip it. [0, 6) starts at 0: skip it too. A skipped talk is never looked at again.
last = 4 (3, 5): 3 >= 4 is false → skip (0, 6): 0 >= 4 is false → skipTake the next that fits
[5, 7) starts at 5, and 5 ≥ 4: it fits after [1, 4). Add it and move the fence to 7. Because the talks are sorted, this is the talk that fits and ends soonest.
last = 4 (5, 7): 5 >= 4 → take it out = [(1, 4), (5, 7)] last = 7Done: 3 talks
[6, 10) starts at 6, before 7: skip. [8, 11) starts at 8 ≥ 7: take it. One pass, each talk checked once. The answer lane holds 3 talks side by side, with no overlap.
last = 7 (6, 10): 6 >= 7 is false → skip (8, 11): 8 >= 7 → take it last = 11 return [(1, 4), (5, 7), (8, 11)]Why it is safe: a swap
Take any best answer, say [3, 5), [5, 7), [8, 11). Its first talk ends at 5. Greedy's first ends at 4, no later, so swap it in: nothing new clashes and the count stays 3. Some best answer starts with greedy's pick.
a best answer O: [3, 5) [5, 7) [8, 11) greedy picked first: [1, 4) 4 ≤ 5, the finish of O's first talk so [1, 4) ends before [5, 7) starts O' = [1, 4) [5, 7) [8, 11): no clash, still 3Repeat on what is left
After the first pick, what remains is the same problem, smaller: the talks that start at 4 or later. The same swap works there, and after every pick. So greedy never falls behind a best answer: it is optimal.
after taking [1, 4), the rest of O' is a best answer for the talks with s ≥ 4: [5, 7) [6, 10) [8, 11) greedy on those talks is the same scan from last = 4 induction on the number of talks taken: greedy takes as many as a best answerCost: O(n log n)
Sorting n talks takes O(n log n) time; Python's sort is Timsort, a merge sort relative. The loop then checks each talk once: O(n). Total: O(n log n), or O(n) if the talks already come sorted by finish time.
line 2 sort O(n log n) lines 4-7 one pass O(n): n checks, ≤ n appends total O(n log n) extra space: the answer list, O(n)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01Talks [2, 5), [5, 8) and [4, 6). Which pairs clash (intervals are half-open)?
[2, 5) ends at 5 and [5, 8) starts at 5, so they only touch: no clash. [4, 6) overlaps both: it starts before 5 and ends after 5.
02Run earliest finish first on [1, 3), [2, 5), [4, 7), [6, 9), [8, 10). Which talks does it take?
Already sorted by finish. Take [1, 3), last = 3. [2, 5) starts at 2 < 3: skip. [4, 7): take, last = 7. [6, 9): 6 < 7, skip. [8, 10): take. Three talks.
03Why does earliest start first fail on the lesson's six talks?
[0, 6) starts first but holds the room until 6, which rules out [1, 4) and [5, 7). Only [6, 10) fits after it: 2 talks, while 3 are possible.
04In the swap proof, why can greedy's first pick replace the first talk of any best answer?
The rest of the best answer starts at or after its first talk's finish. Greedy's first pick finishes at or before that, so it ends before the rest starts. The count stays the same, so the new answer is also best.
05What is the running time of the algorithm on n talks given in any order?
The sort takes O(n log n) and the scan checks each talk once, O(n). The sort dominates. If the talks already come sorted by finish time, the scan alone is O(n).
المسرد
- Interval
- A stretch of time from a start s to a finish f. Here half-open, written [s, f): it includes s but not f.
- Clash (overlap)
- Two talks clash if each starts before the other finishes. Talks that only touch, like [1, 4) and [4, 6), do not clash.
- Compatible set
- A set of talks in which no two clash.
- Greedy algorithm
- An algorithm that builds its answer one choice at a time, always taking what looks best now, and never undoing a choice.
- Earliest finish first
- The greedy rule for interval scheduling: sort by finish time and take each talk that starts at or after the finish of the last one taken.
- Exchange argument
- A proof that swaps one choice of a best answer for the greedy choice and shows the answer stays valid and just as good.
- Optimal
- As good as any possible answer. Here: no compatible set has more talks.
الملاحظات والمصادر
- Talks are half-open intervals [s, f): a talk that ends at 4 and one that starts at 4 do not clash. That is why the test is s >= last and not s > last (MIT 6.046 and CLRS use the same convention).
- The proof follows MIT 6.046 (lecture 1) and CLRS §15.1 (§16.1 in the 3rd edition): swap the first talk of any best answer for the earliest-finishing talk, then apply the same argument to the talks that start after it, by induction.
- Other tempting rules fail too. Shortest talk first: with [0, 5), [4, 7) and [6, 11) it takes [4, 7), which clashes with both others, so it gets 1 talk instead of 2. MIT 6.046 also shows that "fewest clashes first" can fail.
- If talks have different values and you want the most total value, earliest finish first no longer works. That version, weighted interval scheduling, needs dynamic programming.
- The code is real Python 3: schedule([(5, 7), (0, 6), (8, 11), (1, 4), (6, 10), (3, 5)]) returns [(1, 4), (5, 7), (8, 11)]. list.sort sorts the list in place and is stable; last starts at 0 because every time here is 0 or more (use float("-inf") for any times).
- MIT 6.046J Design and Analysis of Algorithms (Spring 2015), Lecture 1 notes: Interval Scheduling
- Kevin Wayne, lecture slides for Kleinberg and Tardos, Algorithm Design: Greedy Algorithms I (Princeton)
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), §15.1 An activity-selection problem
- Sedgewick and Wayne, Algorithms 4th ed., §2.2 Mergesort
- Python documentation: Sorting Techniques
اختبار يتاح بعد الخطوة الأخيرة.