On the Query Complexity of Black-Peg AB-Mastermind

Mastermind game is a two players zero sum game of imperfect information. The first player, called codemaker, chooses a secret code and the second player, called codebreaker, tries to break the secret code by making as few guesses as possible, exploiting information that is given by the codemaker after each guess. In this paper, we consider the so called Black-Peg variant of Mastermind, where the only information concerning a guess is the number of positions in which the guess coincides with the secret code. More precisely, we deal with a special version of the Black-Peg game with n holes and k<=n colors where no repetition of colors is allowed. We present upper and lower bounds on the number of guesses necessary to break the secret code. We first come back to the upper bound results introduced by El Ouali and Sauerland (2013). For the case k=n the secret code can be algorithmically identified within less than (n-3)*ld(n)+5n/2 queries. That result improves the result of Ker-I Ko and Shia-Chung Teng (1985) by almost a factor of 2. For the case k>n we prove an upper bound for the problem of (n-1)*ld(n)+k+1. Furthermore we prove a new lower bound for (a generalization of) the case k=n that improves the recent result of Berger et al. (2016) from n-log(log(n)) to n. We also give a lower bound of k queries for the case k>n.


Introduction
In this paper we deal with Mastermind, which is a popular board game that in the past three decades has become interesting from the algorithmic point of view.Mastermind is a two players board game invented in 1970 by the postmaster and telecommunication expert Mordecai Meirowitz.The idea of the game is that the codemaker chooses a secret color combination of n pegs from k possible colors and the codebreaker has to identify the code by a sequence of queries and corresponding information that is provided by the codemaker.All queries are also color combinations of n pegs.Information is given about the number of correctly positioned colors and further correct colors, respectively.Mathematically, the codemaker selects a vector y ∈ [k] n and the codebreaker gives in each iteration a query in form of a vector x ∈ [k] n .The codemaker replies with a pair of two numbers, called black(x, y) and white(x, y), respectively.The first one is the number of positions in which both vectors x and y coincide and the second one is the number of additional pegs with a right color but a wrong position: black(x, y) = |{i ∈ [n]; x(i) = y(i)}|, white(x, y) = max σ∈Sn |{i ∈ [n]; y(i) = x(σ(i))}| − black(x, y).
The Black-Peg game is a special version of Mastermind, where answers are provided by black information, only.A further version is the so-called AB game in which all colors within a code must be distinct.In this paper, we deal with a special combination of the Black-Peg game and the AB game, where both the secret vector and the guesses must be composed of pairwise distinct colors (k ≥ n) and the answers are given by the black information, only.
Related Works: In 1963, several years before the invention of Mastermind as a commercial board game, Erdös and Rényi [7] analyzed the same problem with two colors.One of the earliest analysis of this game after its commercialization dealing with the case of 4 pegs and 6 colors was done by Knuth [17].He presented a strategy that identifies the secret code in at most 5 guesses.Ever since the work of Knuth the general case of arbitrary many pegs and colors has been intensively investigated in combinatorics and computer science literature.In the field of complexity, Stuckman and Zhang [21] showed that it is N Pcomplete to determine if a sequence of queries and answers is satisfiable.Concerning the approximation aspect, there are many works regarding different methods [2,3,4,6,9,10,11,12,14,19,20,21].The Black-Peg game was first introduced by Chvátal for the case k = n.He gave a deterministic adaptive strategy that uses 2n⌈log 2 k⌉ + 4n guesses.Later, Goodrich [13] improved the result of Chvátal for arbitrary n and k to n⌈log 2 k⌉ + ⌈(2 − 1/k)n⌉ + k guesses.Moreover, he proved in the same paper that this kind of game is N P-complete.A further improvement to n⌈log 2 n⌉ + k − n + 1 for k > n and n⌈log 2 n⌉ + k for k ≤ n was done by Jäger and Peczarski [15].Recently, Doerr et al. [5] improved the result obtained by Chvátal to O(n log log n) and also showed that this asymptotic order even holds for up to n 2 log log n colors, if both black and white information is allowed.For the AB game Jäger and Peczarski [16] proofed exact worst-case numbers of guesses for fixed n ∈ {2, 3, 4} and arbitrary k.Concerning the combination of both variants, Black-Peg game and AB game, for almost 3 decades the work due to Ker-I Ko and Shia-Chung Teng [18] was the only contribution that provides an upper bound for the case k = n.They presented a strategy that identifies the secret permutation in at most 2n log 2 n + 7n guesses and proved that the corresponding counting problem is #P-complete.
Our Contribution: In this paper we consider the Black-Peg game without color repetition.We first present a polynomial-time algorithm that identifies the secret permutation in less than n log 2 n + 3  2 n queries in the case k = n and in less than n log 2 n + k + 2n queries in the case k > n.The constructive strategy origined in the work of El Ouali and Sauerland [8].Our result for the case k = n improves the result of Ker-I Ko and Shia-Chung Teng [18] by almost a factor of 2. Furthermore we analyze the worst-case performance of query strategies for both variants of the Game and give a new lower bound of n queries for the case k = n, which improves the recently presented lower bound of n − log log(n) by Berger et al [1].We note, however, that the corresponding asymptotic bound of O(n) is long-established.For k > n we give a lower bound of k.Both lower bounds even hold if the codebreaker is allowed to use repeated colors in his guesses.

