A PSPACE-complete Graph Nim
Abstract
We build off the game NimG [9] to create a version named Neighboring Nim. By reducing from Geography, we show that this game is -hard. The games created by the reduction share strong similarities with Undirected (Vertex) Geography and regular Nim, though these are both solvable in polynomial-time. This application of graphs can be used as a form of game sum with any rulesets, not only Nim.
1 Background
1.1 Algorithmic Combinatorial Game Theory
Most of the results here revolve around the computational complexity of determining which player has a winning strategy from a given game position. There exist faster algorithms to solve this problem for some rulesets than for others. For each ruleset, we consider the computational problem that could be solved by such an algorithm. We will refer to both the ruleset and problem by the same name.
We strongly encourage readers unfamiliar with these topics to refer to [1].
1.2 Terminology
There is a small amount of non-standard terminology used in this paper.
-
•
We use the word sticks to refer to the objects in nim heaps. Thus, a nim heap of size six contains six sticks.
-
•
An optimal sequence set is a set of sequences of plays for both players such that any move deviating from one of the sequences results in an N-position. No move in that sequence should be non-optimal for either player. Thus, if a player does not know whether they have a winning strategy, adhering to an optimal sequence is at least as good as any other move.
1.3 Nim
Nim is an impartial game played on a collection of heaps, each with a non-negative number of sticks. On a player’s turn, they choose a non-empty pile and remove as many sticks as desired (at least one) from that pile. A player loses when they cannot remove sticks (all piles are empty).
1.4 NimG
Nim has been extended to incorporate graphs so that nim heaps are assigned to either edges or vertices. There are three different versions of the game named NimG. In all three versions, a turn consists of both traversing an edge of the graph and removing sticks from a visited element.
1.4.1 Edge-heap NimG
Fukuyama describes NimG where nim heaps are embedded into the edges of the graph[4]. On each turn, the current player chooses an edge to traverse (which has at least 1 stick on it) and removes any number of sticks from that edge. The next player then starts on the vertex on the other end of that edge and must choose an adjacent edge for their move. When there are no more edges with sticks adjacent to the current vertex, the current player loses. Many results for this game are known on complete graphs[2].
1.4.2 Vertex-heap NimG
In Vertex NimG, players similarly move from one vertex to another, but heaps are connected to the vertices instead of edges[9]. The two variants can be easily described here as: “remove sticks, then move” and “move, then remove sticks”. In both cases, a player loses if they cannot complete their turn. The main topic of this paper is a variant of “move, then remove”.
1.5 Geography
We will use Geography to show the -hardness of Neighboring Nim. There are many flavors of Geography; we use the term to refer to Directed Vertex Geography. This impartial game is played on a directed graph; each turn begins with a vertex chosen. The current player’s turn consists of choosing an outgoing arc that leads to a vertex that hasn’t been visited yet. The next player then starts their turn with the resulting vertex selected. We formally describe the ruleset as follows:
Definition 1.1 (Geography)
(Directed Vertex) Geography positions are described by: and . Move options for are all where:
-
•
,
-
•
,
-
•
is the subset of induced by , and
-
•
.
Geography is known to be -complete[7].
2 Neighboring Nim
We define the ruleset Neighboring Nim to be similar to the “move then remove” version of NimG, but also allow players to choose to play on the same vertex as the last move as though each vertex has a self-loop. Note that standard Nim is equivalent to a game of Neighboring Nim on a complete graph with each heap on a separate vertex. A more formal definition follows.
Definition 2.1 (Neighboring Nim)
Neighboring Nim positions are described by , , and . The options for are all where and
-
•
or ,
-
•
, and
-
•
.
Our main result for this paper is that Neighboring Nim is -hard. Since our analysis uses graphs with a small number of sticks on each vertex, we define a version of the game with a bounded number of sticks per vertex.
Definition 2.2 (-Neighboring Nim)
-Neighboring Nim is the same ruleset as Neighboring Nim, except that the weight function, , has bounded range: .
We are able to show that 2-Neighboring Nim is -complete, and thus -Neighboring Nim is also -complete for any constant . The case for 1-Neighboring Nim is solvable in polynomial time, since this game is equivalent to Undirected (Vertex) Geography [3]. Thus, if , allowing a second stick on some vertex-heaps can greatly increase the computational hardness of determining the winning player!
3 Computational Complexity of Neighboring Nim
3.1 -hardness
The following is the main result of this paper.
Theorem 3.1 (Hardness)
Neighboring Nim is -hard.
We will show the hardness of this problem by reducing from the game Geography, which is -hard[6].
Proof. Given any Geography position, we will give an algorithm to construct an equivalent Neighboring Nim state, meaning that there is a win in the Geography position exactly when there is a win in corresponding Neighboring Nim position. First we will describe the method for generating these positions, then prove their equivalence.
Let be a Geography position on the directed and unweighted graph . We define a new undirected graph, with weights on the vertices in the following way. let and set . Also, (edge directed from to ) let where, ignoring the -subscripts, , and,
.
See Figure 1 for a visual description.

