# Sprague–Grundy theorem

In combinatorial game theory, the Sprague–Grundy theorem states that every impartial game under the normal play convention is equivalent to a one-heap game of nim, or to an infinite generalization of nim. It can therefore be represented as a natural number, the size of the heap in its equivalent game of nim, as an ordinal number in the infinite generalization, or alternatively as a nimber, the value of that one-heap game in an algebraic system whose addition operation combines multiple heaps to form a single equivalent heap in nim.

The Grundy value or nim-value of any impartial game is the unique nimber that the game is equivalent to. In the case of a game whose positions are indexed by the natural numbers (like nim itself, which is indexed by its heap sizes), the sequence of nimbers for successive positions of the game is called the nim-sequence of the game.

The Sprague–Grundy theorem and its proof encapsulate the main results of a theory discovered independently by R. P. Sprague (1936)[1] and P. M. Grundy (1939).[2]

## Definitions

For the purposes of the Sprague–Grundy theorem, a game is a two-player sequential game of perfect information satisfying the ending condition (all games come to an end: there are no infinite lines of play) and the normal play condition (a player who cannot move loses).

At any given point in the game, a player's position is the set of moves they are allowed to make. As an example, we can define the zero game to be the two-player game where neither player has any legal moves. Referring to the two players as ${\displaystyle A}$ (for Alice) and ${\displaystyle B}$ (for Bob), we would denote their positions as ${\displaystyle (A,B)=(\{\},\{\})}$, since the set of moves each player can make is empty.

