Sudoku as an Expression in Propositional Logic Peter Cappello Department of Computer Science University of California, Santa Barbara INTRODUCTION Sudoku is a kind of puzzle that involves a 9 9 array of squares. Please see the Wikipedia entry on Sudoku: http://en.wikipedia.org/wiki/sudoku. The puzzle summarily is to fill in each empty square with a nonzero digit such that each: row contains all the nonzero digits column contains all the nonzero digits region (emboldened sub-array) contains all the nonzero digits. THE PROBLEM Let P be an indexed set of boolean variables, p(i, j, d), for i, j, d, where p(i, j, d) = true means that the square in row i and column j has digit d. We refer to the set of values of these variables as P s truth assignment. Definition: A truth assignment is feasible when, for any fixed [row] i and [column] j, p(i, j, d) = true for exactly one value of d. Definition: The proposed solution corresponding to a feasible truth assignment has digit d in the square associated with row i and column j, where p(i, j, d) = true. THE PROBLEM SPECIFICATION Input: An instance I of a Sudoku puzzle. Output: A propositional expression over the boolean variables in P that is satisfiable if and only if:. P s truth assignment is feasible and. the proposed solution corresponding to P s truth assignment is a solution to Sudoku puzzle I. Describe a correct output expression, or an algorithm for producing it.
A SOLUTION Each instance of a Sudoku puzzle has some nonempty squares. We refer to these constraints as the instance s initial conditions. We refer to the conditions on any solution that are stated in the Introduction as the general conditions. A truth assignment α corresponds to a puzzle solution when α:. is feasible and. satisfies the instance s initial conditions and. satisfies the general conditions. We construct an expression that is the conjunction of sub-expressions, corresponding to the conditions described above. We will construct an expression, β, that corresponds to the initial conditions, and expression, γ, that ensures feasibility, and an expression σ that encodes the general conditions. Our goal: a truth assignment α satisfies β γ σ if and only if the proposed solution corresponding to α is a solution to the puzzle. Before we begin this program, we establish a generic expression that will prove useful in several places. Let p, p,..., p n be boolean variables. We seek an expression over these variables that is satisfiable if and only if exactly of the variables is true. This expression can be written as the conjunction of conditions:. At least one variable is true. At most one variable is true. Clearly, the first condition is satisfied by the disjunction of the variables: p p... p n. One way to express condition is to ensure that no variables are both true: For every i j, (p i p j ) p i p j. Thus, the expression we seek is the conjunction of these expressions. For example, if we want exactly one of p, q, r, and s to be true, an expression that ensures this is (p q r s) (( p q) ( p r) ( p s) ( q r) ( q s) ( r s)). Let S be a set of boolean variables. Let ψ(s) denote the propositional logic expression that is satisfiable if and only if exactly element of S is true. We see from the discussion above that we always can construct such an expression. AN EXPRESSION THAT CORRESPONDS TO THE INITIAL CONDITIONS Again, the initial conditions are the squares that initially have a number. Let us say that there are k such squares. We denote their rows, columns, and numbers as (r, c, n ),..., (r k, c k, n k ). For any given initial condition, such as the square in row r column c has number n, we require that. p(r, c, n) is true, and
. all other variables for this square are false: m n p(r, c, m). We define a parameterized expression, β(r, c, n), for the initial condition that the square in row r and column c has number n: β rcn = p(r, c, n) p(r, c, m). Then, an expression for the initial conditions, β, is just the conjunction of the expressions for the each initialized square: β = β(r, c, n ) β(r k, c k, n k ). m n AN EXPRESSION THAT ENSURES FEASIBILITY Our expression of the initial conditions is satisfiable if and only if exactly one associated propositional variable is assigned true. Regarding feasibility, we thus need only concern ourselves with the empty squares. Let the square in row r and column c be empty. A feasible assignment of truth values to the propositional variables p(r, c, n), for n =,,, requires that exactly one is true. Let P rc denote this set of propositional variables. ψ(p rc ) expresses this requirement. Validity requires the conjunction of ψ(p rc ) over the set of empty squares. Let E = {(r, c) the square in row r and column c is empty }. Define γ = ψ(p rc ). (r, c) E Then, γ is satisfiable if and only if, for each empty square s, exactly of the propositional variables associated with s is true. AN EXPRESSION THAT ENCODES THE GENERAL CONDITIONS To aid in the subsequent discussion, we present the -dimensional set of boolean variables (row, column, number) visually in Figure. General condition General condition states that each row contains all the nonzero digits. Consider row. If this row satisfies general condition, then exactly column in this row has a in it. We focus our attention of the variables that represent the columns in row with number value : p(,, ), p(,, ), p(,, ), and p(,, ). In Figure, these variables are green. Let R be the set that contains these variables. Then ψ(r ) is satisfiable if and only if exactly of the variables associated with row and number value is assigned true. We want similar expressions for row and number values,, and. Indeed, for general condition, we want the conjunction of such expressions, for each row, and for each number value in each row: ψ(r rn ), r,n where r, n. The R rn of interest are shown in Figure??. Let R rn be the set of variables associated with row r and number value n. In Figure, each set of variables connected by a black line represents an R rn, for some particular row r and number value n.
COLUMN ROW NUMBER Figure : The -dimensional array of propositional variables: (i) Variables with a common row and column (i.e., are associated with the same square) are connected by a red line; (ii) variables with a common row and number value are connected by a black line; (iii) variables with a common column and number value are connected by a blue line.
Figure : The -dimensional array of propositional variables, where variables are green if they are associated with row and number value : R. 5
General condition General condition 6