Given a universe and a family of subsets, choose a subfamily so that every element is covered exactly once. NP-complete (one of Karp’s 21).

Equivalently: given a 0/1 matrix, select rows so that every column contains exactly one 1.

Solving it

Algorithm X — backtracking with two decisions that make it fast:

  1. Branch on the most-constrained column (the one with the fewest 1s). This is MRV, and it collapses the search tree.
  2. Undo in using Dancing Links (DLX), where removing and restoring a doubly linked node are both two pointer writes.
AlgorithmX(matrix):
    if no columns remain: report the solution
    c = column with the fewest 1s
    if c has no 1s: backtrack
    for each row r with a 1 in c:
        add r to the solution
        cover every column in which r has a 1 (removing conflicting rows)
        recurse
        uncover, remove r

Modelling — the actual skill

Columns are constraints, rows are choices.

Each row must have a 1 in exactly the constraints its choice satisfies.

Sudoku — 324 columns, 729 rows

Constraint groupColumnsMeaning
Cell81cell is filled once
Row81digit appears once in row
Column81digit appears once in column
Box81digit appears once in box

Each row is “place digit at ” with exactly four 1s. DLX solves the hardest known puzzles in microseconds.

N-Queens

  • Primary columns (exactly once): each rank, each file.
  • Secondary columns (at most once): each diagonal.

Secondary columns are the “exact cover with optional columns” generalisation — they may be left uncovered.

Polyomino tiling

  • One column per board square (must be covered once).
  • One column per piece (must be used once, if pieces are distinct).
  • One row per legal placement (position × orientation).

This is the canonical DLX application, and it solves pentomino packings that resist ad-hoc search.

Other encodings

ProblemColumnsRows
Latin squarescell, row-symbol, column-symbolplace symbol at cell
Perfect matchingeach vertexeach edge
Set partitioningeach elementeach candidate set
Langford pairseach position, each numbereach placement
Instant Insanityeach cube, each face-colour constrainteach orientation

Exact cover vs set cover

Exact coverSet cover
Requirementeach element exactly onceeach element at least once
Objectivefeasibility (or count)minimise the number of sets
Approximationnot meaningful greedy, and that is optimal
MethodDLX / Algorithm Xgreedy, ILP

They look similar and behave completely differently. Exact cover is a constraint satisfaction problem; set cover is an optimisation problem.

Variants

  • With optional (secondary) columns — “at most once” constraints, as in N-Queens diagonals.
  • XCC (exact cover with colours) — Knuth’s TAOCP 4B extension, where secondary columns carry a colour and multiple rows may share one if the colours agree. Handles puzzles with compatibility constraints.
  • Multiplicities — a column must be covered between and times.
  • Counting solutions rather than finding one — the same search, no early exit.

When to use DLX

Signal
The problem is naturally “cover every requirement exactly once”
Choices are enumerable and each satisfies a fixed small set of constraints
Puzzle-like: tiling, placement, filling
The constraint matrix is sparse
It is an optimisation, not a feasibility, problem✘ — use ILP or branch and bound
The constraints are boolean clauses✘ — use SAT

For contest use, a hand-written backtracking search with MRV usually suffices; DLX earns its place when the same solver must handle many instances or the naive search is too slow.

See also: Algorithm X · Dancing Links · Backtracking