ENع
→Interval scheduling
الدرس 27 · Greedy and divide and conquer

Interval scheduling

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةAdvanced
عن هذا الدرس

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

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 27 · تفاعلي
01 / 10

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)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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)
  2. 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)
  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 ≤ 11
  4. Take 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 = 4
  5. Skip 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 → skip
  6. Take 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 = 7
  7. Done: 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)]
  8. 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 3
  9. Repeat 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 answer
  10. Cost: 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)

اختبار يتاح بعد الخطوة الأخيرة.