On the Complexity of Solving Subtraction Games

08/10/2018 ∙ by Kamil Khadiev, et al. ∙ 0

We study algorithms for solving Subtraction games, which sometimes are referred to as one-heap Nim games. We describe a quantum algorithm which is applicable to any game on DAG, and show that its query compexity for solving an arbitrary Subtraction game of n stones is O(n^3/2 n). The best known deterministic algorithms for solving such games are based on the dynamic programming approach. We show that this approach is asymptotically optimal and that classical query complexity for solving a Subtraction game is generally Θ(n^2). This paper perhaps is the first explicit "quantum" contribution to algorithmic game theory.



There are no comments yet.


page 1

page 2

page 3

page 4

This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

1 Introduction

A Subtraction game is similar to a canonical Nim game [3]. The difference is that players deal with just one heap of stones but with some limitations imposed on the number of stones they can take from the heap. The most common limitation is defining maximum for the number of stones to be taken away, and this kind of games has nice combinatorial solutions. Here we study a much more general class of such limitations and thus a broader class of Subtraction games.

2 Definitions

2.1 Subtraction games

Let us formally define what we call a Subtraction game throughout this paper: It is a two-player game in which the players alternately remove some positive amounts of stones from a heap. Let be the initial number of stones in the heap, and be a (triangular) binary matrix of size . A player can remove stones once there remain exactly stones in the heap () iff . One who cannot make a legal move loses, and their opponent wins the game. Thus a player wins a game if he takes all the remaining stones, or leaves such number of stones that no allowed moves remain: .

Obviously, the rules of a Subtraction game are fully determined by such matrix , so hereafter we sometimes refer to as to a game. We also reserve name to denote the initial size of stones, which also corresponds to the dimension of the matrix . Note that there are only meaningful bits in the matrix , as a player cannot take more stones than there remain in the heap: for all .

2.2 Winning function

We define a Boolean function Win: iff the first player has a winning strategy in game .

We also extend its domain to all possible positions in the game: iff a player having stones in game has a winning strategy. In particular:

  • ;

  • ;

  • .

Hereinafter we alternately use Boolean and integer forms of these and other values: and .

2.3 Properties of Subtraction games

We call a game -balanced if the number of winning positions differs from the number of losing positions by at most :


We call a game sensitive if in each winning position a player has a unique winning move. Or, equivalently, if in each position a player can have at most one winning move:


In the next section we study sensitive -balanced games for small -s. Hereafter one can assume for simplicity, but all our considerations also hold for any . The exact value of only affects the size of the considered subset of games.

3 Classical query compexity

Lemma 1.

Let be a losing sensitive -balanced Subtraction game picked uniformly at random: . Let game differ from in exactly one random bit of the binary representation: . Then


We first make three assumptions for :

  • , which does not fully correspond to the uniform distribution for the considered subset of games, but is asymptotically equivalent to it (i.e. expected difference between the two pmf-s is neglectable for large

    -s). We leave this fact without formal proof, because even non-uniform distribution is sufficient for our purposes. We also neglect addend

    in this estimation.

  • . Informally: a winning position is achievable from any preceding position

    with probability

    . This assumption is perfectly correct since possibility or impossibility to make a losing move in any position leaves a game strategically unaffected.

  • . Informally: from a winning position , all subsequent losing positions are achievable equiprobably (and these probabilities sum up to ). This assumption also is perfectly correct due to the definition of the considered subset of games.

Now let for some pair of indices picked at random. Then:

  • Informally: each preceding gives some small chance of for a losing position to be accessible from a winning position .

  • Informally: even though each previously estimated chance was small, alltogether they result in some significant probability for a losing position to be accessible from at least one preceding winning position (of totally preceding positions).

We follow that once is a losing position, with probability it is achievable from some winning position . We shall consider the least if there happen to occur several (this can be important!).

On the other side, , so once is a losing position, then with probability position also is losing, and thus (since one losing position cannot be achievable from another losing position). And alltogether: with probability


we have .

Now let us observe what happens with in this (not so improbable) case: we have and . It implies since allows access from to the losing position . And then the only winning move in position becomes obsolete: (some other winning positions of game also may become losing in , but it definitely should happen with the “smallest” former winning position ).

Finally we note that with probability this difference also implies . This probability together with (3) results in the overall lower bound:

Theorem 2.

There is no deterministic or randomized algorithm for solving function Win faster than in steps.


We shall prove this impossibility by analyzing performance of the best classical algorithm on the set LSB of losing sensitive -balanced Subtraction games. But perhaps we first need to make a couple of remarks on the eligibility of such proof:

  • It may seem that LSB is a too small set, and that we are going to prove the lower bound for some negligible number of games. However, actually the number of such games is roughly while the total number of Subtraction games is roughly . That is, a game from LSB and an arbitrary Subtraction game are representable by asymptotically similar numbers of bits. Although just one example would be sufficient to make a statement about the “worst case” complexity, we here demonstrate that a significant part of all Subtraction games are hard to solve. We also note that we strongly believe that similar bound should also hold for the whole set of Subtraction games.

  • It may seem that since LSB contains only losing games (), one could design an algorithm which somehow recognizes that a game belongs to this set and returns answer . Actually we can easily refute this criticism by extending this set with sufficient number of -s which often are winning and always are hardly-distinguishable from the losing games of LSB. Formally, we can consider set .

Suppose a classical algorithm, given , reports an answer after querying on average less than bits of .

Obviously, such algorithm cannot pretend to be correct with probability more than , since Lemma 1 implies that there are on average such crucial bits that inverting the bit also inverts the value of the game . Leaving any such bit unchecked means failing to guess with adequate probability.

4 Quantum Algorithm

In this section we suggest a quantum algorithm for solving an arbitrary Subtraction game (should it belong to LSB or not). We consider directed acycling graph (DAG) with adjacency matrix : set corresponds to positions of the game , and set corresponds to all legal moves.

The algorithm applies dynamic programming approach [5, 2]: it solves a problem using precomputed solutions of smaller parts of the same problem. For analyzing DAGs it typically means usage of some modification of Depth-first search algorithm (DFS) as a subroutine [2].

We exploit the same idea, but for solving (for each vertex , starting from ) we use Grover’s Search algorithm [4, 1] to find a losing vertex among directly accessible vertices , s.t. . This algorithm has two important properties:

  • its time complexity is , where is the number of vertices directly accessible from the vertex ;

  • it returns the desired losing vertex with some constant probability (say ) if such vertex exists.

In Algorithm 1 we use Grover’s Search in form of function Grover_IsZeroAmong which returns or equiprobably if there is zero among its input bits, and returns if all inputs are ones. We store the search results in array and reuse them in all the subsequent searches.

for  do
     for  do
         if  then
         end if
     end for
end for
Algorithm 1 Quantum Algorithm for solving
Theorem 3.

Algorithm 1 computes in time and with error probability .


Correctness of the algorithm is obvious.

Time complexity follows from Cauchy-Bunyakovsky-Schwarz inequality:

The probability of error in evaluating one particular is , so the probability of no error at all among evaluations of is .

We note that for a random Subtraction game the expected number of edges and formulate the final conclusion: while the best classical algorithms require time to solve a Subtraction game, there exists a polynomially faster quantum algorithm which runs in time .


The work is performed according to the Russian Government Program of Competitive Growth of Kazan Federal University.

The research is supported by ERC Advanced Grant MQC, PostDoc Latvia Program, and by the ERDF within the project “Optimal quantum-entangled behavior under unknown circumstances”.