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 and a collection of subsets of , an exact cover is a subcollection whose members are pairwise disjoint and whose union is . Equivalently, is a partition of using only blocks from . Deciding whether an exact cover exists is NP-complete; it is one of Karp's 21 NP-complete problems (1972).
Key Formula
Where:
- = An 0-1 matrix with if element belongs to subset (one row per element, one column per subset)
- = Selection vector: if subset is chosen, otherwise
- = The all-ones vector of length , 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 with subsets , , , , , . Use Algorithm X to find an exact cover.
Step 1: Element 1 lies only in and . Try . It overlaps , , , and , so delete them. Only is left, and no remaining subset contains element 4. This branch is a dead end, so undo .
Step 2: Try instead. It overlaps and , so delete them. Element 2 now lies only in , so choose . Then delete , which shares element 3 with .
Step 3: Choose . Check that the three subsets are disjoint and cover . Their sizes add to 6, the size of , so no element is counted twice.
Answer: The exact cover is . Algorithm X tried every branch, so this is the only exact cover.
Why It Matters
Sudoku is an exact cover problem. Each of the constraints (each cell filled, and each digit once per row, column, and box) is an element, so it is a row of . Each of the possible placements of a digit in a cell is a subset, so it is a column of . 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 , , and together, which cover all of but both and 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 .
