N-Queens Genetic Algorithm Visualizer

Watch a generational genetic algorithm evolve toward a conflict-free board.

How solutions are represented & how the algorithm works

Every candidate ("chromosome") is an N-tuple (r₀, r₁, ..., r_(N-1)), where r_i is the row of the queen placed in column i. Since each column has exactly one entry, queens can never share a column — the only possible conflicts are queens sharing a row or a diagonal. Fitness is measured as the number of attacking pairs; the algorithm minimizes this, with 0 meaning a solved board.

This is a generational genetic algorithm: each generation, parents are chosen via tournament selection and combined with either single-point or two-point crossover (at your chosen probability), then mutated by randomly reassigning some queens' rows (at your chosen probability). Optionally, the single best individual from the previous generation can be copied through unchanged (elitism), so the best-seen solution is never lost — you can toggle this off below to see how the population behaves without it.

In the individual list, each queen's row value is shown as a colored chip rather than a plain number — the same row value always gets the same color. Individuals with matching color patterns are genetically identical, so the list doubles as a quick visual read on population diversity.

non-attacking queen    attacking queen   (showing best individual of current generation)

Top 50 Individuals (best → worst)