Mathwords logoMathwords

Exact Cover — Definition, Formula & Example

Exact cover is the problem of choosing some subsets from a given collection so that every element of a set lies in exactly one chosen subset. No element may be missed, and none may be covered twice.

Given a finite set UU and a collection S\mathcal{S} of subsets of UU, an exact cover is a subcollection S∗⊆S\mathcal{S}^* \subseteq \mathcal{S} whose members are pairwise disjoint and whose union is UU. Equivalently, S∗\mathcal{S}^* is a partition of UU using only blocks from S\mathcal{S}. Deciding whether an exact cover exists is NP-complete; it is one of Karp's 21 NP-complete problems (1972).

Key Formula

Mx=1,xj∈{0,1}M\mathbf{x} = \mathbf{1}, \qquad x_j \in \{0, 1\}
Where:
  • MM = An n×mn \times m 0-1 matrix with Mij=1M_{ij} = 1 if element ii belongs to subset jj (one row per element, one column per subset)
  • x\mathbf{x} = Selection vector: xj=1x_j = 1 if subset jj is chosen, 00 otherwise
  • 1\mathbf{1} = The all-ones vector of length nn, forcing each element to be covered exactly once

How It Works

Knuth's Algorithm X solves exact cover by backtracking. Pick an element that is still uncovered, ideally one that lies in the fewest remaining subsets. Try each subset that contains it. Each time you choose a subset, delete every subset that overlaps it, then repeat on the elements still uncovered. If some element has no remaining subset, undo the last choice and try the next one. Knuth's Dancing Links technique stores the table as linked lists so these deletions and undos are fast.

Worked Example

Problem: Let U={1,2,3,4,5,6}U = \{1, 2, 3, 4, 5, 6\} with subsets A={1,4}A = \{1,4\}, B={2,3}B = \{2,3\}, C={1,2,5}C = \{1,2,5\}, D={3,6}D = \{3,6\}, E={5,6}E = \{5,6\}, F={2,4}F = \{2,4\}. Use Algorithm X to find an exact cover.
Step 1: Element 1 lies only in AA and CC. Try CC. It overlaps AA, BB, EE, and FF, so delete them. Only DD is left, and no remaining subset contains element 4. This branch is a dead end, so undo CC.
C={1,2,5}  ⇒  uncovered {3,4,6}, options left: D={3,6}C = \{1,2,5\} \;\Rightarrow\; \text{uncovered } \{3,4,6\},\ \text{options left: } D = \{3,6\}
Step 2: Try AA instead. It overlaps CC and FF, so delete them. Element 2 now lies only in BB, so choose BB. Then delete DD, which shares element 3 with BB.
uncovered after A,B:{5,6}, options left: E={5,6}\text{uncovered after } A, B: \{5,6\},\ \text{options left: } E = \{5,6\}
Step 3: Choose EE. Check that the three subsets are disjoint and cover UU. Their sizes add to 6, the size of UU, so no element is counted twice.
{1,4}∪{2,3}∪{5,6}={1,2,3,4,5,6},2+2+2=6\{1,4\} \cup \{2,3\} \cup \{5,6\} = \{1,2,3,4,5,6\}, \qquad 2 + 2 + 2 = 6
Answer: The exact cover is {A,B,E}\{A, B, E\}. Algorithm X tried every branch, so this is the only exact cover.

Why It Matters

Sudoku is an exact cover problem. Each of the 4×81=3244 \times 81 = 324 constraints (each cell filled, and each digit once per row, column, and box) is an element, so it is a row of MM. Each of the 9×81=7299 \times 81 = 729 possible placements of a digit in a cell is a subset, so it is a column of MM. The same model solves pentomino tilings and the n-queens puzzle, and you will meet it again in an algorithms course on NP-completeness.

Common Mistakes

Mistake: Accepting subsets that cover every element but overlap, such as C={1,2,5}C = \{1,2,5\}, D={3,6}D = \{3,6\}, and F={2,4}F = \{2,4\} together, which cover all of UU but both CC and FF contain 2.
Correction: That is a set cover, not an exact cover. Each element must appear in exactly one chosen subset. A quick test: the chosen subsets' sizes must add up to exactly ∣U∣|U|.

Related Terms

  • Combinatorics — Parent field studying selections, arrangements, and partitions
  • Stirling Numbers — Count all partitions of a set into blocks
  • NP-Complete Problem — Exact cover is a classic NP-complete problem
  • Subset — Exact cover chooses subsets that partition a set
  • Tiling — Tiling puzzles are classic exact cover problems