2026-01-28
Running time \(O(nm)\)
Every edge is visited once, hence \(O(m)\).
// This vector of vectors represents the reversed adjacency list of the graph. vector<vector<int>> adj_rev; // These vectors are used to store information about each vertex in the graph. // winning[v] is true if vertex v is in a winning state, and false otherwise. // losing[v] is true if vertex v is in a losing state, and false otherwise. // visited[v] is true if the vertex v has been visited during the depth-first search. // degree[v] represents the in-degree (number of incoming edges) of vertex v. vector<bool> winning; vector<bool> losing; vector<bool> visited; vector<int> degree;
// Depth-First Search (DFS) function to traverse the graph and determine winning and losing states.
void dfs(int v) {
// Mark the current vertex as visited.
visited[v] = true;
// Traverse all vertices u that have an incoming edge from v in the reversed adjacency list.
for (int u : adj_rev[v]) {
// If vertex u has not been visited yet.
if (!visited[u]) {
// If the current vertex v is in a losing state, mark u as winning.
if (losing[v])
winning[u] = true;
// If the in-degree of vertex u is reduced to 0, mark u as losing.
else if (--degree[u] == 0)
losing[u] = true;
// Otherwise, continue the DFS traversal for vertex u.
else
continue;
// Recursively call the DFS function for vertex u.
dfs(u);
}
}
}
Lemma: Any cycle of size greater than 3 can be used by the robber to escape forever.
Proof: Robber initially chooses a node not adjacent to the cop. Then she moves in the same direction as the cop, forever.
Definition: Vertex \(v\) is dominated by vertex \(w\), if its closed neighbourhood is a subset of the neighbourhood of \(w\).
Definition: A vertex \(v\) is a losing vertex if it is dominated by another vertex.
Definition: An undirected graph \(G\) is a cop-win graph if the cop can always force a win on this graph.
Definition: An undirected graph \(G\) is a robber-win graph if the robber can forever escape the cop on this graph.
Lemma: Every cop-win graph has a dominated vertex
Proof: Otherwise, the robber would have an escape path from any vertex
Lemma: There is a dominated vertex in any cop-win graph, whose removal will result in another cop-win graph.
Proof: We will prove by showing that a robber cannot convert a cop-win graph to a robber-win graph by adding a dominated vertex. Let \(G\) be any cop-win graph. Add a dominated vertex \(v\) to \(G\) to obtain \(G'\). Let \(w\) be the vertex dominating \(v\) in \(G'\). Obviously, the closed neighbourhood of \(v\) is a subset of the neighbourhood of \(w\) in \(G'\), by definition. This means, any move from \(v\) can also be made from \(w\). Therefore, going to \(v\) has no advantages over \(w\) for the robber, she is simply going to a more restricted vertex. If the robber ever had a winning strategy in \(G'\), she could simply replace \(v\) moves by \(w\) moves and would still win. Therefore, if \(G'\) is robber-win, then \(G\) is also robber-win. We know that \(G\) is cop-win. A contradiction. Therefore, \(G'\) is also cop-win.
Corollary: A cop-win graph can be dismantled down to a single node by removing dominated nodes one by one.
Proof: It follows from the previous two lemmas.
\(v\) is dominated by \(u\). Any dominated vertex \(v\) and dominating vertex \(u\) has an edge in between, which makes \(\delta_v-1\) triangles with \(v\)’s neighbours (except \(u\)).
Theorem. The current player has a winning strategy iff the xor-sum of the pile sizes is non-zero. The xor-sum (a.k.a. nim-sum) of a sequence \(a\) is \(a_1\oplus a_2\oplus a_3\oplus\ldots\oplus a_n\), where \(\oplus\) is the bitwise exclusive or.
X
X X 1 1
X X X -->> 1 1 0 -->> 0 -->> YOU WIN!
X X X 1 0 0 1
3 2 4 s
We will prove it by induction on the next slide.
1101
1100
1000
----
1001 -> non-zero
Hence the state is a winning-state
Nimble. Nimble is played on a game board consisting of a line of squares labelled: 0, 1, 2, 3, … . A finite number of coins is placed on the squares with possibly more than one coin on a single square. A move consists in taking one of the coins and moving it to any square to the left, possibly moving over some of the coins, and possibly onto a square already containing one or more coins. The players alternate moves and the game ends when all coins are on the square labelled 0. The last player to move wins. Show that this game is just nim in disguise. In the following position with 6 coins, who wins, the next player or the other? What is a winning move?
Turning Turtles A horizontal line of n coins is laid out randomly with some coins showing heads and some tails. A move consists of turning over one of the coins from heads to tails, and in addition, if desired, turning over one other coin to the left of it (from heads to tails or tails to heads). For example consider the sequence of n = 13 coins:
T H T T H T T T H H T H T 1 2 3 4 5 6 7 8 9 10 11 12 13
One possible move in this position is to turn the coin in place 9 from heads to tails, and also the coin in place 4 from tails to heads.
T H T H H T T T T H T H T 1 2 3 4 5 6 7 8 9 10 11 12 13
Turning Turtles A horizontal line of n coins is laid out randomly with some coins showing heads and some tails. A move consists of turning over one of the coins from heads to tails, and in addition, if desired, turning over one other coin to the left of it (from heads to tails or tails to heads).
Show that this game is just nim in disguise if an H in place n is taken to represent a nim pile of n chips.
Assuming (a) to be true, find a winning move in the above position.
Turning the first coin at position k from H to T represents removing a pile with k stones. Turning a coin to the left of it at position m (m < k) acts like removing k-m stones from a nim pile.
To find a winning move simply find one or two coins to turn, such that after turning them the xor-sum of the positions of the remaining heads are zero.
Any state of any impartial game has a corresponding Nim state!
\[ x = \text{mex}\ \{ x_1, \ldots, x_k \}, \]
Find the SG-values for the nodes of the following graph:
Find the SG-values for the nodes of the following graph:
At-Least-Half. Consider the one-pile game with the rule that you must remove at least half of the counters. The only terminal position is zero.
At-Least-Half. Consider the one-pile game with the rule that you must remove at least half of the counters. The only terminal position is zero.
We may compute the Sprague-Grundy function inductively as
x 0 1 2 3 4 5 6 7 8 9 10 11 12 ... g(x) 0 1 2 2 3 3 3 3 4 4 4 4 4 ...
In closed form \(g(x) = \lfloor \lg x \rfloor + 1\).
At-Most-Half. Consider the one-pile game with the rule that you may remove at most half the chips. Of course, you must remove at least one, so the terminal positions are 0 and 1. Find the Sprague-Grundy function.
At-Most-Half. Consider the one-pile game with the rule that you may remove at most half the chips. Of course, you must remove at least one, so the terminal positions are 0 and 1. Find the Sprague-Grundy function.
We may compute the Sprague-Grundy function inductively as
x 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15... g(x) 0 0 1 0 2 1 3 0 4 2 5 1 6 3 7 0...
In closed form
\[ g(x) = \left\{\begin{array}{lr} x/2, & \text{for even x}\\ g(\lfloor x/2 \rfloor), & \text{otherwise}\\ \end{array}\right\} \]
Consider the one-pile game with the rule that you may remove c chips from a pile of n chips if and only if c is a divisor of n, including 1 and n. For example, from a pile of 12 chips, you may remove 1, 2, 3, 4, 6, or 12 chips. The only terminal position is 0. Find the Sprague-Grundy function.
Suppose the above rules are in force with the exception that it is not allowed to remove the whole pile. This is called the Aliquot game by Silverman, (1971). Thus, if there are 12 chips, you may remove 1, 2, 3, 4, or 6 chips. The only terminal position is 1. Find the Sprague-Grundy function.
The game starts with a pile of \(n\) stones, and the player to move may take any positive number of stones. Calculate the Grundy Numbers for this game. The last player to move wins. Which player wins the game?
If there are no stones, the player loses. There is no reachable state from here. Therefore, the grundy value for this state is \(0\).
If there is \(1\) stone, we can remove it and reach state with grundy value \(0\). \(mex(\{0\})=1\), hence this state has grundy value \(1\).
If we have \(2\) stones, we can remove \(1\) or \(2\) to reach states with grundy values \(1\) and \(0\). \(mex(\{0,1\})=2\). Therefore, the grundy value of this state is \(2\)
\(\ldots\)
Since each state (except 0) can reach state \(0\), the first player always wins.
The game starts with a pile of n stones, and the player to move may take any positive number of stones up to 3. The last player to move wins. Which player wins the game?
The game starts with a number- \(n\) and the player to move divides the number with \(2, 3\) or \(6\) and then takes the floor. If the integer becomes \(0\), it is removed. The last player to move wins. Which player wins the game?
Consider a game where you have to cut slices from a rectangular board. Each player, in her turn, cuts a slice either horizontally or vertically, with a thickness of \(1\). The player who has nothing left to cut loses the game. If you start with a \(5\times 5\) board, can you win?
Start with \((0,0) \rightarrow 0\)
Note that each state \((i,j)\) can be transitioned to one of states \((i-1,j)\) or \((i,j-1)\).
We can quickly create a 2d-matrix of all states as follows:
0 1 2 3 4 5 ------------------------- 0 | 0 | 0 | 0 | 0 | 0 | 0 | ------------------------- 1 | 0 | 1 | 2 | 1 | 2 | 1 | ------------------------- 2 | 0 | 2 | 0 | 2 | 0 | 2 | You will lose! ------------------------- 3 | 0 | 1 | 2 | 0 | 1 | 0 | ------------------------- 4 | 0 | 2 | 0 | 1 | 0 | 1 | ------------------------- 5 | 0 | 1 | 2 | 0 | 1 | 0 | -------------------------
Consider a game where you have to cut slices from a rectangular board. Each player, in her turn, cuts a slice either horizontally or vertically, with a thickness of \(1\). The player who has nothing left to cut loses the game. If you start with a \(5\times 5\) board, can you win?
Consider a game of Nim on a single heap and with a subtraction set of \(\{1,3,4\}\). Analyze its Grundy values.
Consider a game of Nim where you start with three piles of stones having 1 million, 1 million and 1, 1 million and 2 stones. In one turn, each player is allowed to get at least 1 and at most 3 stones from a single pile. Will the starting player win?
Consider a game of Nim where you start with three piles of stones having 1 million, 1 million and 1, 1 million and 2 stones. This time you can get at most 1 stone from the first pile, at most 2 from the second pile and at most 3 from the third pile.
Suppose in a game with a pile containing a large number of chips, you can remove any number from 1 to 6 chips at each turn.
The Thirty-one Game. (Geoffrey Mott-Smith (1954)) From a deck of cards, take the Ace, 2, 3, 4, 5, and 6 of each suit. These 24 cards are laid out face up on a table. The players alternate turning over cards and the sum of the turned over cards is computed as play progresses. Each Ace counts as one. The player who first makes the sum go above 31 loses. It would seem that this is equivalent to the game of the previous exercise played on a pile of 31 chips. But there is a catch. No integer may be chosen more than four times.
The Thirty-one Game. (Geoffrey Mott-Smith (1954)) From a deck of cards, take the Ace, 2, 3, 4, 5, and 6 of each suit. These 24 cards are laid out face up on a table. The players alternate turning over cards and the sum of the turned over cards is computed as play progresses. Each Ace counts as one. The player who first makes the sum go above 31 loses. It would seem that this is equivalent to the game of the previous exercise played on a pile of 31 chips. But there is a catch. No integer may be chosen more than four times.
Find the set of losing states for the following subtraction sets:
S = {1, 3, 5, 7}.
S = {1, 3, 6}.
S = {1, 2, 4, 8, 16,…} = all powers of 2.
Who wins each of these games if play starts at 100 chips, the first player or the second?
Empty and Divide. There are two boxes. Initially, one box contains m chips and the other contains n chips. Such a position is denoted by (m, n), where m > 0 and n > 0. The two players alternate moving. A move consists of emptying one of the boxes, and dividing the contents of the other between the two boxes with at least one chip in each box. There is a unique terminal position, namely (1, 1). Last player to move wins. Find all losing states.
Consider a game where a player is able to replace any heap of size \(k\) with up to \(k-1\) heaps of any positive size strictly less than \(k\). Prove that in this game, the Sprague-Grundy function of a heap of size \(k\) is \(0\) if \(k=1\), and \(2^{k-2}\) otherwise.
Consider a game on heaps, where a turn consists of taking a heap of size \(n\), and a positive integer \(d\) that divides \(n\), where \(1 \leq d < n\), and splits it into \(n/d\) heaps of size \(d\).
Analyze the Sprague-Grundy function of this game. (Try doing it with just the odd heap sizes first)