Details
An exact cover is a partitioning of a set of elements into disjoint subsets whose union is exactly the original set.
The exact cover (XC) problem gives an original set along with a list of allowable subsets and asks if any selection of the allowable subsets is an exact cover of the original set. It is an NP complete problem, and very similar to graph clique finding (cf.
FindClique).
Here we do not deal with sets and subsets, but rather with binary matrices. The input binary matrix mat lists allowable subsets as rows and set elements as columns, by coding that the kthelement of the original set is also a member of the jth listed subset if and only if matjk=1. Thus a one's row {1,1,…,1} represents a trivial exact cover.
Sometimes the terms items and options are used in place of column and row headers respectively. The exact cover problem can then be stated as follows: Choose a set of options such that every item is included in exactly one option.
If two options are not disjoint, their respective row sum will have a value exceeding 1 in at least one column. Similarly, if neither of two options includes the
kth item, their respective row sum will have a 0 in the
kth column. Let
{ j1,j2,… }∈𝒪 be a set of options. It is an exact cover if and only if the respective row sum satisfies that
.
Now define a graph whose vertices are options and whose edges indicate pairwise disjunction. Cliques in the graph represent maximal sets of disjoint options, which can easily be tested for the exact cover property once they are found.
FindExactCover has a
Method option for switching between three distinct algorithms:
"DancingLinksX" | Donald Knuth's algorithm |
"CliqueToCover" | transforms input to a graph and uses FindClique |
"BranchAndPrune" | uses a depth-first search |
When using the DancingLinksX method, setting the Heuristic sub-option to either
Automatic or
"Increment" changes the search order:
{"DancingLinksX","Heuristic"→ Automatic} | Knuth's suggested MRV heuristic |
{"DancingLinksX","Heuristic"→ "Increment"} | select right-nearest column |
The "MRV heuristic" chooses to cover the next item with the minimal number of associated options.
ResourceFunction["FindExactCover"] ignores empty subsets, i.e. zero's rows of the form {0,0,…,0}.