I came across this nice snippet by John D. Cook, and it immediately possessed me to think about what the compressed Boolean representation of such a problem would look like. For those not aware of the 8 queens problem, here is a brief look below:

Question

Place 8 queens of either colour in an empty chessboard, such that no two queens lie on the same row, column or diagonal. Does there exist a valid arrangement under these constraints?

Assuming that there exists such an arrangement, we can convey the solution as a Boolean equation. A Boolean equation is simply a collection of predicates (or variables) which can take either True or False values and are connected together as an expression using logical operators like AND, OR and NOT. An example of a Boolean expression would be , where , and are Boolean variables and , , are AND, OR and NOT logical operators respectively.

Boolean Satisfiability Problems

Consider an expression which can have a truth value, such as β€œthe sky is blue and the road is empty”, it can be broken into two parts: β€œthe sky is blue” (), β€œthe road is empty” () which are connected together using an AND operator. Thus if you represent the entire statement as an expression , the truth value of the overall statement is contingent on whether both its predicates are True or not.

Similarly, you can condense the 8 queens problem into a Boolean expression and can tell whether a possible arrangement of queens satisfies the preset conditions based on what the truth value of the overall expression representing the overall problem is. Such a class of problems are called Boolean Satisfiability Problems, often abbreviated as SAT, and involves finding truth value assignments for which the overall expression is true.

Consider one of the above examples:

The statement is only true when is True, is True and is False, this can be represented as:

Therefore, the 8 queens problem holds only if we are able to find such a set of truth values for each of its predicates.

n-Queens problem

As illustrated in the blog, consider the chessboard as a grid each of whose cells is represented by where if there is a queen in that particular square or otherwise. There are three conditions that have to be met for any arrangement of queens to be valid:

  1. Only one queen per row
  2. Only one queen per column
  3. No two queens on the same diagonal

Ensuring that there is at least one queen per row and column is simple. For each row and for each column we will have: and respectively. However, this alone is not sufficient to encode the necessary constraints. We must ensure that there is at least one queen for each row, column and additionally we must also ensure that there are not more that one queen for each row, column.

As mentioned in the blog, that is indeed the difficult part, and from the given two approaches, I will highlight the one that I felt was easier to understand and thus explain (although both approaches are equivalent).

For each grid cell in each row we will need to ensure that if that particular grid cell contains a queen none of the rest of the cells in its row should contain another queen. This can be expressed for each grid cell as:

where is a logical operator that means β€œimplies”, and can be broken down into . This means that if there is a queen in , (i.e., ) there are no queens in any of the rest of the cells of that row. For a chessboard square, the number of rows and columns will be equal with each and being an element of the set , which in this case is , with representing the set of all grid cells excepting . This statement if valid for only a particular grid cell in context of the -th row of the chessboard. Identical statement have to be constructed for all the elements of the -th row and joined together using AND operators in the following manner:

In a similar manner column specific constraints can be constructed.

where again .

Thus, the constraints for rows () and columns () can be encoded as:

and

Finally, we will have a diagonal constraint that ensure that there are no two queens that share the same diagonal. This is rather straightforward as you have:

where dictates the range of based on where the grid cell is situated. This might not be the most efficient encoding of the problem as it contains many redundant logical operations, but it certainly a valid one out of many. The overall expression that for the 8 queens problem therefore is:

For an chessboard, this same representation can be scaled up by expanding the set to extend from to .

n-Kings problem

I also became interested in a problem slightly reminiscent of the n-Queens problem. Here instead of having a chessboard with n queens we can have n kings, in such a way that no two kings exist in the same neighbourhood. A neighbourhood is defined as the 8 boxes surrounding a grid cell. An example of which can be seen below:

center

Again, a chessboard is expressed as a collection of grid cells each having some truth value. A grid cell will be true only when neither of the cell in the rows above and below it are true: , similarly, no cells on either sides should be true , additionally, no cells on either diagonals of the cell should contain a king .

A complete and viable arrangement for a simple grid is shown below:

center

Putting it all together we will have a constraint for each cell :

The expression that dictates the constraints of the n-kings problem is thus:

This is not the most efficient representation of the problem considering the high number of redundant operations being considered. However, it is a sufficiently complete one.