The resulting is the graph for our Neighboring Nim position equivalent to . The only final step is to declare that if has a starting vertex, , then is the starting vertex (where the previous play had been made) in our game and is set to 0 instead of 1.
To complete the reduction, we must show that the structure in Figure 1 “acts” like a directed edge in Geography. Thus, we must prove:
-
•
Moving “backwards” is a losing play. If the previous play was at , then a backwards play would be to remove the only stick at . A backwards play results in an -position.
-
•
The same player moving into the gadget should also move out. If a player moves from to , then in an optimal sequence of plays, the same player will move from to .
We prove the former in Lemma 3.2 and the latter in Lemma 3.3. The result is that each of these gadgets (as in Figure 1) in the Neighboring Nim position works just like a (directed) edge in Geography. Trying to go backwards will result in losing and, if players play optimally, they both might as well continue through each gadget.
Lemma 3.2 (Don’t Go Backwards)
Any play from to (for all ) results in an -position.
(See Appendix A for a proof of this claim.) This implies that our gadgets are directed: if a player tries to go “backwards”: from an -vertex to an -vertex, the opponent will have a winning strategy.
To finish showing that our gadget acts like a directed edge, we must prove that “nothing can go wrong” during a regular forward traversal of the structure. To this end, we find two sequences that constitute an optimal sequence set through the gadget, thus showing that neither player benefits from deviating from the sequence. In order to get from one end of the gadget (as in Figure 1) to the other, the following sequence of moves suffices (let Alice and Bob be our two players; we will again ignore subscripts): Alice “takes” , Bob takes , Alice takes , Bob takes , Alice decrements by 1, Bob takes , Alice takes . Note that the same player (in this example, Alice) who chooses to take also moves to . The other sequence is where Bob takes instead of —here Alice will take the remaining object at and Bob will be forced to take , rejoining with the first sequence. See Figure 2 for a visual description of the safe sequences. We must prove that neither player benefits from deviating from these sequences. To do this, we show that any deviation is a losing move.

Lemma 3.3 (Stick to the Script)
Let the notation denote taking objects from vertex in a turn. Then, after the plays any play deviating from the following sequences is a losing move:
.
(See Appendix B for the proof of this claim.) This implies that once a player makes an appropraite move onto the gadget (playing on an -node) any “safe” sequence of moves in the gadget results in that same player making the play at the opposite node. The two above claims combined show that our gadget correctly models a directed edge in a graph just between the nodes.
Thus, for any edge in our Geography position, , the move to will result in the same player moving to as desired. Also, since we proved players shouldn’t go backwards, this game is equivalent to ; the first player has a winning strategy in exactly when the first player has a winning strategy in this Neighboring Nim position.
Thus, Neighboring Nim is -hard.
The hardness of Vertex NimG follows directly.
Corollary 3.4 (Vertex NimG hardness)
Vertex NimG is -hard.
Proof. Neighboring Nim is a special case of Vertex NimG where all vertices have self-loops. Thus, Vertex NimG is also -hard.
3.2 Speculation on Completeness
Unfortunately, Neighboring Nim is not automatically -complete as games could take a number of moves exponential in the size of the description of the game. For example, a vertex can have a number of sticks exponential in the amount of bits needed to express that number and the rest of the graph. We leave this unsolved as Open Problem 6.1. There are good arguments to conjecture either way.
On one hand, it seems to not be inside . Games can last an exponential number of turns, so the game trees are extremely tall. A straight-forward brute-force traversal can’t be performed in polynomial space.
On the other hand, it might be inside . Although there are many -hard rulesets, the authors know only of loopy examples. This means they can have positions that repeat during the course of a game, which cannot occur in Neighboring Nim. Additionally, Nim heaps are well-understood, perhaps increasing the size of the heaps doesn’t greatly increase the difficulty of finding strategies.
3.3 -complete versions
We can sidestep this problem a bit by using our bounded-heap-size version of the game.
Corollary 3.5 (2-Neighboring Nim Completeness)
-Neighboring Nim is -complete for any .
Proof. The result of the reduction from Theorem 3.1 is always a 2-Neighboring-Nim position. Thus, the -hardness holds for this subset of positions as well. The positions are in because bounds the maximum number of moves per vertex.
4 Generalization
This graph-embedding technique works with games other than Nim. Given a graph, assign different game states to the vertices, and use similar rules: players may make one move legal in the game in any vertex neighboring the last play. We define this formally.
Definition 4.1 (Neighboring-)
Given any ruleset , Neighboring- has positions of the form , , and . The left options for are where and:
-
•
or ,
-
•
is a left option of , and
-
•
.
The right options are defined analagously: must be a right option of .
4.1 Inequivalent Positions
This new definition allows a Neighboring Nim vertex to contain multiple heaps instead of only a single heap. Although each Nim position is equivalent to a single heap, that equivalence doesn’t carry over in the Neighboring situation. Consider the two Neighboring Nim games in figure 3. (The previous move was made on the left-most vertex in both cases.) The values of the games embedded in the left vertices are both 0, the values on the middle vertices are both 0, and the values of the rightmost games are both . However, the overall value of the positions are not equivalent.

