# Solving the “princess on a graph” puzzle

This post is about the best logic puzzle I’ve seen in absolutely ages. It’s easy to explain and doesn’t require any silly tricks, but has a decent amount of depth to it and challenges your intuition. I’m going to present the puzzle, an extended version of the puzzle, and a general solution to the extended version.

The “classic” puzzle was told to me by David Cushing, who was told it by Alistair Bird, who himself found it on the MathOverflow “math puzzles for dinner” thread.

The setup goes like this:

- there is a castle with 17 rooms in it, arranged in a line.
- the princess who lives in the castle sleeps in a different room each night, but always one adjacent to the one she slept in the previous night. She is free pick any room to sleep in on the first night.
- a prince would like to find the princess, but she will not tell him where she is going to sleep each night.
- the prince can look in a single room each night, with no other restrictions

And the puzzle is:

*Is there a strategy the prince can follow to guarantee he looks in the room the princess is sleeping in within a finite number of days?*

If you haven’t seen this puzzle before, look away from the screen now and spend some time working out the solution. Try starting with castles of just a few rooms, and see if you can spot a pattern in the winning strategies. This version of the puzzle is definitely solvable by just sitting and thinking for a while.

I’ve created an interactive version of the puzzle to help with trying out strategies. The interactive version keeps track of where the princess can be at any point in time. Can you spot a pattern in the set of rooms the Princess can be in at each stage of the strategy?

If you have seen this puzzle before, or if you’ve given up (don’t give up! Have another go, then come back here. Finding the correct solution is much more satisfying when you’ve tried lots of other ways), here are the winning strategies for several different numbers of rooms, for reference. I’ve put links to the relevant layout in the interactive puzzle at the bottom of each solution.

### 4 rooms in a line.

The prince should pick

Click here to try the interactive version.

$5$ rooms in a line.

The prince should pick

Click here to try the interactive version.

### 17 rooms in a line

The prince should pick

Click here to try the interactive version.

These aren’t the only solutions to the puzzle. Are they the shortest? Why?

### General solution

For a castle with

Andrew Lobb came up with a very good explanation of why this works:

As a first go at a strategy, pick the rooms

Think about the rooms as either *even* or *odd*. The princess must move from an even room to an odd room, or from an odd room to an even room, each night. So if the princess swapped places with you at some point, she must have started with the opposite kind of room to you. So staying in room

My solution doesn’t bother with

## Princess on a graph

So that’s the classic puzzle solved. David thought he would like more to do, so he asked: *what if the rooms are laid out in any arrangement, not necessarily a line?*

That is, if the layout is an arbitrary graph, can you still find a winning strategy? What does it look like?

That’s a very good question! The general solution we had for straight lines doesn’t work because it needs a path straight through all the vertices, which you can’t make in general.

### Simple graphs

The first thing we did was to play with some simple examples to get an idea for how the puzzle works. First, we tried a binary tree with three levels. That’s solvable: one solution is

What about a triangle? It isn’t solvable – no matter what you do, the princess always has two rooms to choose from.

We were fairly sure that all binary trees could be solved. Certainly,

David came up with a couple of conjectures:

- if a graph
contains an unsolvable subgraph,$G$ is unsolvable.$G$ - every tree is solvable.

Only one of those is true!

### MathsJam

This was as far as we got before I brought the puzzle up at MathsJam in November.

Matthew Taylor, a student at Durham, started looking at graphs with linear spokes coming out a central vertex. The first two of these are fairly easy to solve: the one with three spokes of length 1 has solution

In the blog, Matthew claimed that any graph consisting of a central vertex with spokes of length

Interestingly, the graph made by extending only two spokes to length

So Matthew asked, can we find a property of graphs which tells us whether there is or is not a solution? This was the insight that set us on the way to a solution.

## The solution!

I’ll stop the rambling narrative now and deal out some straightforward maths. I’ll define my terms, give an algorithm which finds the shortest winning strategy for a graph, if one exists, then I’ll tell you which graphs are solvable.

### Definitions

Let

The prince’s strategy is a sequence

