Skip to content

Activity selection

planning ahead

Level 1 of 3

Also called Interval scheduling and Earliest finish time first.

test yourself

Six questions, four to pick from and two for paper.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

A studio takes five bookings. P runs from 3 to 6, Q from 2 to 4, R from 6 to 9, S from 0 to 7 and T from 1 to 3. Which does earliest finish first choose?

the answer, and why

the answer is A

In order of ends, T, Q, P, S, R. T is chosen, free from 3. Q starts at 2, so it is ruled out. P starts at 3, so it is chosen, free from 6. S is ruled out. R starts at 6, so it is chosen.

this tests how it works, on the learn page

if you chose another

  • That is earliest start first. S starts at 0 and runs till 7, so it clashes with all the rest.
  • That is shortest first. Q and T are 2 hours each, and Q, typed first, is taken first. Q then rules out T, P and S, and R fits after it.
  • T runs till 3 and Q starts at 2, so they clash.

question 2

These four activities are typed in this order: A from 0 to 1, B from 2 to 3, C from 1 to 2 and D from 3 to 4. The drawing lines them up by end. How many comparisons does the sort make?

012345678ACBDACBD
the answer, and why

the answer is A

Round 1: A with B, then C with D, 1 comparison each. Round 2: ends 1, 3 with 2, 4 compares 1 with 2, 3 with 2 and 3 with 4. That is 3, so 5 in all.

if you chose another

  • 4 is the drawing's order, ends 1, 2, 3, 4; the sort meets them as typed, 1, 3, 2, 4.
  • That counts only the checks after the sort, one for each activity after the first.
  • That counts round 2 as 4, but the last activity follows with no comparison.

question 3

Which list makes the sort compare the fewest times?

the answer, and why

the answer is A

Latest end first, every right run is used up first. 2 + 2 + 1, which is 5.

if you chose another

  • In end order, every left run is used up first, and a left run can be the longer. 2 + 2 + 4, which is 8.
  • That is the dearest order, 2 + 3 + 4, which is 9.
  • Round 1 puts each swapped pair in end order, 1 comparison each. That leaves end order's runs, so 2 + 2 + 4, which is 8.

question 4

A booking system takes 1,000 requests for one hall. Which is true, whatever order they come in?

the answer, and why

the answer is D

Every request after the first is checked once against the hour the hall is free from.

this tests best, worst and average, on the learn page

if you chose another

  • A request that clashes is still checked, then ruled out.
  • The order moves the comparisons, from 4,932 to 8,985 at 1,000.
  • The sort makes at least 4,932 comparisons, against 999 checks.

pen and paper

question 5about 6 minutes

A lab bench takes five bookings. A runs from 2 to 5, B from 0 to 3, C from 4 to 7, D from 3 to 4 and E from 6 to 8.

  1. Line them up by the hour each ends.
  2. Use earliest finish first: which are chosen and which ruled out?
  3. How many checks did you make?
a hint

Keep the hour the bench is free from. A booking fits if it starts at that hour or later.

a worked answer

By end: B at 3, D at 4, A at 5, C at 7, E at 8. B is chosen, free from 3. D starts at 3: chosen, free from 4. A starts at 2: ruled out. C starts at 4: chosen, free from 7. E starts at 6: ruled out. B, D and C are chosen, with 4 checks, one for each booking after B.

earliest finish first: 3 jobslooks in order: B D A C EABCDE0123456789
The five bookings, A to E. Earliest finish first takes B, D and C.

this tests how it works, on the learn page

what a full answer includes
  • B, D, A, C, E.
  • B, D and C chosen; A and E ruled out.
  • 4 checks.

question 6about 8 minutes

A hall's booking system sorts 1,024 requests with the watch page's merge sort, then uses earliest finish first.

  1. How many comparisons latest end first?
  2. How many in the dearest order?
  3. How many checks, in either order?
a hint

1,024 is ten 2s multiplied together, so no run ever waits. Questions 1 to 3 of the learn page's section 4 count each.

a worked answer

No run waits, and each of the 10 rounds' right runs hold half the requests, 512. Latest end first, each right run is used up first: 10 times 512, which is 5,120. In the dearest order, 1,024 times 10, less 0 waiting, less 1,023 for the merges: 9,217. The checks are 1,023 either way.