An impartial game is one in which at any given point in the game, each player is allowed exactly the same set of moves. Normal-play nim is an example of an impartial game. In nim, there are one or more heaps of objects, and two players (we'll call them Alice and Bob), take turns choosing a heap and removing 1 or more objects from it. The winner is the player who removes the final object from the final heap. The game is impartial because for any given configuration of pile sizes, the moves Alice can make on her turn are exactly the same moves Bob would be allowed to make if it were his turn. In contrast, a game such as checkers is not impartial because, supposing Alice were playing red and Bob were playing black, for any given arrangement of pieces on the board, if it were Alice's turn, she would only be allowed to move the red pieces, and if it were Bob's turn, he would only be allowed to move the black pieces.

Note that any configuration of an impartial game can therefore be written as a single position, because the moves will be the same no matter whose turn it is. For example, the position of the zero game can simply be written ${\displaystyle \{\}}$, because if it's Alice's turn, she has no moves to make, and if it's Bob's turn, he has no moves to make either. A move can be associated with the position it leaves the next player in.

Doing so allows positions to be defined recursively. For example, consider the following game of Nim played by Alice and Bob.

### Example Nim Game

Sizes of heaps  Moves
A B C

1 2 2           Alice takes 1 from A
0 2 2           Bob   takes 1 from B
0 1 2           Alice takes 1 from C
0 1 1           Bob   takes 1 from B
0 0 1           Alice takes 1 from C
0 0 0           Bob   has no moves, so Alice wins
• At step 6 of the game (when all of the heaps are empty) the position is ${\displaystyle \{\}}$, because Bob has no valid moves to make. We name this position ${\displaystyle *0}$.
• At step 5, Alice had exactly one option: to remove one object from heap C, leaving Bob with no moves. Since her move leaves Bob in position ${\displaystyle *0}$, her position is written ${\displaystyle \{*0\}}$. We name this position ${\displaystyle *1}$.
• At step 4, Bob had two options: remove one from B or remove one from C. Note, however, that it didn't really matter which heap Bob removed the object from: Either way, Alice would be left with exactly one object in exactly one pile. So, using our recursive definition, Bob really only has one move: ${\displaystyle *1}$. Thus, Bob's position is ${\displaystyle \{*1\}}$.
• At step 3, Alice had 3 options: remove two from C, remove one from C, or remove one from B. Removing two from C leaves Bob in position ${\displaystyle *1}$. Removing one from C leaves Bob with two piles, each of size one, i.e., position ${\displaystyle \{*1\}}$, as described in step 4. However, removing 1 from B would leave Bob with two objects in a single pile. His moves would then be ${\displaystyle *0}$ and ${\displaystyle *1}$, so her move would result in the position ${\displaystyle \{*0,*1\}}$. We call this position ${\displaystyle *2}$. Alice's position is then the set of all her moves: ${\displaystyle {\big \{}*1,\{*1\},*2{\big \}}}$.
• Following the same recursive logic, at step 2, Bob's position is
${\displaystyle {\big \{}\{*1,\{*1\},*2\},*2{\big \}}.}$
• Finally, at step 1, Alice's position is
${\displaystyle {\Big \{}{\big \{}*1,\{*1\},*2{\big \}},{\big \{}*2,\{*1,\{*1\},*2\}{\big \}},{\big \{}\{*1\},\{\{*1\}\},\{*1,\{*1\},*2\}{\big \}}{\Big \}}.}$

### Nimbers

The special names ${\displaystyle *0}$, ${\displaystyle *1}$, and ${\displaystyle *2}$ referenced in our example game are called nimbers. In general, the nimber ${\displaystyle *n}$ corresponds to the position in a game of nim where there are exactly ${\displaystyle n}$ objects in exactly one heap. Formally, nimbers are defined inductively as follows: ${\displaystyle *0}$ is ${\displaystyle \{\}}$, ${\displaystyle *1=\{*0\}}$, ${\displaystyle *2=\{*0,*1\}}$ and for all ${\displaystyle n\geq 0}$, ${\displaystyle *(n+1)=*n\cup \{*n\}}$.

While the word nimber comes from the game nim, nimbers can be used to describe the positions of any finite, impartial game, and in fact, the Sprague–Grundy theorem states that every instance of a finite, impartial game can be associated with a single nimber.

### Combining Games

Two games can be combined by adding their positions together. For example, consider another game of nim with heaps ${\displaystyle A'}$, ${\displaystyle B'}$, and ${\displaystyle C'}$.

#### Example Game 2

Sizes of heaps    Moves

A' B' C'
1  1  1           Alice takes 1 from A'
0  1  1           Bob takes one from B'
0  0  1           Alice takes one from C'
0  0  0           Bob has no moves, so Alice wins.

We can combine it with our first example to get a combined game with six heaps: ${\displaystyle A}$, ${\displaystyle B}$, ${\displaystyle C}$, ${\displaystyle A'}$, ${\displaystyle B'}$, and ${\displaystyle C'}$:

#### Combined Game

Sizes of heaps     Moves
A  B  C  A' B' C'

1  2  2  1  1  1   Alice takes 1 from A
0  2  2  1  1  1   Bob takes 1 from A'
0  2  2  0  1  1   Alice takes 1 from B'
0  2  2  0  0  1   Bob takes 1 from C'
0  2  2  0  0  0   Alice takes 2 from B
0  0  2  0  0  0   Bob takes 2 from C
0  0  0  0  0  0   Alice has no moves, so Bob wins.

To differentiate between the two games, for the first example game, we'll label its starting position ${\displaystyle \color {blue}S}$, and color it blue:

${\displaystyle \color {blue}S={\Big \{}{\big \{}*1,\{*1\},*2{\big \}},{\big \{}*2,\{*1,\{*1\},*2\}{\big \}},{\big \{}\{*1\},\{\{*1\}\},\{*1,\{*1\},*2\}{\big \}}{\Big \}}}$

For the second example game, we'll label the starting position ${\displaystyle \color {red}S'}$ and color it red:

${\displaystyle \color {red}S'={\Big \{}\{*1\}{\Big \}}.}$

To compute the starting position of the combined game, remember that a player can either make a move in the first game, leaving the second game untouched, or make a move in the second game, leaving the first game untouched. So the combined game's starting position is:

${\displaystyle \color {blue}S\color {black}+\color {red}S'\color {black}={\Big \{}\color {blue}S\color {black}+\color {red}\{*1\}\color {black}{\Big \}}\cup {\Big \{}\color {red}S'\color {black}+\color {blue}\{*1,\{*1\},*2\}\color {black},\color {red}S'\color {black}+\color {blue}\{*2,\{*1,\{*1\},*2\}\}\color {black},\color {red}S'\color {black}+\color {blue}\{\{*1\},\{\{*1\}\},\{*1,\{*1\},*2\}\}\color {black}{\Big \}}}$

The explicit formula for adding positions is: ${\displaystyle S+S'=\{S+s'\mid s'\in S'\}\cup \{s+S'\mid s\in S\}}$, which means that addition is both commutative and associative.

### Equivalence

Positions in impartial games fall into two outcome classes: either the next player (the one whose turn it is) wins (an ${\displaystyle {\boldsymbol {\mathcal {N}}}}$- position), or the previous player wins (a ${\displaystyle {\boldsymbol {\mathcal {P}}}}$- position). So, for example, ${\displaystyle *0}$ is a ${\displaystyle {\mathcal {P}}}$-position, while ${\displaystyle *1}$ is an ${\displaystyle {\mathcal {N}}}$-position.

Two positions ${\displaystyle G}$ and ${\displaystyle G'}$ are equivalent if, no matter what position ${\displaystyle H}$ is added to them, they are always in the same outcome class. Formally, ${\displaystyle G\approx G'}$ if and only if ${\displaystyle \forall H}$, ${\displaystyle G+H}$ is in the same outcome class as ${\displaystyle G'+H}$.

To use our running examples, notice that in both the first and second games above, we can show that on every turn, Alice has a move that forces Bob into a ${\displaystyle {\mathcal {P}}}$-position. Thus, both ${\displaystyle \color {blue}S}$ and ${\displaystyle \color {red}S'}$ are ${\displaystyle {\mathcal {N}}}$-positions. (Notice that in the combined game, Bob is the player with the ${\displaystyle {\mathcal {N}}}$-positions. In fact, ${\displaystyle \color {blue}S\color {black}+\color {red}S'}$ is a ${\displaystyle {\mathcal {P}}}$-position, which as we will see in Lemma 2, means ${\displaystyle \color {blue}S\color {black}\approx \color {red}S'}$.)

## First Lemma

As an intermediate step to proving the main theorem, we show that for every position ${\displaystyle G}$ and every ${\displaystyle {\mathcal {P}}}$-position ${\displaystyle A}$, the equivalence ${\displaystyle G\approx A+G}$ holds. By the above definition of equivalence, this amounts to showing that ${\displaystyle G+H}$ and ${\displaystyle A+G+H}$ share an outcome class for all ${\displaystyle H}$.

Suppose that ${\displaystyle G+H}$ is a ${\displaystyle {\mathcal {P}}}$-position. Then the previous player has a winning strategy for ${\displaystyle A+G+H}$: respond to moves in ${\displaystyle A}$ according to their winning strategy for ${\displaystyle A}$ (which exists by virtue of ${\displaystyle A}$ being a ${\displaystyle {\mathcal {P}}}$-position), and respond to moves in ${\displaystyle G+H}$ according to their winning strategy for ${\displaystyle G+H}$ (which exists for the analogous reason). So ${\displaystyle A+G+H}$ must also be a ${\displaystyle {\mathcal {P}}}$-position.

On the other hand, if ${\displaystyle G+H}$ is an ${\displaystyle {\mathcal {N}}}$-position, then ${\displaystyle A+G+H}$ is also an ${\displaystyle {\mathcal {N}}}$-position, because the next player has a winning strategy: choose a ${\displaystyle {\mathcal {P}}}$-position from among the ${\displaystyle G+H}$ options, and we conclude from the previous paragraph that adding ${\displaystyle A}$ to that position is still a ${\displaystyle {\mathcal {P}}}$-position. Thus, in this case, ${\displaystyle A+G+H}$ must be a ${\displaystyle {\mathcal {N}}}$-position, just like ${\displaystyle G+H}$.

As these are the only two cases, the lemma holds.

## Second Lemma

As a further step, we show that ${\displaystyle G\approx G'}$ if and only if ${\displaystyle G+G'}$ is a ${\displaystyle {\mathcal {P}}}$-position.

In the forward direction, suppose that ${\displaystyle G\approx G'}$. Applying the definition of equivalence with ${\displaystyle H=G}$, we find that ${\displaystyle G'+G}$ (which is equal to ${\displaystyle G+G'}$ by commutativity of addition) is in the same outcome class as ${\displaystyle G+G}$. But ${\displaystyle G+G}$ must be a ${\displaystyle {\mathcal {P}}}$-position: for every move made in one copy of ${\displaystyle G}$, the previous player can respond with the same move in the other copy, and so always make the last move.

In the reverse direction, since ${\displaystyle A=G+G'}$ is a ${\displaystyle {\mathcal {P}}}$-position by hypothesis, it follows from the first lemma, ${\displaystyle G\approx G+A}$, that ${\displaystyle G\approx G+(G+G')}$. Similarly, since ${\displaystyle B=G+G}$ is also a ${\displaystyle {\mathcal {P}}}$-position, it follows from the first lemma in the form ${\displaystyle G'\approx G'+B}$ that ${\displaystyle G'\approx G'+(G+G)}$. By associativity and commutativity, the right-hand sides of these results are equal. Furthermore, ${\displaystyle \approx }$ is an equivalence relation because equality is an equivalence relation on outcome classes. Via the transitivity of ${\displaystyle \approx }$, we can conclude that ${\displaystyle G\approx G'}$.

## Proof

We prove that all positions are equivalent to a nimber by structural induction. The more specific result, that the given game's initial position must be equivalent to a nimber, shows that the game is itself equivalent to a nimber.

Consider a position ${\displaystyle G=\{G_{1},G_{2},\ldots ,G_{k}\}}$. By the induction hypothesis, all of the options are equivalent to nimbers, say ${\displaystyle G_{i}\approx *n_{i}}$. So let ${\displaystyle G'=\{*n_{1},*n_{2},\ldots ,*n_{k}\}}$. We will show that ${\displaystyle G\approx *m}$, where ${\displaystyle m}$ is the mex (minimum exclusion) of the numbers ${\displaystyle n_{1},n_{2},\ldots ,n_{k}}$, that is, the smallest non-negative integer not equal to some ${\displaystyle n_{i}}$.

The first thing we need to note is that ${\displaystyle G\approx G'}$, by way of the second lemma. If ${\displaystyle k}$ is zero, the claim is trivially true. Otherwise, consider ${\displaystyle G+G'}$. If the next player makes a move to ${\displaystyle G_{i}}$ in ${\displaystyle G}$, then the previous player can move to ${\displaystyle *n_{i}}$ in ${\displaystyle G'}$, and conversely if the next player makes a move in ${\displaystyle G'}$. After this, the position is a ${\displaystyle {\mathcal {P}}}$-position by the lemma's forward implication. Therefore, ${\displaystyle G+G'}$ is a ${\displaystyle {\mathcal {P}}}$-position, and, citing the lemma's reverse implication, ${\displaystyle G\approx G'}$.

Now let us show that ${\displaystyle G'+*m}$ is a ${\displaystyle {\mathcal {P}}}$-position, which, using the second lemma once again, means that ${\displaystyle G'\approx *m}$. We do so by giving an explicit strategy for the previous player.

Suppose that ${\displaystyle G'}$ and ${\displaystyle *m}$ are empty. Then ${\displaystyle G'+*m}$ is the null set, clearly a ${\displaystyle {\mathcal {P}}}$-position.

Or consider the case that the next player moves in the component ${\displaystyle *m}$ to the option ${\displaystyle *m'}$ where ${\displaystyle m'. Because ${\displaystyle m}$ was the minimum excluded number, the previous player can move in ${\displaystyle G'}$ to ${\displaystyle *m'}$. And, as shown before, any position plus itself is a ${\displaystyle {\mathcal {P}}}$-position.

Finally, suppose instead that the next player moves in the component ${\displaystyle G'}$ to the option ${\displaystyle *n_{i}}$. If ${\displaystyle n_{i} then the previous player moves in ${\displaystyle *m}$ to ${\displaystyle *n_{i}}$; otherwise, if ${\displaystyle n_{i}>m}$, the previous player moves in ${\displaystyle *n_{i}}$ to ${\displaystyle *m}$; in either case the result is a position plus itself. (It is not possible that ${\displaystyle n_{i}=m}$ because ${\displaystyle m}$ was defined to be different from all the ${\displaystyle n_{i}}$.)

In summary, we have ${\displaystyle G\approx G'}$ and ${\displaystyle G'\approx *m}$. By transitivity, we conclude that ${\displaystyle G\approx *m}$, as desired.

## Development

If ${\displaystyle G}$ is a position of an impartial game, the unique integer ${\displaystyle m}$ such that ${\displaystyle G\approx *m}$ is called its Grundy value, or Grundy number, and the function that assigns this value to each such position is called the Sprague–Grundy function. R. L. Sprague and P. M. Grundy independently gave an explicit definition of this function, not based on any concept of equivalence to nim positions, and showed that it had the following properties:

• The Grundy value of a single nim pile of size ${\displaystyle m}$ (i.e. of the position ${\displaystyle *m}$) is ${\displaystyle m}$;
• A position is a loss for the next player to move (i.e. a ${\displaystyle {\mathcal {P}}}$-position) if and only if its Grundy value is zero; and
• The Grundy value of the sum of a finite set of positions is just the nim-sum of the Grundy values of its summands.

It follows straightforwardly from these results that if a position ${\displaystyle G}$ has a Grundy value of ${\displaystyle m}$, then ${\displaystyle G+H}$ has the same Grundy value as ${\displaystyle *m+H}$, and therefore belongs to the same outcome class, for any position ${\displaystyle H}$. Thus, although Sprague and Grundy never explicitly stated the theorem described in this article, it follows directly from their results and is credited to them.[3][4] These results have subsequently been developed into the field of combinatorial game theory, notably by Richard Guy, Elwyn Berlekamp, John Horton Conway and others, where they are now encapsulated in the Sprague–Grundy theorem and its proof in the form described here. The field is presented in the books Winning Ways for your Mathematical Plays and On Numbers and Games.