Combinatorical Game Theory
Introduction
from wikipedia
Combinatorial game theory (CGT) is a branch of mathematics and theoretical computer science that typically studies sequential games with perfect information.
This is a rather niche topic in competitive programming, yet it is(arguably) one of the most interesting and there is more logic behind these combinatorial puzzles than you would have imagined.
Let’s start with a simple and typical Nim’s game:
Nim’s Game
There are several piles of stones. A player can take any positive number of stones from any one pile and throw them away in a move. A player loses if they cannot make a move i.e. when all the piles are empty.
Before we get started, notice that this particular game has the following features:
-
This is an impartial two-player game i.e. available moves and win/lose positions only depend on the current state of the game, and the only difference between the two players is that one of them starts first.
-
There is perfect information i.e. both players are rational individuals who know about the rules and set of available moves for any state of the game, and are trying to win(at least not lose) the game.
-
The game is finite i.e. the game would end after a finite number of moves. In this case, since the total number of stones is finite, we know that the game is going to end with one player losing.
Knowing all these features, we can think of this game as a directed acyclic graph (DAG) where the vertices represent different states of the game while the edges are all possible moves from one state to another. of course, there will be a vertex with no outgoing edges(outdegree=0) which represents the losing stance, since there is no available move.
Consider the scenario where we have three piles of rocks with 2,1,1 rocks respectively. We can draw out the following diagram for all the possible states in the game:
We define any state to be a
p
−
p
o
s
i
t
i
o
n
p-position
p−position if the current player is losing, and a
N
−
p
o
s
i
t
i
o
n
N-position
N−position if the current player is wining. Clearly, the bottom state(0,0,0) is a
P
−
p
o
s
i
t
i
o
n
P-position
P−position. It is not hard to notice that which class other states fall into can be deduced recursively by the following two rules:
- The current state is a N − p o s i t i o n N-position N−position if it has an outward edge to a P − p o s i t i o n P-position P−position .
- The current state is a P − p o s i t i o n P-position P−position if it does not have an outward edge to a P − p o s i t i o n P-position P−position.
This is intuitive to understand. For the current player, he only loses if no matter how he moves, he always end up letting the other player win. As long as there exists one move that let him go to a losing state( P − p o s i t i o n P-position P−position ), he would choose that particular move and proceed to win the game.
Now since this is a DAG, we can define every state to be either a N − p o s i t i o n N-position N−position or a P − p o s i t i o n P-position P−position according to the above two rules. And we know that the first player is going to win the game if any of the starting state is a N − p o s i t i o n N-position N−position.
Sprague-Grundy Theorem
To conclude a winning strategy of the game, we first represent the game state by a multiset of positive integers as described earlier. A move strictly decreases a chosen integer, and the integer is removed from the set if it becomes zero.
We claim the following strategy exists:
The current player has a winning strategy if and only if the xor-sum of the pile sizes is non-zero. The xor-sum of a sequence a a a is a 1 ⊕ a 2 ⊕ … ⊕ a n a_1⊕a_2⊕…⊕a_n a1⊕a2⊕…⊕an, where ⊕ is the bitwise exclusive or.
We can prove this is true by mathematical induction.
When all the piles are empty (i.e. the multiset is empty) the xor-sum is zero and the theorem is true since he automatically loses the game.
Now suppose we are in a non-empty state. We firstly ssume that the the statement is true for all states reachable from the current one. This is a valid induction assumption given that all the game states from an acyclic graph.
Let the current xor sum be s s s, and the reachable state xor sum be t t t.
Case 1:
s
=
0
s=0
s=0. Any current move reduces the size of some pile from
x
x
x to
y
y
y.
Notice that
t
=
s
⊕
y
⊕
x
=
0
⊕
y
⊕
x
=
y
⊕
x
t=s⊕y⊕x=0⊕y⊕x=y⊕x
t=s⊕y⊕x=0⊕y⊕x=y⊕x. Since
y
<
x
y<x
y<x,
t
=
y
⊕
x
≠
0
t=y⊕x\neq0
t=y⊕x=0. Thus, any reachable state is a winning state, and the current state is hence a losing state.
Case 2: s ≠ 0 s≠0 s=0. In this case, we let d d d be Consider the leading "1"s in the binary representation of the number s s s, let the number be m m m. We know that there is at least one pile that contributes to all these "1"s to the final xor sum, since xor sum of all other piles is going to give 0 on these digits. Suppose the current player its size from x x x to y = x ⊕ s y=x⊕s y=x⊕s. This is a valid move since the first m m m bits is now 0, while other bits stay unchanged, giving us y < x y<x y<x. Since t = s ⊕ x ⊕ y = s ⊕ x ⊕ ( s ⊕ x ) = 0 t=s⊕x⊕y=s⊕x⊕(s⊕x)=0 t=s⊕x⊕y=s⊕x⊕(s⊕x)=0, this is a reachable losing stateand the current state is a winning state.
Thus, if a state satisfies our theorem, all the states that can reach this state (where all the reverse edges from the state point towards) also satisfy the theorem. By induction, we can see that our theorem is true for all the game states.