Skip to content

greedy choices, and when they fail

why it works

what it is

A greedy method takes the step that looks best now, keeps it, and never goes back. This page shows when that gives the best answer, how to prove it, and when it fails.

Here are 5 jobs for one room. Each runs from the hour it starts to the hour it ends. Two jobs clash when they share some time: each starts before the other ends. Jobs that only touch, one ending as the next starts, do not clash. A day here is a set of jobs with no clash. Which rule fits in the most jobs?

earliest start first: 1 joblooks in order: A B C D EABCDE0123456789
earliest finish first: 2 jobslooks in order: B C D E AABCDE0123456789
shortest first: 2 jobslooks in order: B C D E AABCDE0123456789
The same 5 jobs, A to E, each on its own line, hours 0 to 9 below. A job taken is solid, a job left out dashed. Under each rule, the order it looks at the jobs. Earliest finish takes B and D, which only touch at 2.

Earliest start first takes A, from 0 to 6, and A clashes with every other job: 1 job. Earliest finish first takes B, then D: 2 jobs. Shortest first looks at B, 1 hour, then C, D and E, 2 hours each, then A. B clashes with C, and D with E, so it takes B and D too. Is 2 the most? A day with A holds A alone. A day holds at most one of B and C, which clash, and one of D and E. So 2 at most. A best day is one with the most.

Now a till with coins of 10, 7 and 1, and 14 to pay. The biggest coin that fits each time pays 10, then four 1s: 5 coins. The fewest is 7 and 7: 2 coins.

biggest coin first: 5 coins101111fewest coins: 2 coins77
Paying 14 with coins of 10, 7 and 1, the two ways one above the other.

Both pay 14, and nothing looks wrong. That is the danger: a greedy rule that fails still gives an answer, just not the best one. The table shows where these coins go wrong.

to paybiggest first: count, coinsfewest: count, coins
134: 10, 1, 1, 14: 10, 1, 1, 1
145: 10, 1, 1, 1, 12: 7, 7
156: 10 and five 1s3: 7, 7, 1
167: 10 and six 1s4: 7, 7, 1, 1
172: 10, 72: 10, 7
Coins of 10, 7 and 1. Right at 13, wrong at 14, 15 and 16, right again at 17, with no warning at either edge.

Australian coins are 5, 10, 20 and 50 cents, 1 dollar and 2 dollars. With them, the biggest coin first gave the fewest on each of the 400 amounts checked, from 5 cents to 20 dollars in 5-cent steps. One set of coins works and another fails, and you cannot tell which by looking. So a greedy rule needs a proof: a reason that holds for every input, not just the ones tried. Or it needs one input where it loses, which books call a counterexample. On the 3 jobs below, shortest first loses.

shortest first: 1 joblooks in order: B A CABC0123456789
earliest finish first: 2 jobslooks in order: A B CABC0123456789
3 jobs. Shortest first takes B, 2 hours, which clashes with both A and C. Earliest finish takes A and C.

Question 1 below proves that earliest finish is never beaten. Question 2 is about maps with lengths on their roads. A network keeps some of a map's roads, enough to travel from any place to any other, maybe through other places on the way. Its cost is its lengths added up, and a cheapest network costs as little as any. A map may have more than one: three places joined by three roads of length 1 have three, any two of the roads.

3421ABCDE
A network keeps some of a map's roads, here 4 of the 7 roads of the whole map drawn next. From any place you can travel to any other. Its cost is 3 + 4 + 2 + 1, which is 10.

Split the places into two groups, which books call a cut. A greedy rule: take the cheapest road across. On the map below, A and B against the rest, the roads across are 4, 6 and 7, so take A to C, 4. Question 2 proves that some cheapest network always holds that road.

3462517ABCDE
The whole map, 5 places and 7 roads, split into A and B, shaded, against C, D and E. Three roads cross, of 4, 6 and 7. The cheapest, A to C, is drawn heavy.

where the formulas come from

Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about nn, or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.

question 1A best day holds kk jobs. Swap its job that ends first for earliest finish's first job. How many jobs, SS, does it hold now, and does any clash?
  1. the move: one out, one in

    the line:
    S=(k−1)+1S = (k - 1) + 1
    a best day: 2 jobsABCDE0123456789
    after swap 1: 2 jobsABCDE0123456789
    after swap 2: 2 jobsABCDE0123456789
    A best day of the jobs at the top, C and E. Swap 1 puts B, earliest finish's first job, in for C. Swap 2 puts D, its second, in for E. No swap makes a clash.

    why it is allowed: The plan is to turn a best day into earliest finish's day, one swap at a time, never losing a job. One job goes out and one comes in. On the day at the top, C goes out and B comes in.

  2. the move: add them up

    the line:
    S=kS = k

    why it is allowed: One out and one in leaves kk. B and E are 2 jobs, as C and E were.

  3. the move: nothing clashes

    the line:
    the swapped day:no clash\begin{gathered} \text{the swapped day:} \\ \text{no clash} \end{gathered}
    a best day: 2 jobsABCDE0123456789
    after swap 1: 2 jobsABCDE0123456789
    Swap 1, B in for C. B and E fit.

    why it is allowed: B ends at 2, before C ends at 3. E starts at 3, so no earlier than B ends, and B and E fit. In general, the job put in ends first of all, so no later than the job taken out. Every other job of the best day ends no earlier than the one taken out, so it cannot sit wholly before it. As they do not clash, it starts no earlier than that job ends, so no earlier than the new one ends.

  4. the move: swap again

    the line:
    swap job 2, then 3,and so on\begin{gathered} \text{swap job 2, then 3,} \\ \text{and so on} \end{gathered}
    after swap 1: 2 jobsABCDE0123456789
    after swap 2: 2 jobsABCDE0123456789
    Swap 2, D in for E. B and D fit.

    why it is allowed: Both days now start with B, and only jobs starting at 2 or later can sit beside it. Each time, earliest finish takes the job ending first of those starting no earlier than its last job ends. Here that is D, ending at 4. E, the best day's second job, starts at 3, so it is one of them, and ends at 5. So D ends no later than E. D fits beside B, as it starts no earlier than B ends. Any later job of the best day starts no earlier than E ends, as in line 3, so no earlier than D ends. Each next job swaps the same way.

  5. the move: none left over

    the line:
    no best day beatsearliest finish\begin{gathered} \text{no best day beats} \\ \text{earliest finish} \end{gathered}

    why it is allowed: Earliest finish's jobs are a day too, so a best day holds at least as many, and the swaps never run out. Swap until every job of earliest finish is in. Any job left over in the best day ends no earlier than the last one swapped in, so, as in line 3, it starts no earlier than that job ends. Then earliest finish would have taken it too. So the best day has no more. At the top, after the swaps the day is B and D, and no job starts at 4 or later.

