PuzzleReels
Sections

The Burglar's Street

The maths · Episode 6 · 2:14 · gives the answer

The puzzle

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?

The maths, properly

Number the houses 1,2,…,n and write vi for the money in house i. A plan for the first i houses is a set of those houses with no two neighbours in it. Its total is the sum of the vj over the houses in it. Write f(i) for the largest total of any plan for the first i houses. With no houses there is one plan, the empty one, so f(0)=0. It also helps to set f(−1)=0, so that the rule below needs no special case at house 1.

Route one: list every plan

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 i≥1 houses and sort the plans for them by whether they rob house i.

  • A plan that leaves house i is exactly a plan for the first i−1 houses.
  • A plan that robs house i cannot contain house i−1. Take house i away and what is left is a plan for the first i−2 houses. Going back the other way, add house i to any plan for the first i−2 houses: the nearest house in it is at most i−2, so the result has no neighbours. So the plans that rob house i correspond one to one with the plans for the first i−2 houses.

(For i=1 the second kind is the single plan {1}, which is the empty plan with house 1 added.)

Counting. Let p(n) be the number of plans for n houses. The split says

p(n)=p(n−1)+p(n−2)(n≥2)

and p(0)=1 (the empty plan), p(1)=2 (leave it or rob it). With F1=F2=1 and each Fibonacci number the sum of the two before it, the first few are F2=1, F3=2, F4=3, so p(n)=Fn+2 for n=0 and n=1. If it holds for n−1 and n−2 then p(n)=Fn+1+Fn=Fn+2, so it holds for all n 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.

Route two: one question per house

Stand at house i and ask whether to rob it. The split above says what each answer is worth.

  • Leave it. The best you can do is the best for the first i−1 houses, f(i−1).
  • Rob it. You get vi, and house i−1 is out, so the best you can add is f(i−2).

The better of the two is f(i). Here is the proof, since "the better of the two" hides two separate things that have to be true.

Let B(−1)=B(0)=0 and B(i)=max(B(i−1),B(i−2)+vi) for i≥1: this is the table the film fills in. We show B(i)=f(i) for every i≥−1, by strong induction on i.

Base. B(−1)=f(−1) and B(0)=f(0) by definition.

Step. Take i≥1 and suppose B(j)=f(j) for every j<i.

  • f(i)≤B(i). Let a plan for the first i houses be optimal. If it leaves house i, it is a plan for the first i−1 houses, so its total is at most f(i−1)=B(i−1). If it robs house i, take i away: what remains is a plan for the first i−2 houses, so its total is at most f(i−2)=B(i−2), and the whole plan is worth at most B(i−2)+vi. Either way the total is at most B(i).
  • f(i)≥B(i). Both numbers inside the maximum can be achieved. An optimal plan for the first i−1 houses is also a plan for the first i, worth f(i−1). An optimal plan for the first i−2 houses, with house i added, has no neighbours (as above) and is worth f(i−2)+vi. So a plan worth B(i) exists.

Hence B(i)=f(i) for all i, and in particular the answer to the puzzle is B(n).

Getting the plan back. At each house, note which side of the maximum won. Starting at house n, if robbing was strictly better, rob it and jump to house i−2; otherwise skip it and go to i−1. The same induction shows this plan has total B(n), and it has no neighbours because every robbery is followed by a jump of two.

The cost. Each step uses only B(i−1) and B(i−2), so thirty houses take thirty steps, and you only ever hold two numbers. Compare that with 2,178,309 plans.

Why no fixed rule will do

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 5,6,5 it takes the 6 and has to stop, for a total of 6. The two 5s give 5+5=10. 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.

A worked case: the film's five houses

The houses hold 2, 7, 9, 3, 1. Row i of the table is B(i): take is B(i−2)+vi, leave is B(i−1), and the larger is best.

House iviB(i−2)B(i−1)TakeLeaveBest
1200202
2702727
392711711
43711101111
511111121112

The first two rows use the convention B(−1)=B(0)=0. 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.

Generalising

  • Any values. Nothing in the proofs used the size of the vi, only that a plan with no neighbours is allowed and the empty plan is one. Streets with a house worth nothing, or a street of one house, are covered.
  • A ring of houses. If the first and last houses are neighbours, either house 1 is left, which is a straight street of houses 2,…,n, or house n is left, which is a straight street of houses 1,…,n−1. Every plan leaves at least one of them, so the answer is the larger of two straight-street answers. The verifier does not run this variant.
  • A longer gap. If you must leave k houses between robberies, the same split works with f(i−k−1) in place of f(i−2). Also not run by the verifier.
  • The same count in other costumes. A plan is a row of yes and no with no two yeses together. Tiling a strip with squares and dominoes, or climbing stairs one or two at a time, obey p(n)=p(n−1)+p(n−2) for the same reason.

Checking it

The script scripts/burglar.mjs asserts the following before it writes the numbers this page uses.

  • All 32 subsets of the five houses are tried. The 13 legal plans are counted and totalled, the best is 12, and one plan reaches it.
  • The recurrence, with its take, leave and best columns, matches that brute force, and the traceback recovers houses 1, 3, 5.
  • Plan counts are enumerated directly for every n from 1 to well beyond the film's ten, and equal Fn+2, and each count is the sum of the two before it.
  • On the trap street, brute force confirms that both alternations give 11, and that 18 is the true best, reached only by houses 2, 5.
  • On the thirty-house street all 2,178,309 plans are enumerated, the largest total equals the recurrence's 97, the traceback has no neighbours, two running totals reach the same answer as the whole table, and every-other falls short.
  • 5000 seeded random streets of up to 16 houses agree between brute force over every subset, the recurrence and its traceback.
  • The narration's number words are the verified figures.

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.

Where this shows up

  • The house robber problem. This exact puzzle is a standard first exercise in dynamic programming, where it is usually met as an array of non-negative numbers with no two adjacent picks.
  • Independent sets on a path. Houses are the nodes of a path and neighbours are joined by an edge. A plan is a set of nodes with no edge inside it, and f is the largest total weight of such a set. The same idea, working from the leaves up, handles trees.
  • Overlapping subproblems. A direct recursion on "rob or leave house i" calls itself twice and solves the same shorter streets again and again. Storing each f(i) once is what turns an exponential search into a linear one.
  • Fibonacci counting. Strings of zeros and ones with no two ones together are counted by the same p(n)=p(n−1)+p(n−2).

Try it yourself

1. Why does the recurrence look two houses back and not one?

Because robbing house i forbids house i−1 but nothing earlier. The best you can combine with house i is a plan that stops at house i−2, and that best is f(i−2). Using f(i−1) instead would allow a plan that has already robbed house i−1.

2. How would you handle a ring of houses?

Solve two straight streets and take the larger answer: houses 2 to n, and houses 1 to n−1. Any plan on the ring leaves house 1 or leaves house n (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.

3. On the thirty houses, how far ahead of every-other is the best?

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.

4. Show that p(n) is the number of rows of n yeses and noes with no two yeses together.

Write yes for a house that is robbed and no for one that is not. A plan is a row of n marks, and the rule says no two yeses are neighbours. So p(n) 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 p(n)=p(n−1)+p(n−2).