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 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.
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 pay | biggest first: count, coins | fewest: count, coins |
|---|---|---|
| 13 | 4: 10, 1, 1, 1 | 4: 10, 1, 1, 1 |
| 14 | 5: 10, 1, 1, 1, 1 | 2: 7, 7 |
| 15 | 6: 10 and five 1s | 3: 7, 7, 1 |
| 16 | 7: 10 and six 1s | 4: 7, 7, 1, 1 |
| 17 | 2: 10, 7 | 2: 10, 7 |
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.
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.
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.
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 , 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.
the move: one out, one in
the line: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.
the move: add them up
the line:why it is allowed: One out and one in leaves . B and E are 2 jobs, as C and E were.
guess it, thenthe move: nothing clashes
the line: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.
guess it, thenthe move: swap again
the line: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.
guess it, thenthe move: none left over
the line: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.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: swap a road on the loop
the line: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 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.
the move: add them up
the line:why it is allowed: Taking out 6 and putting in 4 is 2 less.
guess it, thenthe move: no dearer
the line:After the swap, B to D out and A to C in, at 12 less 2, which is 10. why it is allowed: less 2 is at most , so the swap costs no more. Before, 12; after, 10.
guess it, thenthe move: still one piece
the line: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.
guess it, thenthe move: on any map
the line:why it is allowed: On any map, let be the cheapest road across, and a road where the loop crosses back. Each letter also stands for its road's length. Here is A to C, of 4, and is B to D, of 6. The swap takes out and puts in.
guess it, thenthe move: f costs at least e
the line:why it is allowed: crosses too, and is the cheapest road across, so is at least . Taking out saves at least what putting in adds.
guess it, thenthe move: start from a cheapest one
the line:why it is allowed: The network of 12 only showed the swap. Start instead from a cheapest network without . 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 . A cheapest network that holds already needs no swap.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
a worked example
Run earliest finish first on the 5 jobs at the top, by hand.
- Put the jobs in order of when they end: B at 2, C at 3, D at 4, E at 5, A at 6.
- B comes first, and nothing is taken yet: take B. The jobs taken end at 2.
- C starts at 1, before 2: it clashes with B. Leave it out.
- D starts at 2, just as B ends: take it. The jobs taken end at 4 now.
- E starts at 3, before 4: leave it out. A starts at 0, before 4: leave it out too.
- B and D: 2 jobs, in one pass along the list.
the trap
check yourself
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.
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.
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.