Last year I wrote a Firefox extension to get the world record on LinkedIn’s Queens using SAT. While reviving the project, I realized there was a bug when it was unable to solve a recent puzzle, which sent me down a rabbithole to fix it.
I’ve also begun working through a graph theory textbook, and found there was a graph theoretic approach to solving Star Battle (which Queens effectively rips off) that paints my failed attempt on the previous blog post in a much better light than I gave myself credit for.
So, I thought I’d write a short blog post about both!
The Bug
I noticed that the following puzzle was causing CVC5 to return UNSAT. To debug why I solved the puzzle by hand:

Solved board
I bisected the boolean formulas I generated to find that a singluar edge constraint was causing the issue. Here’s a close up of that edge centered at \((2, 3)\):

The central square whose constraints are causing UNSAT
Recall, edges are created when colors border each other and placing a queen on one square will disallow a queen on all the different colored adjacent diagonals. In my previous blog post I wrote the definition of these constraints as:
$$ adjacentConstraint = \bigwedge\limits_{i=0}^{numAdjacent} atLeastOne(adjacent_i) \newline $$
Except there’s two problems! I actually meant: $$ adjacentConstraint = \bigwedge\limits_{i=0}^{numAdjacent} atMostOne(adjacent_i) \newline $$
Even when we use \(atMostOne\), my code to collect the adjacent diagonals would end up producing a set of adjacent squares that looks like: $$ adjacent_{(2,3)}=\{(2, 3), (1, 4), (3, 2)\} $$
The above set causes an UNSAT because the only solution has a queen on \((1, 4), (3, 2)\), which is a contradiction in our \(atMostOne\) constraint!
Rather than taking \(atMostOne\) of the set of all adjacent diagonals at an edge, the fix is to generate an \(atMostOne\) constraint for each adjacent diagonal at the edge independently. In this case, it would look like: $$ adjacentConstraint_{(2, 3)} = atMostOne(\{(2, 3), (1, 4)\}) \land atMostOne(\{(2, 3), (3, 2)\}) $$
I’ve updated the code on my Github, and everything is now working!
Graph Theoretic Queens
While working though Chapter 1 of Start Doing Graph Theory, I noticed that practice problem 1(c) made use of Star Battle to teach problem solving with Graph Theory. At the beginning of the chapter, the author introduces the concept of an independent set of a graph:
Independent set: A set of vertices with no edges between them
In the case of Star Battle, or in our case Queens, you can construct a graph such that every square is a vertex in a graph, placing an edge to every other square that is excluded when the solution contains the current square. Then to find a solution to Queens, you could find an independent set of N vertices on an NxN board.
If you read my previous blog post, my intial attempt at solving Queens with SAT may sound fairly similar to finding an independent set:
… my first idea was that “given a cell, it implies all the cells in its group (row, column) are false”.
The independent set problem can also be reduced to a SAT problem, although more complicated, so the solution I ended up with is better for the solver and for being able to grok the formulas generated.
So I wasn’t very wrong after all!
Conclusion
Hopefully you learned that I am in fact human, and that graph theory is pretty cool! Hope to post more graph theory and technical content soon!