Before we begin, you should probably familiarize yourself with a traditional explanation of the Sprague-Grundy theorem first, such as this one.

Note

Game states will henceforth be referred to as positions. Note that any position can be thought of as a game itself: just take the subgraph of the overall DAG that is reachable from the current position.

Theoretically, to solve any impartial game, we need only to determine a single bit of information about every possible position: whether the next player (about to make a move) wins, or loses. If the next player wins, the state is called an N-position; otherwise, if the previous player wins, the state is a P-position. Together, N-positions and P-positions comprise the two possible outcome classes of any impartial game.

An example of a simple game, with positions labeled N or P. Recall that all games can be represented as DAGs (directed acyclic graphs).

However, let’s say we want equivalence classes that are a bit more fine-grained. Why? Well, consider the operation of combining two positions 𝐺 and 𝐻 (henceforth denoted as 𝐺+𝐻) in parallel, which means that these two positions will be played side-by-side: on each player’s turn, they can choose to move on exactly one of the positions, and leave the other unchanged (a classic example of this would be a two-pile Nim game).

Note

Considering the positions as DAGs, this + operation corresponds precisely to a Cartesian product: A simple Cartesian product, illustrated. Each edge corresponds to a move on exactly one of the two parallel positions.

Note that the + operation as defined here is both commutative and associative. It is also closed, since the Cartesian product of two DAGs is still a DAG.

Importantly, under our current definition of equivalence by outcome classes (i.e. all N-positions are equivalent, as are all P-positions), equivalent positions might not produce equivalent results when combined with other positions. Specifically, two N-positions combined could produce either an N-position or a P-position.

Two N-positions combined don’t always yield equivalent results.

This motivates a new definition of equivalence:

Definition

𝐺≑𝐺′ iff for all positions 𝐻, 𝐺+𝐻 and 𝐺′+𝐻 belong to the same outcome class.

Note that this also means that if 𝐺≑𝐺′, 𝐺+𝐻≑𝐺′+𝐻 as well, since

πΊβ‰‘πΊβ€²β‡’βˆ€(𝐻,𝐻′),𝐺+(𝐻+𝐻′)≑𝐺′+(𝐻+𝐻′)β‡’βˆ€π»β€²,(𝐺+𝐻)+𝐻′≑(𝐺′+𝐻)+𝐻′

Here, we use the fact that + is associative.

And the equivalence classes of positions under this definition are precisely what the Sprague-Grundy theorem helps us find!

Note

While the Sprague-Grundy theorem is often thought of as a very general result, it’s only really useful when games are being combined in parallel. Otherwise, the Grundy number has no real meaning.


Let’s consider the identity equivalence class, 0, defined such that for every position 𝐺 in this class, 𝐴≑𝐴+𝐺 for all games 𝐴. A trivial example of such a game is the empty position {}, but there are actually more. In fact:

Lemma 1

0 is exactly the set of all P-positions.

Proof: Let’s pick some P-position 𝐺. We wish to prove that for all positions 𝐴, 𝐴≑𝐴+𝐺. Note that this is actually equivalent to proving the simpler statement that for all positions 𝐴, 𝐴 and 𝐴+𝐺 belong to the same outcome class.

If 𝐴 is an N-position, the first player can win 𝐴+𝐺 like so:

  • Always play in 𝐴 according to their original strategy for 𝐴.
  • If the second player plays in 𝐺, the first player will always have a response since 𝐺 is a P-position.

A similar argument holds for the case when 𝐴 is a P-position.

On the contrary, no N-position can be in 0: a simple counter-case is 𝐴={}.

Another useful lemma:

Lemma 2

For all positions 𝐺, 𝐺+𝐺=0.

Proof: Player 2 can always simply mirror Player 1’s move. Thus, 𝐺+𝐺 is a P-position and therefore is in 0.

…Wait a minute. 𝐺+𝐺=0? This + operation is starting to look suspiciously like XOR! In fact, Lemma 2 is precisely the reason why XOR appears in the Sprague-Grundy theorem: not because of the nim-sum, but because of this much more fundamental β€œmirror” property.

By the way, this property also gives us an easier way to check equivalence:

Lemma 3

𝐺≑𝐺′ iff 𝐺+𝐺′=0.

Proof: For all 𝐴, 𝐴+𝐺≑𝐴+𝐺+(𝐺+𝐺′)≑𝐴+(𝐺+𝐺)+𝐺′≑𝐴+𝐺′. Here, we’ve used the fact that + is associative.


Now, let’s define a basis of games 𝑋𝑖 such that every game 𝐺 is equivalent to the sum of exactly one subset of 𝑋. Essentially, the games in 𝑋 must all be independent, as well as span all possible games under the + operation.