Upper Bounds on the Number of Queries
We first consider Black-Peg Mastermind with k = n and the demand for pairwise distinct colors in both the secret code and all queries, i.e., we deal with permutations in S n .

The Case k = n: Permutation-Mastermind
For convenience, we will use the term permutation for both, a mapping in S n and its one-line representation as a vector.Our algorithm for finding the secret permutation y ∈ S n includes two main phases which are based on two ideas.In the first phase the codebreaker guesses an initial sequence of n permutations that has a predefined structure.In the second phase, the structure of the initial sequence and the corresponding information by the codemaker enable us to identify correct components y i of the secret code one after another, each by using a binary search.Recall, that for two codes w = (w 1 , . . ., w n ) and x = (x 1 , . . ., x n ), we denote by black(w, x) the number |{i ∈ [n] | w i = x i }| of components in which w and x are equal.We denote the mapping x restricted to the set {s, . . ., l} with (x i ) l i=s , s, l ∈ [n].
Phase 2. The strategy of the second phase identifies the values of y one after another.This is done by using two binary search routines, called findFirst and findNext, respectively.The idea behind both binary search routines is to exploit the information that for 1 ≤ i, j ≤ n− 1 we have While, except for an unfrequent special case, findFirst is used to identify the first correct component of the secret code, findNext identifies the remaining components in the main loop of the algorithm.Actually, findFirst would also be able to find the remaining components but requires more guesses than findNext (twice as many in the worst case).On the other hand, findNext only works if at least one value of y is already known such that we have to identify the value of one secret code component in advance.
Identifying the First Component: Equation (1) implies that either black(σ j , y) = 1 holds for all j ∈ [n] or that we can find a j ∈ [n] with black(σ j , y) = 0.
In the first case, which is unfrequent, we can find one correct value of y by guessing at most n 2 + 1 modified versions of some initial guess, say σ 1 .Namely, if we define a guess σ by swapping a pair of components of σ 1 , we will obtain black(σ, y) = 0, if and only if one of the swapped components has the correct value in σ 1 .
In the frequent second case, we find the first component by findFirst in at most 2⌈log 2 n⌉ guesses.The routine findFirst is outlined as Algorithm 1 and works as follows: In the given case, we can either find a j ∈ [n − 1] with black(σ j , y) > 0 but black(σ j+1 , y) = 0 and set r := j + 1, or we have black(σ n , y) > 0 but black(σ 1 , y) = 0 and set j := n and r := 1.We call such an index j an active index.Now, for every l ∈ {2, 3, . . ., n} we define the code and call the peg at position l in σ j,l the pivot peg.From the information σ j i = σ r i+1 for 1 ≤ i ≤ n − 1 we conclude that σ j,l is actually a new permutation as required.The fact that black(σ r , y) = 0 implies that the number of correct pegs up to position l − 1 in σ j is either black(σ j,l , y) (if y l = σ r 1 ) or black(σ j,l , y) − 1 (if y l = σ r 1 ).For our algorithm, we will only need to know if there exist one correct peg in σ j up to position l − 1.The question is cleared up, if black(σ j,l , y) = 1.On the other hand, if black(σ j,l , y) = 1, we can define a new guess ρ j,l by swapping the pivot peg with a wrong peg in σ j,l .We define assuming for the case l = n, that we know that σ j 1 = y 1 .We will obtain black(ρ j,l , y) > 0, if and only if the pivot peg had a wrong color before, meaning that there is one correct peg in σ j in the first l − 1 places.Thus, we can find the position m of the left most correct peg in σ j by a binary search as outlined in Algorithm 1. Guess σ j,l := (σ j i ) l−1 i=1 , σ r 1 , (σ r i ) n i=l+1 ; 9 s := black(σ j,l , y); 13 Guess ρ j,l ; Note, that the term black(σ, x) is known by the codebreaker.After the first component of y has been found and fixed in x, there exists a j ∈ [n] such that black(σ j , y, x) = 0.As long as we have open components in x, we can either find a j ∈ [n − 1] with black(σ j , y, x) > 0 but black(σ j+1 , y, x) = 0 and set r := j + 1, or we have black(σ n , y, x) > 0 but black(σ 1 , y, x) = 0 and set j := n and r := 1.Again, we call such an index j an active index.Let j be an active index and r its related index.Let c be the color of some component of y that is already identified and fixed in the partial solution x.With l j and l r we denote the position of color c in σ j and σ r respectively.The peg with color c serves as a pivot peg for identifying a correct position m in σ j that is not fixed, yet.There are two possible modes for the binary search that depend on the fact if m ≤ l j .The mode is indicated by a Boolean variable leftS and determined by lines 4 to 8 of findNext.Clearly, m ≤ l j if l j = n.Otherwise, the codebreaker guesses By the information σ j i = σ r i+1 we obtain that (σ j i ) . We further know that every open color has a wrong position in σ r .For that reason, black(σ j,0 , y, x) = 0 implies that m ≤ l j .
The binary search for the exact value of m is done in the interval [a, b], where m is initialized as n and 18 Guess σ j,l ; In the first case, the first l − 1 components of σ j,l coincide with those of σ j .The remaining components of σ j,l cannot coincide with the corresponding components of the secret code if they have not been fixed, yet.This is because the l-th component of σ j,l has the already fixed value c, components l + 1 to l j coincide with the corresponding components of σ r which satisfies black(σ r , y, x) = 0 and the remaining components have been checked to be wrong in this case.Thus, there is a correct open component on the left side of l in σ j , if and only if black(σ j,l , y, x) = 0.In the second case, the same holds for similar arguments.Now, if there is a correct open component to the left of l, we update the binary search interval The Main Algorithm.The main algorithm is outlined as Algorithm 3.

