1. Introduction
Sudoku is a popular number placement puzzle. In an ordinary Sudoku (
Figure 1), given a partially completed
grid, the goal is to fill in all empty cells with digits from 1 to 9 in such a way that each cell has a single digit, and each digit appears only once in every row, column, and
subgrid.
Sudokus that appear in books, newspapers, and so on often have the following characteristics:
Regarding 1, for example, the left grid of
Figure 1 has reflection symmetry on the two diagonal axes. It may also have the shape of a number, a letter, or a symbol. Regarding 2, completing Sudokus generally requires backtracking, which is amenable to computers. On the other hand, there is a set of techniques (called
strategies) that humans use in solving Sudokus by hand [
1]. Typically strategies are if-then rules: If a strategy is applicable to the current grid, then it might rule out digits as candidates or might determine digits as those to be finally placed. A
strategy-solvable Sudoku is a partially completed grid that can be completed by applying strategies repeatedly. Here, the strategies must be selected from a set of predetermined ones. Since there is no need to guess, strategy-solvable Sudokus (at least for a few basic strategies) might be referred to as Sudokus having a moderate level of difficulty for humans.
Making new Sudoku instances having these characteristics is not an easy task. There is a public software (The generator of Zama and Sasano [
2]
http://www.cs.ise.shibaura-it.ac.jp/2016-GI-35/SudokuGenerator.tar.gz, accessed on 26 May 2020) that is able to generate Sudokus from a given set of clue positions so that they are solvable with some known strategies. However, it is still hard to generate those with 20 or less clues and those with around 45 or more clues. For such instances, there might be no other choice but to rely on human intelligence involving the intuition, the inspiration, and the experience of enthusiasts.
In this paper, we consider a method for determining Sudoku clues in specified positions such that all empty cells can be filled in with a specified set of strategies (see
Figure 2).
Almost all Sudoku generators simply output proper Sudoku instances (i.e., partially completed grids having unique solutions). Clue arrangements and strategies necessary for solving are uncontrollable. To the best of our knowledge, the only exception is the generator of Zama and Sasano mentioned earlier. Their generator is based on the generate-and-test method, which repeats the following steps until the test is passed or the number of trials exceeds a predetermined limit:
A similar idea is also mentioned in [
3,
4]. Since the test step can be done quickly, the key is to devise a criterion for the generation step so that the number of trials is as small as possible.
Since the generate-and-test method examines only a limited portion of the vast search space, it has the drawbacks listed below:
The lower the density of solutions (i.e., strategy-solvable clues) over the whole search space becomes, the harder it becomes to find a solution.
If a set of clue positions happens to be not strategy-solvable, it is unable to recognize it no matter how much time passes.
Even for a strategy-solvable set of clue positions, there is no guarantee for being able to find a solution in a finite amount of time.
To tackle these issues (in particular the last two), we consider it necessary to formulate the concept of strategy-solvability. It seems that strategy-solvability has been recognized intuitively, and no formal treatment has been given so far. In this paper, we introduce a rigorous framework to discuss solvability for Sudoku instances with respect to strategies and define the notion of strategy-solvable Sudoku clues. This allows us to handle not only known strategies but also general strategies under a few reasonable assumptions. We then propose an exact method for determining strategy-solvable Sudoku clues for a given set of clue positions that is solvable with a given set of strategies. The key is a reduction to a constraint satisfaction problem (CSP). Our method is able to benefit from the power of state-of-the-art CSP solvers. Pruning techniques of CSP solvers are expected to be effective for the first issue above. Moreover, as long as any complete CSP solver is utilized, it is guaranteed that if a set of clue positions is not strategy-solvable, our method eventually recognizes the unsolvability; otherwise, our method eventually finds strategy-solvable clues.
This is the first exact method except for a trivial brute-force search. As strategies allowed in completing Sudokus, this paper employs naked singles, hidden singles, and locked candidates [
1], which are quite basic yet powerful enough to complete many Sudokus. Indeed, we have confirmed that the combination of the three strategies allows for solving as many as 37,373 of the 49,151 minimum Sudokus (i.e., Sudokus with 17 clues) collected by Gordon F. Royle [
5]. Here we remark that our CSP formulation is almost independent of specific strategies. In order to allow other strategies, it is sufficient to formulate the corresponding logical constraints and add them with a minor modification of the strategy-independent part.
Besides the clue generation, we present an application of our method to the problem of determining the minimum number of Sudoku clues that are solvable with a given set of strategies. We demonstrate that our method is easily customized for this problem with a small modification in the CSP constraints. It is well-known that there is no proper Sudoku with 16 or less clues [
6]. There are many Sudokus with 17 clues that are solvable with basic strategies such as hidden singles. Interestingly, this may not be the case for naked singles, arguably one of the most basic strategies, as we have confirmed that no grid solvable with only naked singles is included in the minimum Sudoku collection. This poses an open problem of whether there is a gap between the minimum numbers for strategy-solvable Sudokus and proper Sudokus. Our method will be useful in tackling this problem thanks to the ability of determining unsolvability.
We conduct experiments to compare our method with the generator of Zama and Sasano [
2], using grids varying the position and the number of clues at random. From the results, we observe that our method terminates within 1 min for many instances, showing our method being stable in terms of running time. However, as the number of clues gets closer to 20, the running time rapidly increases and exceeds a time limit. On the other hand, the generator of Zama and Sasano can often find solutions much faster even with close to 20 clues, while the performance sharply deteriorates around 45 clues and exceeds the time limit for all grids with more clue positions. Perhaps this is due to the exponential blow-up of the search space (in other words, the exponential decline in the density of solutions). We also evaluate our method for several instances with 17 clue positions taken from known minimum Sudokus to see the efficiency for deciding unsolvability.
The main contributions of the present paper are to formulate the concept of strategy-solvability, to establish an exact method for the strategy-solvable Sudoku clues problem, and to demonstrate the flexibility of the CSP-based approach. It remains as future work to improve our method for less clues.
Unless otherwise noted, the size of the Sudoku is fixed to throughout the paper. This is simply for convenience and our method can be easily translated into general Sudokus on a grid.
The paper is organized as follows.
Section 2 introduces necessary notations and terminology, and explains strategies.
Section 3 formulates the concept of strategy-solvability and the strategy-solvable Sudoku clues problem.
Section 4 proposes an exact method for the strategy-solvable Sudoku clues problem, and
Section 5 presents two improvements for our method.
Section 6 presents an application of the strategy-solvable minimum Sudoku problem.
Section 7 presents experimental results.
Section 8 summarizes related work.
Section 9 concludes this paper.
3. Formulation
Although some known strategies are introduced in the previous section and some solvable cases are explained using particular grids, there are still unclear points, such as what is a strategy in general and what operations are allowed for placing or ruling out digits. In this section, we thus introduce a rigorous framework to discuss solvability for Sudoku instances. This allows us to handle not only known strategies but also general strategies under a few reasonable assumptions. This also makes it clear that, for example, it is not allowed to temporarily place digits and then cancel them when a contradiction occurs. Such an inference would allow us to do backtracking, which nullifies the restriction of completion methods.
A
state transition model of Sudoku is a tuple
such that
Q is the set of all possible states,
R is a state transition relation,
I is the set of initial states, and
F is the set of final states. We will explain each component below.
Figure 5 shows how a grid evolves as strategies are applied, which will be used as an example in the succeeding explanation.
A state of grid (or a state in short) is a pair of functions. The function f maps the set of cells to the set of digits, and means that if , then n is placed in ; otherwise, no digit is placed. The function g maps the set of cells to the powerset of the set of non-zero digits, and represents the set of all candidates at . Here, by abuse of notation, and denote and , respectively. All states must satisfy the conditions and below: for all cells and non-zero digits n,
- S1
,
- S2
if there is a different non-zero digit from n such that or there is a different cell from with both in a common group such that .
We denote by Q the set of all states.
The antecedent of
is a condition for digits
n that can be safely ruled out whenever some digits are placed. Imposing
ensures that all such digits are ruled out. This is simply to make all states have a type of normal form, and it would not deduce any wrong consequence as long as decisions for number placements are correct, such as not violating the properness. In
Figure 5 we can observe that all grids are in “normal form”.
A state transition relation is a binary relation . The (or in infix notation) means that there are strategies by which q is changed to . We refer to q and as the source state and the target state of the transition, respectively. All state transitions must satisfy that for any cell and non-zero digit n, if holds in the source state, it does also in the target state; if holds in the source state, it does also in the target state. This simply means that once digits are placed or ruled out it cannot be cancelled afterward. We put one more assumption as follows. For any three states , , and with and , if , then . We consider this assumption not so strong. Indeed, many strategies, such as naked singles and hidden singles, decide to place digits by examining only candidates. For such strategies, suppose that is obtained from by applying all strategies applicable to . Then, if , no strategy applicable to remains, and cannot be changed any more, i.e., .
A state
is an
initial state if it satisfies the following:
for all clue cells;
for the other cells; and
if and only if the antecedent of
holds. The last condition ensures that for each clue cell, all digits but the determined one are ruled out; all digits that appear as candidates in a common group to some determined digit are ruled out; and—the only-if part—no other digits are ruled out. Notice that the only-if part is the most essential because the if part can be omitted thanks to
. It is worth noting, that for non-initial states, this must not be assumed in general because of strategies for number removals such as locked candidates. For example, in the-left most grid of
Figure 5, we can observe that all digits not designated to be ruled out by the antecedent of
remain as candidates. We denote by
I the set of all initial states.
A final state is a state such that digits are determined for all cells, that is, for all . We denote by F the set of all final states.
Definition 1. Let be a state transition model of Sudoku. A state is strategy-solvable if there is a sequence of states such that , , and for all .
Definition 2. Let be a state transition model of Sudoku. The strategy-solvable Sudoku clues problem (or SSC in short) is to determine whether there is a strategy-solvable state of .
Suppose that R is given as a logical formula that represents strategies and a set S of clue cells is also given. Consider Q, I, and F as the ones defined as described above. Sudoku clues are strategy-solvable if the initial state corresponding to the Sudoku clues is strategy-solvable with respect to . The strategy-solvable Sudoku clues problem is defined accordingly. If the output of the problem is yes, in practice we will also ask for generating strategy-solvable Sudoku clues.
Proposition 1. Let be a state of grid. For any cells , in a common group, implies or .
Proof. Suppose that , , and . Let . Applying , we obtain . Since , applying , we obtain for all . From both, follows, and this is contradictory to . □
Corollary 1. Every final state represents a Sudoku solution.
Proof. Let be a final state. Since f is a function, each cell must have a single digit. From Proposition 1 and , it follows that each digit appears once in every group. □
Notice that this corollary holds no matter what state transition relation is given.
Proposition 2. Let R be a state transition relation. Let be a sequence of states such that for all . If for all , then .
Proof. Let be the number of all triples such that . Clearly, . Hence . □
8. Related Work
There are several methods for sudoku clue generation [
11]. As far as we are aware, most methods lack either a strategy-solvability aspect or a geometric constraint aspect, and they often aim at controlling the difficulty levels of the Sudoku instances to be generated. Since both aspects are at the heart of our present work, most methods are considered as intended to tackle different generation problems from ours. In the following, we briefly summarize them in terms of the differences from ours.
Known Sudoku generation methods are mainly classified into four types:
a local search,
a forward search,
a backward search, and
a logical modeling approach. The local search consists of starting with a partially-completed grid, which may be randomly generated or selected from known proper Sudoku instances, and then repeatedly searching neighbor grids by changing a portion of the current grid, such as relocating a few clue positions, removing a few clues, or replacing digits with different ones until a desired grid is found [
12].
The forward search consists of repeatedly generating an initial grid (typically at random) and checking if it has a unique solution using Sudoku solvers. The checking step is sometimes done without verifying uniqueness in an exact sense. For example, Mantere and Koljonen used a Sudoku solver based on genetic algorithms [
13]. In their method, an initial grid consists of randomly generated 20–50 clues and the check step is done in such a way that an initial grid is solved 10 times using their GA-based solver and it is checked if only one solution is obtained over the trials. Examples of other Sudoku solving methods include stochastic local searches, CSP solvers, and dancing links [
14]. The generator of Zama and Sasano [
2] falls into this group. Strategy-solvable Sudokus are guaranteed to be proper in an exact sense, and checking solvability can be done quickly, unlike checking properness. A similar idea is also mentioned in [
3,
4]. As far as we are aware, all known generators based on the forward search, with a few exceptions, do not take the strategy-solvability of Sudoku instances into consideration.
The backward search consists of starting with a completed grid and repeatedly eliminating digits so as not to violate properness; in other words, the resulting grid does not have distinct solutions [
15]. Boothby et al. used inverse operations of some known strategies to eliminate digits and propose a constrained breadth search [
16]. Their method admits a strategy-solvability aspect; however no known method based on the backward search can handle geometric constraints for an initial grid in the sense that no grid satisfying geometric constraints might appear during a search. Xue el al. used a Las Vegas algorithm to generate completed grids and used several operations (called dig-hole strategies) to eliminate digits.
The logical modeling approach consists of converting generation problems into equivalent constraints described in languages such as CSP (constraint satisfaction problem) and ASP (answer set programming), and to apply a generic solver to solve them. Our method falls into this group. Gebser provided an ASP encoding for proper Sudokus [
17]. Fatemi el al. used a CSP encoding, but the constraints were simply those ensuring that grids do not violate Sudoku rules [
18].
9. Conclusions
Sudokus that appear in books, newspapers, and so on often have a regular pattern in the arrangement of initial digits and are typically made so that all empty cells can be completed using some known techniques, called strategies. We formally defined the problem of generating such Sudoku instances by introducing the concept of strategy-solvability, which means that all empty cells can be filled in with digits using only a given set of strategies. We proposed an exact method for solving this problem. The key is to encode a given problem instance into an equivalent CSP instance and then solve it by applying a CSP solver.
There are a few existing researches, but all of them are based on the generate-and-test method, which repeatedly generates a set of clues and then tests whether it is strategy-solvable. There are some drawbacks, such as not being able to recognize that a specified set of cells is strategy-unsolvable.
To the best of our knowledge, our method is the first exact method except for the trivial brute-force search. Our method can eventually find strategy-solvable Sudoku clues if they exist, and otherwise our method can eventually recognize strategy-unsolvability. Besides the clue generation, we presented an application of our method to the problem of determining the minimum number of strategy-solvable Sudoku clues, demonstrating that our method is easily customized with a small modification.
We conducted experiments to compare our method with the generator of Zama and Sasano, using grids varying the positions and the numbers of clues at random. From the results we observed that our method terminated within 1 min for many grids, showing our method to be stable in terms of running time. However, as the number of clues got closer to 20, the running time rapidly increased and exceeded the time limit, which was set to 600 s. On the other hand, the generator of Zama and Sasano could often find solutions much faster even nearing 20 clues, while the performance sharply deteriorated around 45 clues and exceeded the time limit for all grids with more clue positions. We also evaluated our method for several instances with 17 clue positions taken from known minimum Sudokus to see the efficiency for deciding unsolvability. It remains for future work to improve our method for less clues.