Each night, before the prince makes his move, we can consider the state of each room: could the princess currently be in that room, or not? A room is *occupiable* on a particular night if there is a sequence of rooms the princess can choose which puts her in that room on that night, and differs from the prince’s strategy every previous night. If there’s no such sequence, the room is *unoccupiable* on that night.

Mark rooms which are *occupiable* with a *not occupiable* with an *state* is a

Here is how the puzzle works in this language:

- On the first night, mark all rooms with
, let$Y$ , then repeat the following:$i=1$ - Mark room
with${V}_{i}$ .$N$ - To construct the new state of the graph: for each vertex in
, mark it with$G$ if there is an adjacent vertex marked$Y$ in the previous night’s state; otherwise, mark it with$Y$ .$N$ - If all vertices are now marked
, the princess must have been caught on this night or a previous one no matter what she did, so the strategy$N$ is a winning strategy. Otherwise, if one or more rooms are marked${V}_{1},\dots ,{V}_{i}$ , let$Y$ and return to step 2.$i:=i+1$

This is just a representation of the rules of the game; it does not give us a way of finding winning strategies.

### An algorithm to find the shortest winning strategy (if one exists)

Notice that there are only finitely many states a given graph can be in. So finding a winning strategy consists of finding a path from the state with all

Make a couple of observations:

- Any winning strategy prepended with arbitrarily many moves will still be a winning strategy. I’d like to find the shortest winning strategy.
- If I have a strategy
and there exists a pair${V}_{1},{V}_{2},\dots $ such that the state of the graph on night$({V}_{i},{V}_{j}),i<j,$ is the same as the state of the graph on night$i$ , then the moves$j$ are redundant.${V}_{i}+1,\dots ,{V}_{j}$

But when are two states the same? If every vertex has the same

This extends to all symmetries of the graph. For example, this state

is really the same as this one

and these states are all equivalent:

So here’s a recursive procedure for finding the shortest winning strategy for a graph:

- The initial state has all rooms marked
.$Y$ - For each vertex
marked$V$ in the current state, compute the state the graph would be in after looking in$Y$ . If that state has been seen before (modulo the symmetries of the graph), then there are redundant moves in the strategy, so looking in$V$ now is not part of the shortest strategy. If the state hasn’t been seen before, perform this step on the new state.$V$ - Of the rooms looked at in step 2 which didn’t return
`null`

, return the room which produced the shortest strategy. If no rooms produced new states, return`null`

.

The stuff about symmetries is just an optimisation – the above algorithm will always produce the shortest strategy even without the symmetry check, but it’s a good optimisation.

This procedure will return `null`

iff the graph is unsolvable. But it doesn’t tell us much about which graphs are solvable.

### So which graphs *are* solvable?

Some facts:

- David’s first conjecture, that graphs which contain unsolvable subgraphs are themselves unsolvable is clearly true – the princess can restrict her movements to the unsolvable subgraph and there is no strategy which will find her.
- Now note that the prince’s first move must be to look in a vertex with a neighbour of degree 1. Otherwise, every room remains occupiable on the second night.
- So all cycles are unsolvable – they have no vertex of degree 1.
- Facts 1 and 3 together imply that only trees can be solvable – if a graph contains a cycle, then it is unsolvable.

But which trees are solvable? David conjectured that they all are. Well, the algorithm I gave above shows that the graph with three spokes of length three joined to a central vertex (aka “threesy”) is unsolvable. Because it would take ages to describe every step, here’s a handwavey video:

So only graphs not containing “threesy” can be solvable.

What do they look like?

Let *threesy*. We can find the longest path through *spine*. Now, every tree attached to the spine must have depth at most *threesy*. Also note that vertices

Every such

Walk along the vertices

- look in vertex
${v}_{i}$ - for each vertex
neighbouring${v}^{\prime}$ but not in the spine, if${v}_{i}$ has degree${v}^{\prime}$ , look in$>1$ and then in${v}^{\prime}$ again.${v}_{i}$

Once you have finished the process for