It starts with an empty
Algorithm 3: Algorithm for Permutations 1 Let y be the secret code and set x := (0, 0, . . ., 0); 2 Guess the permutations Find the position m of the correct peg in σ 1 by at most n 2 + 1 further guesses; 7 else 8 Call findFirst for an active j ∈ [n] to find the position of the correct peg in σ j by at most 2⌈log 2 n⌉ further guesses; Choose an active index j ∈ [n]; 13 m := findNext(y, x, j); 16 Make at most two more guesses to find the remaining two unidentified colors; partial solution and finds the components of the secret code y one-by-one.Herein, the vector v does keep record about the number of open components in which the permutations σ 1 , . . ., σ n equal y and is, thus, initialized by As mentioned above, the main loop always requires an active index.For that reason, if v = 1 n in the beginning, we fix one solution peg in σ 1 and update x and v, correspondingly.Every call of findNext in the main loop augments x by a correct solution value.Since one call of findNext requires at most 1 + ⌈log 2 n⌉ guesses, Algorithm 3 does not need more than (n − 3)⌈log 2 n⌉ + 5  2 n − 1 queries (inclusive at most n 2 + 1 initial and 2 final queries, respectively) to break the secret code.
Example.We consider the case n = k = 8 and suppose that the secret code y is Figure 1 shows n possible initial queries.We illustrate the procedure findNext and further suppose that  we have already identified the positions of 3 colors indicated in the partial solution x: From the n 3 values in Figure 1 we see that black(σ 3 , y, x) = 1 and black(σ 4 , y, x) = 0, so we choose 3 as our active index applying findNext with the highlighted initial queries, σ 3 and σ 4 .Choosing the already identified color 2 as a pivot color, findNext does its binary search to identify the next correct peg as demonstrated in Figure 2. Since the information n 3 for query σ a is 0 (cf.lines 5-7 of Algorithm 2) all correctly placed pegs in σ 3 are on the left side of the pivot peg.Thus, we can apply a binary search for the left most correct peg in the first 4 places of query σ 3 using the pivot peg.here, the binary search is done by queries σ b and σ c and identifies the peg with color 7 (in general, the peg that is left to the most left pivot position for which n 3 is non-zero).If the response to σ a would have been greater than 0, we would have found analogously a new correct peg in σ 3 on the right side of the pivot peg.