In the top game, there are no move options, so the value is . In the bottom position, the next player can move to the middle vertex, even though the value of the Nim game there is also zero. After that move there are exactly two moves remaining. Thus, the initial game has exactly three moves remaining and has a value of .
4.2 Generalized Hardness
The next result allows us to say something about the hardness of graph-embedded versions of many impartial games.
Theorem 4.2 (Neighboring- Hardness)
For any ruleset, , which has positions identical to , and , Neighboring- is -hard.
Proof. Two positions are identical if they have isomorphic game trees. Replacing the nim heaps of size 1 and 2 with and , respectively, in the reduction of theorem 3.1 doesn’t change the winnability of the resulting games. Thus, the reduction applies to .
5 Conclusions
Building on algorithmic work analyzing different versions on NimG, we present Neighboring Nim, a new -hard game.
An interesting aspect of the hardness of Neighboring Nim is the juxtaposition with Vertex Geography. 1-Neighboring Nim is the same ruleset as Undirected Vertex Geography, which is solvable efficiently[3]. However, by adding an extra stick to just a few vertices, we can push the game into -hardness!
Furthermore, we can replace Nim and apply the graph-embedding concept to any other ruleset, , to create Neighboring-.
6 Future Work
There are many extensions to the work described here. The most prominent is certainly the unknown completeness of Neighboring Nim.
Open Problem 6.1
?
Additionally, there is still work to be done on the computational hardness of other flavors of NimG.
Open Problem 6.2
What is the computational complexity of Edge NimG?
Open Problem 6.3
What is the computational complexity of Vertex NimG on graphs without self-loops?
Other explorable problems include the hardness of other versions of Neighboring-.
Open Problem 6.4
Is Neighboring- -hard if includes any positions equivalent to and ?
Note that open problem 6.4 is a stronger statement than shown here because equivalent does not necessarily mean identical.
Open Problem 6.5
For which other computationally easy rulesets, , is Neighboring- hard?
Open Problem 6.6
Are there strictly partisan positions of a ruleset, , that can be used to show Neighboring- is hard? How small can the game trees be to get a hard game?
7 Acknowledgments
The authors would like to thank those who played Neighboring Nim with them, including three excellent Wittenberg University students: Deanna Fink, Dang Mai and Ernie Heyder, as well as Professor Doug Andrews who defeated the first author over and over again. We would also like to thank Adam Parker for listening to initial versions of the hardness proof and proofreading this paper.
References
- [1] Erik D. Demaine and Robert A. Hearn. Playing games with algorithms: Algorithmic combinatorial game theory. In Michael H. Albert and Richard J. Nowakowski, editors, Games of No Chance 3, volume 56 of Mathematical Sciences Research Institute Publications, pages 3–56. Cambridge University Press, 2009.
- [2] Lindsay Erickson. Nim on complete graphs. Ars Combinatoria, Accepted for publication in 2009.
- [3] Aviezri S. Fraenkel, Edward R. Scheinerman, and Daniel Ullman. Undirected edge geography. Theor. Comput. Sci., 112(2):371–381, 1993.
- [4] Masahiko Fukuyama. A nim game played on graphs. Theor. Comput. Sci., 1-3(304):387–399, 2003.
- [5] P. M. Grundy. Mathematics and games. Eureka, 2:198—211, 1939.
- [6] David Lichtenstein and Michael Sipser. Go is polynomial-space hard. J. ACM, 27(2):393–401, 1980.
- [7] C. H. Papadimitriou. Computational Complexity. Addison Wesley, Reading, Massachsetts, 1994.
- [8] R. P. Sprague. Über mathematische Kampfspiele. Tôhoku Mathematical Journal, 41:438—444, 1935-36.
- [9] Gwendolyn Stockman. Presentation: The game of nim on graphs: NimG, 2004. Available at http://www.aladdin.cs.cmu.edu/reu/mini_probes/papers/final_stockman.ppt%.
Appendix A Proof of Lemma 3.2
Lemma A.1 (Don’t Go Backwards)
Any play from to (for all ) is suboptimal.
We will refer to the player who moves from to (we will leave out the subscript for the internal vertices) as the “foe” while the other player is the “hero”. We will show that the hero has a winning strategy after a backwards move. We can now look at two cases, each depending on the state of the game outside the gadget.
The first is the case where the move from to would be a winning play. In this case, the hero can next move from to and take both of the objects there. The foe has two options, both of which, we show, allow the hero to win.
-
1.
The foe moves to . In this case the hero must choose to go to . The foe can now either choose to move to —in which case the hero will gladly move to and win as we assumed—or to . Then the hero simply takes the object at and, as there are no more moves, the hero has won.
-
2.
The foe moves to . The hero must then take and the foe must take . The hero can then move to and win the game.
The second major case assumes that the move from to is a losing play. Here, the hero can still move to (from ) but will take only one of the objects. Now the foe has three options: taking the other object at , moving to or moving to . We show all to be losses.
-
1.
Foe moves to . Now the hero should take the remaining object at . The following sequence must occur: foe must take , hero at , foe at , hero at , followed by the foe at , a losing move by our assumption.
-
2.
Foe takes the remaining object at . The hero will choose to take , so the foe must take . The hero can then take , forcing the foe to take , a losing move by our assumption.
-
3.
Foe takes . The hero should then take so the foe must take . Again, the hero can take , so the foe must move to , a losing move by our assumption.
Thus, it is a losing play to move from an -vertex to a -vertex.
Appendix B Proof of Lemma 3.3
Lemma B.1 (Stick to the Script)
Let the notation denote taking objects from vertex in a turn. Then, after the plays any play deviating from the following sequences is a losing move:
.
We continue by analyzing all possible deviations from these sequences and show that they result in a loss. In this claim, we will refer to the deviating player as the foe and the other player as the hero. We will show that the foe loses in each case. It may be helpful to refer to Figure 2 during these case descriptions.
-
1.
instead of . Here we have two subcases: either moving from to is a winning (result is a -position) or losing (a -position) move. If it’s in , then the hero can respond to with . If the foe then chooses , the hero can take the remaining stick in with . and must follow with the hero winning. If the foe instead chooses , the hero can win instantly by choosing . For the foe’s last chance, they could select , removing the other stick from . The hero should respond with . The foe will lose by selecting , because the hero will win at , but the foe will also lose with , an -position as assumed.
If is instead leaves the board in , the hero should respond to with . The foe could choose , but the hero can then win with . Instead, the foe can choose in which case the hero can choose and win, as assumed.
-
2.
instead of (the first) . Here the hero has a simple move to win. By taking there are no further moves and the foe has lost.
-
3.
instead of (the first) . The hero can respond with . This leaves two different adjacent vertices with 1 object apiece and no other adjacent non-empty vertices. Either move by the foe results in one remaining move and a win for the hero.
-
4.
instead of . The hero can respond with and win.
-
5.
instead of . This cannot happen in the second sequence, but if it happens in the first, the hero can respond with and win.
Thus, any deviation from the two sequences specified in the claim puts the game in an -position.