The reason this strategy works is the same as the reason Andrew’s strategy for straight-line castles works: on the forwards half of the strategy you are ensuring that the princess is either ahead of you or in rooms of the opposite parity to you. Once you’ve reached the end, repeating the strategy backwards puts you on the same parity as the princess, so you’re bound to find her.

So the set of solvable graphs is exactly the set of trees not containing *threesy*. How satisfying!

I expect to edit this post as people ask for bits to be clarified, but I really wanted to share what we’ve found, even if informally. This has been a very satisfying investigation!

## Comments

## Comments

Peter

Great post!

Any chance of tagging this Mathoverflow or PlanetMO so that it can be found through http://www.mathblogging.org/planetmo ?

christianp

OK, I’ve done that.

And thanks to that page, I see you’ve already found someone else talking about the original version of the problem.

Peter

I guess somebody else like it to 🙂 (and I guess I should have remembered since I left a comment there…)

But your post is slightly more involved 😉

Non-mathematician

I think larger binary trees are unsolvable.

I tried to solve a binary tree with 5 layers.

My tree has the same 15 vertices as yours but vertex number 1 connects upwards to a new vertex (called 0) above it. (It is easier to visualise if you draw a picture, but I’m not up to doing that on the computer)

I started by trying to ‘clear’ the princess out of vertices 1 to 15 with the same sequence (4,2,5,2,1,3,6,3,7,7,3,6,3,1,2,5,2,4) that works for 4 layers.

However, I think that, if the princess comes from somewhere in the ‘new’ part of the tree via vertex 0 after I have crossed vertex 1 for the first time in my sequence she is able to remain undetected.

For example

My position: 4,2,5,2,1,3,6,3,7,7,3,6,3,1,2,5,2,4

Her position: 0,1,2,4,2,1,2,4,8,4,8

In fact there are several opportunities to enter and remain undetectable. She can enter vertex 1 when I am in any of the starred rooms:

My position: 4,2,5,2,1,3,6*,3,7*,7,3*,6,3*,1,2*,5,2*,4.

It helped me to think of 5 rows of the tree as having alternating colours (black and white) and to think that when the princess I were both in the same colour room we had the same ‘parity’. To defeat me the princess needs to have the opposite parity to me after I have completed my double stay at vertex 7.

Do you agree that a 5 row binary tree is unsolvable?

Christian Perfect

Thanks for your comment, Non-mathematician!

The

full5-row binary tree is unsolvable, but the one you gave with just an extra vertex attached to the topissolvable.In fact, the solution I gave for the full 4-row tree works for your one, too. Have you tried the interactive puzzle I made?

The strategy you gave for the princess is not as long as the strategy I gave to find her. She needs to evade capture for the whole time!

You’re on the right track with the parity argument though – that was Andrew Lobb’s explanation of the original puzzle.

In your strategy for the princess, she does start on the opposite parity to you. So after your double stay at vertex 7, the Princess’s room has the same parity as yours.

By the way, the reason the full 5-row binary tree is unsolvable is because it contains a copy of the graph ‘threesy’. All the Princess needs to do to avoid capture is to stay inside ‘threesy’.

Christian Perfect

Looks like some comments got lost when I moved this site to a new server. Someone asked for more detail on proving that ‘threesy’ is unsolvable, so I recorded this video explaining how the algorithm goes: http://www.youtube.com/watch?v=4fudkLW9pC4

Tom Verhoeff

Nice post. Thanks.

To eliminate some of the luck involved in the original game, I have reformulated it a bit (for kids in my enrichment class, see vampire-hunt.pdf): The prince actually announces the room he will enter

beforethe princess makes her choice. That way, it is more challenging to play, because the princess will never be found by accident.Then, in all graphs with a cycle, the princess can always avoid discovery. In all trees without a ‘threesy’, the prince can always find the princess.

But trees with a ‘threesy’ are different. The prince cannot force an encounter, but he could be lucky. The princess does not have a guaranteed evasive strategy; she could be out of luck. It is interesting to investigate how the prince can maximize his odds of finding the princess, and how the princess can minimize the odds of being found.

Christian Perfect

That’s a very good idea. I like the “game forms” you’ve made! I think I’ll print some out and try it out on the new students at work when they start.