
Probabilistic model checking extends model checking to domains in which actions will be taken based on a certain probability. This aligns nicely with modeling many real-world systems in which actions are taken in a stochastic manner. Unfortunately, probabilistic model checking further complicates the already high computational complexity challenges associated with deterministic model checking.
N-Queens is a commonly studied computer science and mathematics problem, and many algorithms solve it under different constraints. Some are iterative and deterministic, while others are stochastic. In this paper, written with Bernard Serbinowski at Vanderbilt University, we present a probabilistic model-checking approach to check a fundamental property of the stochastic ones: their ability to find a solution within a given number of transitions (iterations). That number is not fixed. Instead we ask for the probability of success within some number of transitions.
The problem
N-Queens generalizes the Eight Queens puzzle: place N queens on an N by N board so that no queen can attack another. A stochastic solver starts from some arrangement and repeatedly moves queens at random until no queen is under attack. Each solver is a stochastic transition system, so the natural question is how likely it is to have finished after I transitions.
To keep the results comparable we start every run from the same configuration, with the queens on the diagonal, one per row and one per column. We consider only algorithms that move a queen along its own row to a new column, never ones that move it along a column.
Three algorithms
- Random column move. Pick a queen that is under attack and move it to a different column chosen at random, without checking whether that column is already occupied. Solutions must be checked against both columns and diagonals.
- Random column swap. Pick a queen Q1 that is under attack and a different queen Q2 at random, then swap their columns. Q2 may or may not be under attack. Every queen stays in its own row and column, so only diagonal attacks need checking.
- Simulated annealing swap. The same swap, but a swap that leaves more queens attacked is taken only with probability p. Here p starts at 1 and drops by 1/100 after each successful swap, down to a minimum of 1/100.
Modeling in PRISM
PRISM is a probabilistic model checker. We write each algorithm as a discrete-time Markov chain (DTMC): at a state, a guard condition enables transitions, and one of several outcomes is taken according to its probability. Each queen's column is a variable from 1 to N, and its row is a constant. Queen 1 gets a module that describes its transitions, and PRISM's module renaming copies that module for the other queens.
A queen is attacked when its column and row offsets to another queen are equal in magnitude, which is a diagonal attack. The random column move model adds a test for equal columns. In the 4-queens swap model, an attacked queen swaps with each of the other three with probability 1/3. The annealing model needs extra state variables for the multi-step update: select the swap, store the current attack counts, swap, compare, and revert if the swap was rejected. Python meta-scripts generate the model files for any number of queens.
The property is a PCTL (probabilistic computation tree logic) query, run as a PRISM experiment over a counter. It returns the probability that a solution has been reached by each number of transitions.
P =? [ F ¬overall_attack ]
In words: with what probability is it eventually the case that no queen is under attack. As a sanity check we also asked whether P≥0.99 holds that no queen is ever attacked along a whole trace. The queens start under attack, so this must be false, and it was.
Experiments
We ran six experiments: each of the three algorithms with 4 queens and with 5 queens. The random column move and random column swap experiments covered up to 450 transitions in steps of 10, and the annealing experiments up to 50 in steps of 5. For each we read off the number of iterations at which the probability of a solution reaches about 99%.
| Algorithm | 4 queens | 5 queens |
|---|---|---|
| Random column move | 400 | 450 |
| Random column swap | 50 | 49 |
| Simulated annealing swap | 45 | 45 |
Results
For every algorithm and board size, the probability of a solution rises as a smooth, saturating curve. Swapping beats moving by almost an order of magnitude: about 50 iterations against 400 for 4 queens, and 49 against 450 for 5. Simulated annealing is a little faster again, at 45 iterations for both sizes. The swap-based algorithms barely changed from 4 to 5 queens, while random column move needs about 50 more iterations and its curve is closer to linear.
The challenges
The more instructive result is how hard this was for PRISM. There are three kinds of explosion.
- State space. With Q queens, each with Q possible columns, there are up to Q to the power Q combinations, multiplied by the I iterations PRISM must unroll. That is 4⁴ × 450 = 115,200 states for 4 queens, 5⁵ × 450 = 1,406,250 for 5, and about 4.5 × 10²⁰² for 100. The annealing model also carries the swap probability as a state variable, which multiplies the count by another 100.
- Search space. The number of placements of N queens on an N by N board grows extremely fast. Restricting queens to their own rows leaves N to the power N placements, and restricting the swap algorithms to distinct columns leaves N!, which is still fast growth.
- DTMC size. For random column move, the number of outcomes after I transitions is (N(N−1)) to the power I, before repeated states are merged with reduced-order binary decision diagrams.
Together these meant PRISM ran out of memory or time for more than 5 queens. Future work includes a greedy swap taken only when it reduces the attacked count, raising PRISM's memory limits, and fixing its reward functionality, which would allow state-space reductions.
Conclusions
Probabilistic model checking can give an exact answer about a stochastic algorithm: the probability that it has finished after I iterations, rather than an estimate from sampled runs. For N-Queens, random column swap and simulated annealing swap reach about a 99% chance of a solution in roughly 45 to 50 iterations, against 400 to 450 for random column move. The price is a state space that grows too quickly for small boards beyond five queens.
A note from 2026
The 99% thresholds are read from the plotted curves at a resolution of 10 iterations (5 for annealing), so the exact differences between 4 and 5 queens, such as 50 against 49, should not be over-read. The paper's tables call the first algorithm "Random Permutations" and its text "random column move"; they are the same. Everything here starts from the single diagonal configuration, so the numbers describe that start only.
The paper is available as a PDF, and the model files and meta-scripts are in the GitHub repository.