Keywords

1 Introduction

Sudoku is a mathematician game, widely known for being a very entertaining pastime, which consists in assigning numbers in a game board. Traditionally this board is a grid of \(9 \times 9\) size, then there exists 81 positions which are called cells. The grid is composed of 9 columns, 9 rows and 9 \(3 \times 3\) sub-grids; each of these structures containing 9 cells. The game begins with distinct integers from 1 to 9 sparsely assigned in the grid, these are called “givens”. The givens must be at least 17 digits to ensure that the game will have a unique solution [7].

Fig. 1.
figure 1figure 1

Example of a Sudoku instance with 24 givens.

An instance of the game is completed, when all cells have a assigned value, but it is not so easy, the digit assignations must comply the problem constraints. The constraints can be reduced to a single statement which says, every cell must have a number, it must be unique in the row, column and sub-grid which it belongs to. Then, if the statement is separated in single constraints, we have 3 constraints for every cell making this game very difficult to solve (Fig. 1).

Such a puzzle belongs to the NP-complete collection of problems [14], this problem has widely been studied, proposing several approaches during the last years to solve it, from complete search methods such as constraint programming [10, 11] and boolean satisfiability [3] to incomplete search methods such as genetic programming [5], metaheuristics in general [1, 2, 6, 8, 9], and hybrid algorithms  [12, 13]. The main interest of the Sudoku problem is due to some instances are very difficult to solve.

To tackle this issue, we have used a filtering technique in order to validate the assignation from users, moreover to meet the possible value assignation to facilitate the Sudoku resolution and determining whether or not is a solution in an earlier stage of the resolution process by users, in relation to assigned and given values. The filtering technique chosen is named Arc Consistency 3 (AC3) from the Constraint Programming domain, which is an algorithm that enforces the arc consistency property on a constraint network. After reviewing its resolution process, we have realized that the resolution phase is much related to the strategies employed by users in order to solve the instances, but in contrast, this technique is executed in a short time, offering a good resolution guide to the users.

The effectiveness of filtering techniques was addressed performing an evaluation; it was realized in order to identify the quality of filtering over the different difficulties of the Sudoku puzzle. In general, filtering techniques make easier solving Sudoku puzzles, providing good information to users for this.

This paper is organized as follows. In Sect. 2, the employed filtering technique is explained. We describe the game prototype in Sect. 3, and the corresponding discussions. Finally, our conclusions are presented.

2 Filtering Technique: Arc Consistency

The Arc Consistency property comes from the Constraint Programming (CP) domain. CP is a programming paradigm where the mathematical problems are stated as variables and constraints, and then solved by a CP solver. A problem which is stated in terms of variables and constraints is called Constraint Satisfaction Problem (CSP). In order to resolve a CSP, the state of the variable are searched and the restrictions must be satisfied simultaneously. This property establishes a relation between two variables and maintain only feasible values in each variable domains, permitting to reduce the combinatorial search space, deleting the unfeasible values.

To ensure that a problem is arc consistent, it is needed to apply a filter technique, existing different algorithms to ensure the property, the algorithm used in this research was Arc Consistency Algorithm #3, defined by Alan Mackworth [4].

To apply the mentioned algorithm, the problem must be stated as a CSP, then a 9 \(\times \) 9 Sudoku can be modeled as:

  • \(X = (x_1{_1}, \ldots , x_9{_9})\) is the sequence of variables, and \(x_i{_j} \in X\) identifies the cell placed in the \(i^{th}\) row and \(j^{th}\) column of the Sudoku matrix, for \(i = 1, \ldots ,9\) and \(i = 1, \ldots ,9\).

  • D is the corresponding set of domains, where \(D(x_i{_j}) \in D\) is the domain of the variable \(x_i{_j}\);

  • C is the set of constraints defined as follows:

    • To ensure that values are different in rows and columns: \(x_{k,i} \ne x_{k,j} \wedge x_{i,k} \ne x_{j,k}\), \(\forall ( k \in [1,9], i \in [1,9], j \in [i+1,9])\)

    • To ensure that values are different in sub-squares: \(x_{(k1 - 1) * 3 + k2, (j1 - 1) * 3 + j2} \ne x_{(k1 - 1) * 3 + k3, (j1 - 1) * 3 + j3}\), \(\forall (k1,j1,k2,j2,k3,\) \(j3 \in [1,3] | k2 \ne k3 \wedge j2 \ne j3)\)

figure afigure a

The AC-3 algorithm if composed from the Algorithm 1 and 2. The main idea of these algorithms are the examination of value pairs and remove every value in \(D(X_i)\) that is inconsistent with the problem constraints, this is performed by the function Revise3. If the pair of the value \(v_i\) is eliminated from \(D(x_i)\), then it is necessary to verify the arc from the other direction. Every domain must be consistent, doing a detailed revision of all the domains. This is done by using a loop that verifies arcs until no change happens. The algorithm ends when all arcs are revised and no more changes exists (Q is empty), and it returns true when all arcs have been verified and remaining values of domains are arc consistent for all the problem constraints.

figure bfigure b

3 Experiments and Discussions

In Fig. 2, it is possible to see the gameboard with a Sudoku instance at the left. When a user selects a cell, if the checkbox “Help” is checked on the right side, an assistance for solving is given. Before and after typing an digit on the board, the AC3 algorithm is executed and the constraints are checked, delivering the possible assignations. It is possible to recognize that the algorithm is very effective for helping to solve, because a guidance through all the resolution process is given, making more easy to solve the game instances.

Fig. 2.
figure 2figure 2

Showing help solving an instance to a user.

If a wrong digit is entered, the prototype will show an error message on top, and the possible assignations will be shown, as appear in Fig. 3. If there are not possible assignations, clearly the arc consistency has detected an unfeasible assignation, and the previously entered digits were wrongly entered to the gameboard.

Fig. 3.
figure 3figure 3

Correcting an error, in order to help solving an instance to a user.

4 Conclusions

In this paper, we have presented a very useful technique in order to facilitate the resolution process of the Sudoku game. This game is known to be a NP-Complete problem, being a popular game in research fields like Constraint Programming and Metaheuristics, involving instances very difficult to solve. We have used the AC3 algorithm in order to offer a resolution guide to the users. This technique is similar to the strategies employed by users at the resolution process, but this technique is executed in a short time, making more easier and faster the solving process of the Sudoku puzzles.