LibraryReference articles

Designing a plan for the worst case

Reference article

Someone thinks of a whole number from 1 to 100 and answers only higher or lower to each guess. Guessing the middle each time finds it in at most seven guesses, whatever number was chosen. A cleverer guess might win in one try on a lucky day, but its bad day is worse. When the number is chosen to hurt you, you plan for the bad day.

Two ways to judge a plan

Suppose a plan takes few steps on lucky inputs and many on unlucky ones. Is it good? That depends on what you are protecting against. Judging a plan by its average over all inputs is sensible when the input arrives at random. Judging it by its worst input is sensible when the input may be chosen after seeing the plan, or when one bad run is too costly to accept.

A plan chosen to make its worst case as small as possible is called a minimax plan: minimise the maximum. This page is about how to find one.

A guessing game

Someone picks a whole number from 1 to 100 and answers each guess with higher, lower or correct. How many guesses do you need to be certain?

Guessing 1, then 2, then 3 can take a hundred. Guessing the middle, 50, either is right or leaves at most 50 numbers. Guessing the middle of what remains roughly halves it again. In the worst case the numbers still in play after each guess are 50, 25, 12, 6, 3 and 1, so after six guesses at most one number is left and the seventh guess names it. Seven guesses are enough for every number.

Why halving is optimal here

Count what a fixed number of guesses can tell apart. One guess can settle one number: the one you name. Two guesses can settle three: the first guess, plus one number on each side of it that the second guess names. In general, k guesses can tell apart at most 1 + 2 + 4 + ... + 2^(k-1) = 2^k - 1 numbers, because each answer of higher or lower sends you down one of two branches. Six guesses can handle at most 63 numbers, fewer than 100, so six can never be certain. Seven can handle 127, so seven is the smallest number that is certain to work. Seven is a lower bound as well as a plan that achieves it.

Bar chart of Numbers settled for certain against Guesses allowed. Numbers settled. 1: 1; 2: 3; 3: 7; 4: 15; 5: 31; 6: 63; 7: 127. Reference line the 100 numbers at y = 100.Numbers settled for certain0501001501234567Guesses allowed137153163127the 100 numbersBar chart of Numbers settled for certain against Guesses allowed. Numbers settled. 1: 1; 2: 3; 3: 7; 4: 15; 5: 31; 6: 63; 7: 127. Reference line the 100 numbers at y = 100.Numbers settled for certain0501001501234567Guesses allowed137153163127the 100 numbers
Each extra guess doubles how many numbers a plan can settle for certain, so six guesses fall short of 100 numbers and seven clear it.

A good minimax argument has this shape: a plan that reaches a certain guarantee, and a reason nothing can guarantee more.

When the two kinds of mistake cost different amounts

Change the game. A guess that is too high costs three points, and a guess that is too low costs one. Now the middle is no longer right, because wasting a high guess is dearer. The worst-case plan guesses lower than the middle, so a miss on the high side leaves fewer numbers and a miss on the low side leaves more. The principle for finding the split is to balance: choose it so that the worst total cost along either branch comes out equal. If one branch were clearly worse, shifting the guess toward it would improve the worst case.

A small table

Another way to see the idea. Two plans, and two things the world might do. Plan A costs 4 if the world does X and 9 if it does Y. Plan B costs 6 either way. The worst case of A is 9 and of B is 6, so a minimax chooser takes B. A person who knows Y is rare might still take A, and that is a different, average-based choice. Neither is wrong: they answer different questions.

Where this breaks

Worst-case planning is cautious by design. If the bad input is extremely unlikely, a minimax plan can be much worse on ordinary inputs than a plan tuned for the average. It also assumes you can list the possibilities, and that the adversary really can see your plan. Say clearly which of these you are assuming.

How we know this landed

Two checks. With numbers 1 to 20, halving guarantees five guesses, since five guesses can sort out up to 31 numbers and four only up to 15. And a plan costing 6 in every case beats, on worst case, a plan costing 4 or 9. If the first is off, recount how many numbers each extra guess can separate.

In short

List what each plan does under the worst thing that could happen, and choose the plan whose worst case is smallest. Balance is the usual shape of the answer: split so the costly branches take equal effort. Check first that worst case is the right target, because on average a different plan can be better.

Related

States, moves, and finding the shortest route

Many puzzles are really a map: each situation a place, each allowed move a road. Breadth-first search finds the shortest route, worked on a number puzzle.

What something is worth on average

A shop's average sale, worked out twice, turns into the average value of a bet nobody has placed yet. Entirely in words, with arithmetic anybody can do on paper.

More on these topics

Topics

Also in the library