Solving Static Permutation Mastermind using $O(n \log n)$ Queries

Permutation Mastermind is a version of the classical mastermind game in which the number of positions $n$ is equal to the number of colors $k$, and repetition of colors is not allowed, neither in the codeword nor in the queries. In this paper we solve the main open question from Glazik, J\"ager, Schiemann and Srivastav (2021), who asked whether their bound of $O(n^{1.525})$ for the static version can be improved to $O(n \log n)$, which would be best possible. By using a simple probabilistic argument we show that this is indeed the case.


Introduction
Mastermind is a well known two-player board game, whose commercial version was introduced in the 1970's by Mordecai Meirowitz. The game goes as follows: the first player, named Codemaker, secretly chooses a codeword made up of 4 pegs, each of 6 possible colours. The second player, Codebreaker, then tries to guess this codeword in as few queries as possible. A query also consists of 4 pegs of 6 possible colours, and for each query Codebreaker receives the number of pegs they guessed correctly (black pegs) as well as the number pegs of the correct colour, but not at the right position (white pegs).
In 1977, Knuth [7] used a minimax argument to prove that five queries were sufficient to find any codeword. Numerous variants of this game have been studied in the literature. Some of the most common variants are: (i) the number of pegs n and the number of colours k may be arbitrary; (ii) in the black-peg only setting, Codebreaker only receives the black pegs as answer to their queries; (iii) when k n, one may or may not allow repetitions of colours in the codeword and the queries; (iv) in the static setting, Codebreaker needs to decide all queries in advance, before receiving any answer. In the adaptive setting, queries may be adapted depending on the answers to previous queries.
One of the first problem of this type to receive attention is Coin-Weighing, a problem introduced in 1960 by Shapiro and Fine [9], which can be proven equivalent to n-peg, 2colour Mastermind. In 1963, Erdős and Rényi [4] showed via a probabilistic argument that O (n/ log n) queries were sufficient to crack the code. Building up on their idea, Chvátal [1] showed the existence of a strategy for black-peg only Mastermind with repetition, using O(n log k/ log n) queries when k n 1−ε . This matches the information-theoretic lower bound in both the adaptive and static cases up to a constant factor.
Determining the minimum number of queries for static, black-peg only Mastermind with repetitions is part of the wider problem of determining the metric dimension of graphs. In a recent paper, Jiang and Polyanskii [6] determined, up to lower order terms, the metric dimension of large powers of fixed graphs. Concerning Mastermind, their result imply that for any constant number of colours k, the query complexity is (2 + o(1))n/ log k n.
For larger k's, determining the correct query-complexity turned out to be more delicate. When k = n, Chvátal's approach can easily be adapted to find a bound of O(n log n), a factor log n away from the information-theoretic lower bound of Ω(n) (attributed to Duchet, see [2]). Doerr, Doerr, Spöhel and Thomas [2] showed that this was the correct bound (up to constant factors) in the static case. They also presented an adaptive strategy using O(n log log n) queries, by giving a reduction to a sequence of Coin-Weighings problems. The gap to the lower bound was finally closed in a recent paper by Martinsson and Su [8], who showed that by first performing a sequence of O(n) simple "pre-processing" queries, Coin-Weighing schemes can be modified to match the information-theoretic lower bound for Mastermind. In the adaptive case, optimal bounds for k > n can be deduced from this bound, we refer to [2] and [8] for the details.
In the case of Permutation Mastermind we assume k = n and allow no repetitions of colors, neither in the codeword nor in the queries. In this setting, one usually views the codeword and the queries as permutations of [n], thus the name. It is trivial to see that black-peg only, and black-and white-peg settings are equivalent in this case. Restricting the codeword to permutations obviously makes it easier for Codebreaker to win, on the other hand this same restriction for queries rules out many of the usual strategies. In the adaptive setting, the best lower-and upper-bounds currently, of order Ω(n) and O(n log n), are both due to El Ouali, Glazik, Sauerland and Srivastav [3]. Where the exact query-complexity lies inside this Θ(log n) gap is still an open question.
Recently, Glazik, Jäger, Schiemann and Srivastav [5] shed some light on Static Permutation Mastermind. Adapting an argument of Doerr, Doerr, Spöhel and Thomas [2], they proved that any strategy requires at least Ω(n log n) queries. Additionally, they show that there exists a strategy using O(n 1.525 ) queries and ask whether it is possible to find a strategy matching their lower bound. In this paper we answer this question positively. Theorem 1. For Static Permutation Mastermind, there exists a set of O(n log n) queries from which any codeword can be recovered.
Our proof uses the probabilistic method. We show that with positive probability, a random sequence of 28n log n queries chosen uniformly at random has the property that it can uniquely determine all codewords, hence there are deterministic sequences with this property.
Additionally, one easily observes that given such a good sequence, our proof of Theorem 1 can be turned into a simple algorithm which recovers the codeword efficiently from such a sequence (by finding one colour at a time in n turns). This contrasts with Chvátal's original result for k n 1−ε , where no corresponding efficient reconstruction scheme has been presented in the literature, leaving codebreaker to brute-force a codeword that matches all queries. In the case of static classical Mastermind with k = n, our approach could be adapted to have an efficient reconstruction algorithm, but we will not elaborate on this further.
For a quick overview of the state of the art on Mastermind, we provide the best known lower-and upper-bounds for the case k = n in Table 1. The rest of this paper is dedicated to the proof of Theorem 1.