Then, using this basis, every equivalence class 𝐺 has a unique binary representation 𝑓𝑋(𝐺), where the 𝑖th bit of 𝑓𝑋(𝐺) denotes whether 𝑋𝑖 is a member of the equivalent subset or not. Importantly, combining two games in parallel then corresponds to simply XOR-ing these binary representations!

So, are these binary representations the Grundy numbers, then?

Well, not quite. See, we don’t just have one choice of basis. Consider the game of Nim, for instance. The classic basis is {βˆ—1,βˆ—2,βˆ—4,βˆ—8,…} where βˆ—π‘₯ denotes a single pile of size π‘₯. In this basis, we would represent the game βˆ—7 as 1112. However, we could also use something like {βˆ—1,βˆ—3,βˆ—6,βˆ—8,βˆ—16,…}, in which case βˆ—7 would be represented as 1012 instead. Yet, no matter what basis 𝑋 we choose, remember that 𝑓𝑋(𝐺+𝐻) always equals 𝑓𝑋(𝐺)βŠ•π‘“π‘‹(𝐻) due to Lemma 2.

What basis should we use then? And how do we even ensure our basis elements are independent?

Enter: mex! Just like XOR, the use of the mex function in the Sprague-Grundy theorem is typically justified by converting all games into Nim. However, there again exists a more fundamental reason for its use: it encapsulates a clever greedy algorithm that helps us quickly find a valid basis.

Whaa? OK, let me describe this greedy algorithm to you first, without using mex at all.

  1. Topologically sort all game states (guaranteed to be possible since game is a DAG).
  2. Initialize our basis 𝑋 as an empty list, [] (because order matters!).
  3. In reverse topological order, check if our current game 𝐺 is independent of 𝑋. If yes, append 𝐺 to the end of 𝑋.

And that’s it!

Wait, what? That’s it? How is this even remotely related to mex? And how do we check whether a game is independent of 𝑋? The key idea is to show that because of this specific greedy approach, the 𝑖th basis element 𝑋𝑖 has the property that it can transition to an equivalent state of every subset of the first 𝑖 basis elements. That is, for every 𝑧<2𝑖, 𝑋𝑖 can transition to a state 𝑍 such that 𝑓𝑋(𝑍)=𝑧. This will enable us to implement a much faster check for independence.

In fact, together with the properties of XOR, this claim generalizes to the following:

Lemma 4

For every state π‘Œ and every 𝑧<𝑓𝑋(π‘Œ), π‘Œ can transition to some state 𝑍 such that 𝑓𝑋(𝑍)=𝑧.

Do you see where mex could come in now?

Proof: We induct. Let’s say this claim is true for every state we’ve encountered so far in our reverse topological order: the base case is rather simple. Now, let our current state be 𝐺, and the set of possible next states as 𝑇𝑖. Let π‘š be the mex of 𝑓𝑋(𝑇𝑖) over all 𝑇𝑖. We wish to show that 𝑓𝑋(𝐺)=π‘š.

To do this, consider some game 𝑀 in equivalence class π‘š and spanned by our current basis 𝑋. If no such game exists, 𝐺 is independent of 𝑋 and thus will be added as a new element to the basis. Note that this also means we can transition to every game currently spanned by 𝑋, so our inductive hypothesis still holds.

Otherwise, we then need to prove is that 𝐺≑𝑀, or that 𝐺+𝑀=0. We can show this by splitting into two cases:

  1. The first player moves either 𝐺 or 𝑀 to a state 𝑍 with 𝑓𝑋(𝑍)<π‘š. The second player can then match this value by moving on the other game. Both states will have value 𝑓𝑋(𝑍) and will thus form a P-position.
  2. The first player moves either 𝐺 or 𝑀 to a state 𝑍 with 𝑓𝑋(𝑍)>π‘š. The second player can then move this state back to one with value π‘š. Both games will now have value π‘š and will thus form a P-position.

Since the second player can always force the first player to begin their next turn on a P-position, it follows that 𝐺+𝑀 must be a P-position itself. Therefore, 𝐺≑𝑀, as desired.


OK, that was a lot. But the primary takeaway is that the entire reason mex is used is because of the (surprisingly elegant) greedy strategy we used in order to find a valid basis. In fact, (don’t quote me on this) this may be one of the only efficient strategies to compute a valid basis because of the issue of checking independence. If 𝑋 was just some arbitrary basis, this could have taken forever to check, and maybe even have been impossible. Thankfully, due to the nice structure our greedy algorithm imposes on 𝑋, we instead end up with a very quick and elegant way of evaluating independence.

The best part? We derived all these beautiful results without referencing the game of Nim at all! The XOR relation arose naturally from the β€œmirror” nature of combining identical impartial games, and mex showed up as a direct result of the simple greedy strategy we defined to find a valid basis. This, I believe, is truly the best way to understand the fundamental nature of the Sprague-Grundy theorem.