The Case k > n
Now, we consider the variant of Black-Peg Mastermind where k > n and color repetition is forbidden.Let y = (y 1 , . . ., y n ) be the code that must be found.We use the same notations as above.
Phase 1.Consider the k permutations σ 1 , . . ., σ k , where σ 1 corresponds to the identity map on [k] and for j ∈ [k − 1], we obtain σ j+1 from σ j by a circular shift to the right.We define k codes σ 1 , . . ., σ k by σ j = (σ j i ) n i=1 , j ∈ [k].Within those k codes, every color appears exactly once at every position and, thus, we have k j=1 black(σ j , y) = n, similar to (1).Since k > n, this implies that Lemma 1.There is a j ∈ [k] with black(σ j , y) = 0. Phase 2. Having more colors then holes, we can perform our binary search for a next correct position without using a pivot peg.The corresponding simplified version of findNext is outlined as Algorithm 4. Using that version of findNext also allows to simplify our main algorithm (Algorithm 3) by adapting lines 2 and 3, and, due to Lemma 1, skipping lines 4-10.Thus, for the required number of queries to break the secret code we have: the initial k − 1 guesses, a call of the modified findNext for every but the last two positions (at most ⌈log 2 n⌉ guesses per position) and one or two final guesses.This yields, that the modified Mastermind Algorithm breaks the secret code in at most (n − 2)⌈log 2 n⌉ + k + 1 queries.3 Lower Bounds on the Number of Queries In the following we consider the case that the secret code has no repetition but arbitrary questions are allowed.Note that the lower bounds for that case especially hold true for AB-Mastermind and Permutation-Mastermind, respectively, since the codebreaker will not be able to detect a secret code with less attempts, if the set of allowed queries is restricted to the corresponding subset.Similar to the upper bounds, we proof the respective lower bounds on the necessary number of queries by construction.In particular, non of the first n − 1 queries will be answered with n.Thus, the secret code can not be identified with less than n queries.

14 s 15 if s > 0 then 16 b
:= black(ρ j,l , y); := l − 1; 17 if b < m then m := b; 18 else a := l; 19 Return m; Identifying a Further Component: For the implementation of findNext we deal with a partial solution vector x that satisfies x i ∈ {0, y i } for all i ∈ [n].We call the (indices of the) non-zero components of the partial solution fixed.They indicate the components of the secret code that have already been identified.The (indices of the) zero components are called open.Whenever findNext makes a guess σ, it requires to know the number of open components in which the guess coincides with the secret code, i.e. the number black(σ, y, x) := black(σ, y) − black(σ, x).

Algorithm 2 : 7 1 i=1 , (σ j i ) n i=lj +1 ; 8 s
Function findNext input : Code y, partial solution x = 0 and an active index j ∈ [n] output: Position m of a correct open component in σ j 1 if j = n then r := 1 ; 2 else r := j + 1; 3 Choose a color c with identified position (a value c of some non-zero component of x); 4 Let l j and l r be the positions with color c in σ j and σ r , respectively; 5 if l j = n then leftS := true; 6 else Guess σ j,0 := c, (σ j i ) lj −:= black(σ j,0 , y, x); 9 if s = 0 then leftS := true; 10 else leftS := false; 11 if leftS then let a := 1 and b := l j ; 12 else let a := l r and b := n; 13 m := n ; // position to be found 14 while b > a do 15 l := ⌈ a+b 2 ⌉ ; // position for peg c 16 if leftS then σ j,l :=

19 s 20 if s > 0 then 21 b
:= black(σ j,l , y, x); := l − 1; 22 if b < m then let m := b; 23 else a := l; 24 Return m; [a, b] as [a, b] := [1, l j ] if leftS [l r , n] else (lines 9 to 11 of findNext).In order to determine if there is an open correct component on the left side of the current center l of [a, b] in σ j we can define a case dependent permutation: [a, b] by [a, l − 1] and set m := min(m, l − 1).Otherwise, we update [a, b] by [l, b].

Figure 2 :
Figure 2: Binary search queries to extend the partial solution.The highlighted subsequences correspond to the subsequences of the selected initial queries.

3. 1 2 .
The Case k = n: Permutation-Mastermind Notice that the achieved bound for the case k = n especially holds for Permutation-Mastermind.In each iteration, the worst case for the code breaker is simulated by allowing the code maker to replace his secret code with another permutation from the remaining feasible search space.For m ∈ N we denote the m-th query of the code breaker with x m and the m-th secret code adaption of the code maker with y m .The remaining feasible search space R m consists of all permutations that agree with the first m pairs of queries and answers: R m := {σ ∈ S n | ∀j ∈ [m] : black(y j , x j ) = black(σ, x j )}.Now, a simple strategy of the code maker is to reply every query x m , m ∈ N, with the smallest possible number b m := min σ∈Rm−1 black(σ, x m ), choosing his new secret code y m ∈ R m−1 such that black(y m , x m ) = b m .We obtain our lower bound on the necessary number of queries by proving the following Lemma It holds that b m ≤ m for all m ∈ N.
Figure1: Initial queries σ j with associated responses n 1 = black(σ j , y), coincidences with a partial solution n 2 = black(σ j , x), and the difference of both n 3 .