Proof of the Theorem
We use standard notations. By log we denote the natural logarithm. For the sake of conciseness we omit floor and ceil signs. Whenever it is needed, we assume that n is large enough. All permutations are assumed to be over [n] = {1, . . . , n}.
Our proof of Theorem 1 is probabilistic and in certain ways resembles the original proof of Chvátal. Consider a set of queries Q of size q(n) chosen independently and uniformly at random from all permissible queries. If we could show that it allows to decode a randomly chosen codeword with probability 1 − o(1/n!), then a simple union bound argument allows us to conclude that there has to exist such a set of queries of size q(n) that allows to decode all codewords. Now, as it turns out, a success probability 1 − o(1/n!) in this case is too optimistic. For instance, with probability 1/poly(n) a collection of O(n log n) random queries will never query colors 1 or 2 in positions 1 or 2, in which case, distinguishing permutations beginning with 12 and 21 is impossible. Instead, to get the union bound to follow through, we need a more refined picture of what "bad events" could cause the recovery to fail. This is made formal in Lemma 2; first, we introduce some definitions.
Given a set I ⊆ [n], we say that c ∈ [n] I is a colouring of I. We say that it is a valid colouring of I if c(i) = c(j) for all i = j, in particular c may be viewed as the restriction of a permutation to I. Given a permutation σ and a colouring c, we say that σ is a 0-query for c if σ(i) = c(i) for all i ∈ I. Given a permutation σ and two colourings c 1 , c 2 , we say that σ discriminates c 1 from c 2 on I if σ is a 0-query for c 1 but not for c 2 . The following lemma is the core of our argument.

