A row of houses, and you know exactly how much money is in each one. The film's street has five houses, holding 2, 7, 9, 3, 1 in order.
Rob as many as you like, but never two next to each other, because adjacent neighbours notice. Robbing nothing is allowed.
What is the most you can take? And what do you do when the street has thirty houses instead of five?
Number the houses and write for the money in house . A plan for the first houses is a set of those houses with no two neighbours in it. Its total is the sum of the over the houses in it. Write for the largest total of any plan for the first houses. With no houses there is one plan, the empty one, so . It also helps to set , so that the rule below needs no special case at house 1.
This is the film's first route, and it is a fair one. Five houses have 32 subsets, 13 of which have no two neighbours. Total each of those and the biggest is 12, reached by one plan only: houses 1, 3, 5.
It works, and it stops working at once, for a reason we can prove.
Splitting the plans. Take houses and sort the plans for them by whether they rob house .
(For the second kind is the single plan , which is the empty plan with house 1 added.)
Counting. Let be the number of plans for houses. The split says
and (the empty plan), (leave it or rob it). With and each Fibonacci number the sum of the two before it, the first few are , , , so for and . If it holds for and then , so it holds for all by induction.
The film's sequence for one to ten houses is 2, 3, 5, 8, 13, 21, 34, 55, 89, 144. Each number is about 1.618 times the one before (the golden ratio, the limit of the ratio of neighbouring Fibonacci numbers), so a street twice as long has roughly the square as many plans. Thirty houses have 2,178,309 of them. Listing is hopeless, and the count that kills it obeys the same sort of rule as the answer.
Stand at house and ask whether to rob it. The split above says what each answer is worth.
The better of the two is . Here is the proof, since "the better of the two" hides two separate things that have to be true.
Let and for : this is the table the film fills in. We show for every , by strong induction on .
Base. and by definition.
Step. Take and suppose for every .
Hence for all , and in particular the answer to the puzzle is .
Getting the plan back. At each house, note which side of the maximum won. Starting at house , if robbing was strictly better, rob it and jump to house ; otherwise skip it and go to . The same induction shows this plan has total , and it has no neighbours because every robbery is followed by a jump of two.
The cost. Each step uses only and , so thirty houses take thirty steps, and you only ever hold two numbers. Compare that with 2,178,309 plans.
The tempting idea is to take every other house. That is a legal plan, so it can never beat the best, and it often falls short. On the film's trap street, 1, 9, 1, 1, 9, 1, taking houses 1, 3, 5 gives 11 and taking houses 2, 4, 6 gives 11, while the two nines, houses 2, 5, give 18. On the thirty houses the better of the two alternations gives 86, against the best of 97.
A greedy rule, "take the biggest house, strike its neighbours, repeat", fails too. On a street of three houses holding it takes the 6 and has to stop, for a total of 6. The two 5s give . This is not one of the film's streets, and the arithmetic is done here by hand rather than by the script. What goes wrong in both cases is the same: a choice at one house changes what is possible two houses away, and only the recurrence keeps track of that.
The houses hold 2, 7, 9, 3, 1. Row of the table is : take is , leave is , and the larger is best.
| House | Take | Leave | Best | |||
|---|---|---|---|---|---|---|
| 1 | 2 | 0 | 0 | 2 | 0 | 2 |
| 2 | 7 | 0 | 2 | 7 | 2 | 7 |
| 3 | 9 | 2 | 7 | 11 | 7 | 11 |
| 4 | 3 | 7 | 11 | 10 | 11 | 11 |
| 5 | 1 | 11 | 11 | 12 | 11 | 12 |
The first two rows use the convention . House 4 is the only one where leaving wins: robbing its 3 on top of the best from two houses back is worth 10, which is less than the 11 already in hand.
Walk back from house 5. Robbing it wins (12 against 11), so rob house 5 and jump to house 3. Robbing house 3 wins (11 against 7), so rob it and jump to house 1, where robbing wins again. The plan is houses 1, 3 and 5, worth 12. Route one, run on all 13 plans, finds the same plan and the same total.
The script scripts/burglar.mjs asserts the following before it writes the numbers this page uses.
This is a test, not a proof. The induction above is the proof, and the script confirms it on every case it can reach. Streets longer than 16 houses are never brute-forced, apart from the film's thirty, and the greedy counterexample and the variants under Generalising are not computed.
Because robbing house forbids house but nothing earlier. The best you can combine with house is a plan that stops at house , and that best is . Using instead would allow a plan that has already robbed house .
Solve two straight streets and take the larger answer: houses to , and houses to . Any plan on the ring leaves house 1 or leaves house (it cannot take both), so it is a plan on one of those two streets, and every plan on either street is a plan on the ring.
The best is 97 and the better of the two alternations is 86. Every-other is a legal plan, so it cannot be ahead, and the script asserts that it is strictly behind on this street.
Write yes for a house that is robbed and no for one that is not. A plan is a row of marks, and the rule says no two yeses are neighbours. So counts exactly those rows, and the split into "last mark is no" and "last mark is yes, so the one before is no" is the recurrence .