Constraint satisfaction solver
I built a constraint solver from scratch in Java that places 1,000 non-attacking queens in about 15 ms.
- Year
- 2025
- Built with
- Java, Spring Boot, algorithms
- Links
- Source code
Overview
A constraint satisfaction problem (CSP) is a set of variables, a list of allowed values for each one, and rules that limit which combinations are valid. Sudoku is the classic example. I wrote a general solver for these problems in Java 21, without any CSP library, and put it behind a Spring Boot REST API.
The API solves N-Queens, Sudoku, graph coloring and letter puzzles such as SEND + MORE = MONEY. A browser page shows the search as it happens, streamed over a WebSocket.
The solver has two engines. Backtracking explores every possibility, so it can also prove that a puzzle has no solution. Min-conflicts is a local search that gives up that guarantee for speed, and it places 1,000 queens in about 15 ms.

How it works
Backtracking assigns one variable at a time and undoes a choice as soon as it leads to a dead end. Four techniques keep it from wasting time.
- Forward checking. After each assignment, the solver removes the values that now conflict from neighbouring variables, and backs out as soon as one of them has nothing left.
- AC-3. Before the search starts, the solver removes every value that has no compatible value in some neighbour. This property is called arc consistency.
- Variable ordering. By default the solver picks the variable with the fewest values left (MRV) and breaks ties by how many constraints it has. Dom/WDeg is an alternative that learns from failures and gives more weight to the constraints that cause dead ends.
- A trail. Instead of copying every domain at each step, the solver records only the
BitSetdomains it changes, so undoing a choice restores just those.
For N-Queens boards of 50 or more, the API switches to min-conflicts. It starts from a greedy placement, then keeps moving a conflicted queen to the column in its row with the fewest conflicts, until no two queens attack each other.
The live view sends search events over STOMP, a messaging protocol that runs on top of WebSocket, and it limits board updates to about 30 per second.
Design decisions
- A trail instead of copies. The first version copied every domain at every node of the search, so each step allocated memory in proportion to the size of the problem. With the trail, a node costs about half as much.
- Forward checking during the search, AC-3 only before it. Running AC-3 again at every node, an approach called MAC, removes more values but costs more per step. With good variable ordering, forward checking was the better default.
- Two engines, chosen by size. Backtracking stays the default for small boards because it can list every solution. Min-conflicts takes over for large N-Queens, where backtracking slows down sharply.
Problems I ran into
Three bugs failed silently, which made them the hardest to find.
- The value selectors handed back the live domain instead of a copy. The solver pruned that domain while still looping over it, so some values were skipped and the search could miss solutions. The selectors now loop over a snapshot.
- With MAC, a constraint that doesn't link pairs of variables, like the sum in SEND + MORE = MONEY, was never checked. The solver now checks every constraint once an assignment is complete.
- Dom/WDeg set a constraint's weight to 1.0 after its first failure. That is also the starting weight, so the first failure counted for nothing. It now raises the weight to 2.0.
One test also behaved strangely. Solving 200 queens took about twice as long in the full test suite as on its own, because in the full suite ForwardChecker meets many kinds of constraints and the Java JIT compiler stops inlining its calls. The search itself is deterministic at 639,950 nodes, so I raised the test's time limit to 30 seconds instead of chasing the timing.
Results
These backtracking times come from the README, each the best of several runs.
- 100 queens take about 15 ms and 185 search nodes.
- 200 queens take about 8 s and roughly 640,000 nodes.
- A hard Sudoku takes about 65 ms and roughly 8,700 nodes.
On the same search tree, the trail brought 100 queens from 50 ms down to 22 ms, and 200 queens from 16.4 s down to 8.1 s. The project has 61 tests, and one of them checks that SEND + MORE = MONEY has exactly one solution, 9567 + 1085 = 10652.
What I'd do next
- MAC is already written, but the API can't select it yet. Exposing it would let me measure what running AC-3 during the search really costs.
- On N-Queens, backtracking is sensitive to board size when values are tried in order, and it stays fast up to about N = 150.