Question 1: how many comparisons, FF, does the sort make on nn activities typed latest end first, from 2 up?
  1. the move: how the sort goes

    the line:
    runs of 1,2,4,…merged in pairs\begin{gathered} \text{runs of } 1, 2, 4, \dots \\ \text{merged in pairs} \end{gathered}
    the row: 5, 4, 3, 2, 111111start221round 141round 25round 32a run, its length insideno partner, carried downthe first runall in one runa merge in round 2, its numbers45232345goes straight in, the other run used up
    5 numbers stand for 5 activities' ends, latest first. Round 2's merge, 4, 5 with 2, 3, compares 2 with 4, then 3 with 4. Then the right run is used up, and 4 and 5 follow with no comparison.
    roundrun lengthmergescomparisons
    1122
    2212
    3411
    all45
    Five activities, latest end first. In rounds 1 and 2 the last run waits.

    why it is allowed: The watch page's sort is merge sort, from the bottom up. Each activity starts as a run of 1. Each round merges neighbouring runs, comparing their front activities and taking the one that ends first. A last run with no partner waits. Every run is 1, 2, 4 and on long, except perhaps a round's last, so a right run is never the longer. Which runs meet depends only on nn, never on the order.

  2. the move: a merge's least

    the line:
    a merge compares at leastits right run’s length\begin{gathered} \text{a merge compares at least} \\ \text{its right run's length} \end{gathered}

    why it is allowed: A merge stops when one run is used up, and each activity of that run needed one comparison to leave it. By line 1, the right run is never the longer. So whichever run is used up holds at least the right run's length. For 4, 5 with 2, 3, at least 2 comparisons.

  3. the move: latest end first, the right run is used up first

    the line:
    F=b1+b2+b4+…F = b_1 + b_2 + b_4 + \dots
    run lengthright runscomparisons
    15 of 15
    22 of 24
    41 of 44
    81 of 22
    all15
    Ten activities, latest end first. At run length 2 the last run waits, and at 4 the last 2 activities wait. At 8 the right run holds only those 2.

    why it is allowed: Latest end first, each right run's activities end before the left run's beside it. So the right run is used up first, one comparison each, the least line 2 allows. b1b_1 adds up the right runs in the round of runs of 1, b2b_2 of runs of 2, and so on. For 5, 2 + 2 + 1.

  4. the move: a ceiling

    the line:
    F≤n2⌈log⁡2 n⌉F \le \frac{n}{2} \lceil \log_2\, n \rceil
    numberlog× 2+ 11021428316452.32103.32
    Top, numbers, each double the last; bottom, their logs, each 1 more. 5 lies between 4 and 8, so its log, about 2.3, rounds up to 3. 10 lies between 8 and 16, so its log, about 3.3, rounds up to 4.

    why it is allowed: In a pair the right run is never the longer, so a round's right runs hold at most half the activities, n2\frac{n}{2}. Run lengths double until one run holds all nn, so there are ⌈log⁡2 n⌉\lceil \log_2\, n \rceil rounds. Multiply the two. For 5, half of 5 times 3 rounds is 7.5, and FF is 5.

  5. the move: a simpler ceiling

    the line:
    F≤n log⁡2 nF \le n \,\log_2\, n

    why it is allowed: Rounding up adds less than 1, and from 2 up 1 is at most log⁡2 n\log_2\, n. So ⌈log⁡2 n⌉\lceil \log_2\, n \rceil is at most twice log⁡2 n\log_2\, n, and n2\frac{n}{2} times twice log⁡2 n\log_2\, n is n log⁡2 nn \,\log_2\, n. For 5, FF is 5, at most 11.6.

  6. the move: read the bound

    the line:
    F∈O(n log⁡2 n)F \in O(n \,\log_2\, n)

    why it is allowed: By line 5, with the fixed multiple 1.

  7. the move: a floor

    the line:
    F≥n4⌊log⁡2 n⌋F \ge \frac{n}{4} \lfloor \log_2\, n \rfloor
    run lengthfull pairs holdtheir right runsa quarter of 5
    1421.25
    2421.25
    Five activities. In each round with a full pair, the full pairs hold 4 of the 5, at least half, and their right runs 2, at least a quarter. 2 such rounds, so at least 2.5.

    why it is allowed: Take a round whose runs are rr long, with 2r2r at most nn, so at least one full pair of runs fits. What is left over is shorter than a pair, so the full pairs hold at least half the activities, and their right runs at least a quarter, n4\frac{n}{4}. Such rounds have rr as 1, 2, 4 and on, while 2r2r is at most nn, so there are ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor of them. By line 3, FF adds up every round's right runs, so multiply the two. For 5, rounds of 1 and 2, so at least 2.5.

  8. the move: the rounded log against the exact one

    the line:
     log⁡2 n is below j+1,at most 2j\begin{gathered} \,\log_2\, n \text{ is below } j + 1, \\ \text{at most } 2j \end{gathered}

    why it is allowed: Call line 7's rounded-down log jj. Rounding down took off less than 1, so log⁡2 n\log_2\, n is below j+1j + 1. From 2 up jj is at least 1, so j+1j + 1 is at most 2j2j. For 5, jj is 2, and the log of 5, about 2.3, is below 3, at most 4.

  9. the move: drop the rounding

    the line:
    F≥n log⁡2 n8F \ge \frac{n \,\log_2\, n}{8}

    why it is allowed: By line 8, jj is at least half of log⁡2 n\log_2\, n. Put that into line 7. For 5, FF is 5, at least 1.45.

  10. the move: read the bound

    the line:
    F∈Ω(n log⁡2 n)F \in \Omega(n \,\log_2\, n)

    why it is allowed: By line 9, with the fixed multiple an eighth.

  11. the move: both together

    the line:
    F∈Θ(n log⁡2 n)F \in \Theta(n \,\log_2\, n)

    why it is allowed: Lines 6 and 10 bound FF by the same growth.

try it with a small nn: at n=5n = 5, line 3 gives F=5F = 5, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • 5,120.
  • 9,217.
  • 1,023.