Lemma 2.
There exists a set Q of 28n log n permutations such that the following holds. For any I ⊆ [n], any valid colouring v and any colouring c on I such that v(i) = c(i) for all i ∈ I, there exists some q ∈ Q which discriminates v from c.
We emphasise that in the above lemma c can be any colouring while v needs be a valid one. We defer the proof of this lemma to the end of this section and now prove Theorem 1.
Proof of Theorem 1. Let Q be the set as in Lemma 2. Our strategy to recover any codeword c 0 is to find the colours one by one until we have found them all. We let I be the set of pegs for which we have not found the correct colour; at the beginning I = [n].
At any step when |I| 1, because we know the correct colours outside of I, we can compute for each query q ∈ Q how many positions c 0 and q colour identically in I. Let Q I = {q 1 , . . . , q l } ⊆ Q be the set of 0-queries for c 0 on I. We claim that there must exist some i ∈ I such that {c 0 (i), q 1 (i), . . . , q l (i)} = [n]. Indeed, if this were not the case, then there would exist some colouring c (which need not be valid) such that c(i) / ∈ {c 0 (i), q 1 (i), . . . , q l (i)} for all i ∈ I. However, this would imply that no q ∈ Q I (and hence, in Q) discriminates c 0 (or rather its restriction to I) from c on I, and this contradicts the definition of Q. In consequence, such an i ∈ I exists, and we can recover c 0 (i): it is the unique element of [n] \ {q 1 (i), . . . , q l (i)}. We remove this i from I and continue until I = ∅.
All that remains is now to prove Lemma 2.
Proof of Lemma 2 . The proof is probabilistic: we choose independently and uniformly at random a set of 28n log n queries and show that with positive probability, this set satisfies the conditions.
To do this, we wish to apply union-bound over all (I, v, c). A rough application will not work: indeed there are roughly n (2+o(1))n choices of triple (I, v, c), but the probability of failing to discriminate a certain triple may be as large as 1/poly(n) if I is small. To solve this problem, we partition the set of triples (I, v, c) depending on the size of I: when k = |I| is small, there are fewer choices for (I, v, c) so it is fine if the probability of discriminating is smaller.
Concretely, assume that for any fixed triple (I, v, c) as in the statement, a random colouring has probability at least |I|/7n of discriminating v from c. Then the probability that none of the 28n log n random queries discriminate is at most (1 − |I|/7n) 28n log n e −4|I| log n = n −4|I| .
When |I| = k, a crude upper bound for the choice of (I, v, c) is n 3k in total. Hence by union bound, the probability that there exists a triple (I, v, c) with I = ∅ for which no colouring is discriminating is at most n k=1 |I|=k v,c n −4|I| n k=1 n 3k n −4k < 1, whenever n 2.
Therefore, all we need to do is prove that, indeed, for any fixed (I, v, c) as above, a uniformly random permutation has probability at least |I|/7n of discriminating. To bound this, we denote by S i the set of those permutations σ which are 0-queries for v and such that σ(i) = c(i); we now want to show that the size of i∈I S i is at least |I|(n−1)!/7. Using inclusion-exclusion, we get To estimate this quantity we shall compute the sizes of S i and S i ∩ S j . We express them in terms of A(n, |I|) which we define as the number of 0-queries of v on I. If we let m = |I|, write S n for the set of all permutations on [n] and T i for the set of permutations σ such that σ(i) = v(i), then a simple application of the inclusion-exclusion principle gives Claim 3. For all m n we have n!/3 A(n, m) n!.
Proof. Since A(n, m) is the size of a subset of permutations of [n], the upper bound is trivial. For the lower bound recall that the expression of A(n, m) stems from an inclusionexclusion argument. As it is well known, that inclusion-exclusion alternatively over-resp. underestimates we get By spelling out these terms and grouping them appropriately we get A(n, m) (1 − m n )n! + m(m−1) 6n(n−1) 3 − m−2 n−2 n!, from which the claimed bound follows easily by observing that the last bracket is at least two and the remaining terms are monotonous in m.
the electronic journal of combinatorics 29(1) (2022), #P1.20 Using the above, we now find expressions for the sizes of |S i |, |S i ∩ S j |. Proof. We prove this claim by appropriate double counting arguments. For a permutation σ that is a 0-query for v we denote by the index such that σ( ) = c(i). If ∈ I or σ(i) = v( ) then the permutation that is obtained from σ by swapping the colours at positions i and belongs to S i . (Note that this case includes the case i = , as then σ belongs to S i already.) As there exist only n(n − 2)! permutations for which σ(i) = v( ), we thus have For (ii), we first note that S i ∩S j = ∅ when c(i) = c(j), thus in this case the claim holds trivially. In the following we thus assume c(i) = c(j). Consider an arbitrary σ ∈ S i ∩ S j . For some (arbitrary) indices 1 and 2 , perform the following operation: swap the colours at position i and 1 and at positions j and 2 . When do we get a 0-query for v? Clearly, a sufficient condition is to ensure that 1 and 2 are different from i and j, the colours of σ( 1 ) and σ( 1 ) do not coincide with v(i) resp. v(j), and -for 1 , 2 ∈ I -the colors of σ(i) and σ(j) does not coincide with that of v( 1 ) resp. v( 2 ). Clearly, we thus have at least (n − 4)(n − 5) proper choices for 1 and 2 . Observe in addition that for each permutation σ that is obtained by such a double swap of i, 1 and j, 2 , there is a unique (σ, 1 , 2 ) from which it can be obtained: 1 (resp. 2 ) is the index such that σ ( 1 ) = c(i) (resp. σ ( 2 ) = c(j)) and σ is obtained from σ by swapping the colours of i, j with those of 1 , 2 respectively. Thus we have (n − 4)(n − 5)|S i ∩ S j | A(n, |I|).
The formulas as in (i) and (ii) follow immediately from the above and the bounds of Claim 3, where we catch the change to n(n − 1) in the o(·)-notation. For a choice of n large enough, this is at least |I|(n − 1)!/7 as claimed.