try it with a small kk: at k=2k = 2, line 1 gives S=2S = 2, which you can work out by hand

question 2Split the places in two. Why does some cheapest network hold the cheapest road across? Let ww be a network's cost, and TT its cost after one swap.
  1. the move: swap a road on the loop

    the line:
    T=w−6+4T = w - 6 + 4
    36214ABCDE
    A network of 12, A to B, B to D, C to D and D to E, with A to C added. The loop A, C, D, B, A, heavy, crosses back on B to D, of 6.

    why it is allowed: The plan is to show a swap on a network that is not the cheapest, then use it on a cheapest one. Take a network without A to C, the cheapest road across. A to B, B to D, C to D and D to E cost 3 + 6 + 2 + 1, so here ww is 12. They already join A to C another way, so adding A to C closes a loop, roads leading round and back to A. The loop leaves the shaded group on A to C, so it comes back on another road across, B to D, of 6. Take B to D out.

  2. the move: add them up

    the line:
    T=w−2T = w - 2

    why it is allowed: Taking out 6 and putting in 4 is 2 less.

  3. the move: no dearer

    the line:
    T≤wT \le w
    3421ABCDE
    After the swap, B to D out and A to C in, at 12 less 2, which is 10.

    why it is allowed: ww less 2 is at most ww, so the swap costs no more. Before, 12; after, 10.

  4. the move: still one piece

    the line:
    every placestill joined\begin{gathered} \text{every place} \\ \text{still joined} \end{gathered}

    why it is allowed: A trip that used the road taken out goes the other way round the loop. From B to D, that is B, A, C, D.

  5. the move: on any map

    the line:
    cost after:w−f+e\begin{gathered} \text{cost after:} \\ w - f + e \end{gathered}

    why it is allowed: On any map, let ee be the cheapest road across, and ff a road where the loop crosses back. Each letter also stands for its road's length. Here ee is A to C, of 4, and ff is B to D, of 6. The swap takes ff out and puts ee in.

  6. the move: f costs at least e

    the line:
    w−f+eis at most w\begin{gathered} w - f + e \\ \text{is at most } w \end{gathered}

    why it is allowed: ff crosses too, and ee is the cheapest road across, so ff is at least ee. Taking ff out saves at least what putting ee in adds.

  7. the move: start from a cheapest one

    the line:
    a cheapest networkswaps to one with e\begin{gathered} \text{a cheapest network} \\ \text{swaps to one with } e \end{gathered}

    why it is allowed: The network of 12 only showed the swap. Start instead from a cheapest network without ee. The swap costs no more, and no network costs less than a cheapest one, so it costs the same. So it is cheapest too, and holds ee. A cheapest network that holds ee already needs no swap.

try it with a small ww: at w=12w = 12, line 1 gives T=10T = 10, which you can work out by hand

a worked example

Run earliest finish first on the 5 jobs at the top, by hand.

  1. Put the jobs in order of when they end: B at 2, C at 3, D at 4, E at 5, A at 6.
  2. B comes first, and nothing is taken yet: take B. The jobs taken end at 2.
  3. C starts at 1, before 2: it clashes with B. Leave it out.
  4. D starts at 2, just as B ends: take it. The jobs taken end at 4 now.
  5. E starts at 3, before 4: leave it out. A starts at 0, before 4: leave it out too.
  6. B and D: 2 jobs, in one pass along the list.
earliest finish first: 2 jobslooks in order: B C D E AABCDE0123456789
Earliest finish first on the jobs at the top. It looks at them in the order they end, B, C, D, E, A, and takes each that clashes with none taken.

the trap

check yourself

  1. Jobs run from 2 to 4, 0 to 3 and 3 to 6. Which does earliest finish first take?

    the answer

    In order of when they end, 0 to 3, 2 to 4, then 3 to 6. Take 0 to 3. 2 to 4 starts at 2, before 3, so leave it out. 3 to 6 starts just as 0 to 3 ends, so take it. 2 jobs.

  2. Coins of 9, 6 and 1, and 12 to pay. What does the biggest coin first pay, and what is the fewest?

    the answer

    Biggest first takes 9, then pays the 3 left in 1s, 4 coins. The fewest is 6 and 6, 2 coins. One amount where it loses, so the biggest coin first is wrong for these coins.

  3. On the whole map of 7 roads, drawn before question 1, split the places into A, B and C against D and E. Which road must some cheapest network hold?

    the answer

    The roads across are B to D, 6, C to D, 2, and C to E, 5. The cheapest is C to D, so some cheapest network holds it. The network after question 2's swap holds it too.