In the
mathematical
Mathematics is a field of study that discovers and organizes methods, Mathematical theory, theories and theorems that are developed and Mathematical proof, proved for the needs of empirical sciences and mathematics itself. There are many ar ...
field of
combinatorics
Combinatorics is an area of mathematics primarily concerned with counting, both as a means and as an end to obtaining results, and certain properties of finite structures. It is closely related to many other areas of mathematics and has many ...
, given a collection
of
subset
In mathematics, a Set (mathematics), set ''A'' is a subset of a set ''B'' if all Element (mathematics), elements of ''A'' are also elements of ''B''; ''B'' is then a superset of ''A''. It is possible for ''A'' and ''B'' to be equal; if they a ...
s of a
set
Set, The Set, SET or SETS may refer to:
Science, technology, and mathematics Mathematics
*Set (mathematics), a collection of elements
*Category of sets, the category whose objects and morphisms are sets and total functions, respectively
Electro ...
, an exact cover is a subcollection
of
such that each element in
is contained in ''exactly one'' subset in
.
One says that each element in
''is covered by'' exactly one subset in
.
An exact cover is a kind of
cover. In other words,
is a
partition of
consisting of subsets contained in
.
The exact cover problem to find an exact cover is a kind of
constraint satisfaction problem
Constraint satisfaction problems (CSPs) are mathematical questions defined as a set of objects whose state must satisfy a number of constraints or limitations. CSPs represent the entities in a problem as a homogeneous collection of finite const ...
. The elements of
represent choices and the elements of
represent constraints. It is
non-deterministic polynomial time (NP) complete and has a variety of applications, ranging from the optimization of airline flight schedules,
cloud computing
Cloud computing is "a paradigm for enabling network access to a scalable and elastic pool of shareable physical or virtual resources with self-service provisioning and administration on-demand," according to International Organization for ...
, and
electronic circuit design.
An exact cover problem involves the relation ''contains'' between subsets and elements. But an exact cover problem can be represented by any
heterogeneous relation
In mathematics, a binary relation associates some elements of one set called the ''domain'' with some elements of another set called the ''codomain''. Precisely, a binary relation over sets X and Y is a set of ordered pairs (x, y), where x i ...
between a set of choices and a set of constraints. For example, an exact cover problem is equivalent to an
exact hitting set problem, an
incidence matrix
In mathematics, an incidence matrix is a logical matrix that shows the relationship between two classes of objects, usually called an incidence relation. If the first class is ''X'' and the second is ''Y'', the matrix has one row for each element o ...
, or a
bipartite graph
In the mathematics, mathematical field of graph theory, a bipartite graph (or bigraph) is a Graph (discrete mathematics), graph whose vertex (graph theory), vertices can be divided into two disjoint sets, disjoint and Independent set (graph theo ...
.
In
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, the exact cover problem is a
decision problem
In computability theory and computational complexity theory, a decision problem is a computational problem that can be posed as a yes–no question on a set of input values. An example of a decision problem is deciding whether a given natura ...
to determine if an exact cover exists. The exact cover problem is
NP-complete
In computational complexity theory, NP-complete problems are the hardest of the problems to which ''solutions'' can be verified ''quickly''.
Somewhat more precisely, a problem is NP-complete when:
# It is a decision problem, meaning that for any ...
[
This book is a classic, developing the theory, then cataloguing ''many'' NP-Complete problems.
] and is one of
Karp's 21 NP-complete problems. It is NP-complete even when each subset in contains exactly three elements; this restricted problem is known as exact cover by 3-sets, often abbreviated X3C.
Knuth's Algorithm X is an
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
that finds all solutions to an exact cover problem. DLX is the name given to Algorithm X when it is implemented efficiently using
Donald Knuth
Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist and mathematician. He is a professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of comp ...
's
Dancing Links technique on a computer.
The exact cover problem can be generalized slightly to involve not only ''exactly-once'' constraints but also ''at-most-once'' constraints.
Finding
Pentomino
A pentomino (or 5-omino) is a polyomino of order 5; that is, a polygon in the Plane (geometry), plane made of 5 equal-sized squares connected edge to edge. The term is derived from the Greek word for '5' and "domino". When rotation symmetry, rota ...
tilings and solving
Sudoku
Sudoku (; ; originally called Number Place) is a logic puzzle, logic-based, combinatorics, combinatorial number-placement puzzle. In classic Sudoku, the objective is to fill a 9 × 9 grid with digits so that each column, each row, and ...
are noteworthy examples of exact cover problems. The
''n'' queens problem is a generalized exact cover problem.
Formal definition
Given a collection
of
subset
In mathematics, a Set (mathematics), set ''A'' is a subset of a set ''B'' if all Element (mathematics), elements of ''A'' are also elements of ''B''; ''B'' is then a superset of ''A''. It is possible for ''A'' and ''B'' to be equal; if they a ...
s of a set
, an exact cover of
is a subcollection
of
that satisfies two conditions:
* The
intersection
In mathematics, the intersection of two or more objects is another object consisting of everything that is contained in all of the objects simultaneously. For example, in Euclidean geometry, when two lines in a plane are not parallel, their ...
of any two distinct subsets in
is
empty, i.e., the subsets in
are
pairwise disjoint
In set theory in mathematics and Logic#Formal logic, formal logic, two Set (mathematics), sets are said to be disjoint sets if they have no element (mathematics), element in common. Equivalently, two disjoint sets are sets whose intersection (se ...
. In other words, each element in
is contained in ''at most one'' subset in
.
* The
union of the subsets in
is
, i.e., the subsets in
cover . In other words, each element in
is contained in ''at least one'' subset in
.
In short, an exact cover is ''exact'' in the sense that each element in
is contained in ''exactly one'' subset in
.
Equivalently, an exact cover of
is a subcollection
of
that
partitions .
For an exact cover of
to exist, it is necessary that:
* The union of the subsets in
is
. In other words, each element in
is contained in at least one subset in
.
If the
empty set
In mathematics, the empty set or void set is the unique Set (mathematics), set having no Element (mathematics), elements; its size or cardinality (count of elements in a set) is 0, zero. Some axiomatic set theories ensure that the empty set exi ...
is contained in
, then it makes no difference whether or not it is in any exact cover. Thus it is typical to assume that:
* The empty set is not in
. In other words, each subset in
contains at least one element.
Basic examples
Let
be a collection of subsets of a set
such that:
*
,
*
,
*
, and
*
.
The subcollection
is an exact cover of
, since the subsets
and
are disjoint and their union is
.
The subcollection
is also an exact cover of
.
Including the empty set
makes no difference, as it is disjoint with all subsets and does not change the union.
The subcollection
is not an exact cover of
.
Even though the union of the subsets
and
is
, the intersection of the subsets
and
,
, is not empty. Therefore the subsets
and
do not meet the ''disjoint'' requirement of an exact cover.
The subcollection
is also not an exact cover of
.
Even though
and
are disjoint, their union is not
, so they fail the ''cover'' requirement.
On the other hand, there is no exact cover—indeed, not even a cover—of
because
is a proper subset of
: None of the subsets in
contains the element 5.
Detailed example

Let = be a collection of subsets of a set = such that:
* = ;
*
= ;
* = ;
*
= ;
* = ; and
*
= .
The subcollection = is an exact cover, since each element is covered by (contained in) exactly one selected subset, as the highlighting makes clear.
Moreover, is the only exact cover, as the following argument demonstrates: Because and are the only subsets containing the element 1, an exact cover must contain or , but not both. If an exact cover contains , then it doesn't contain , , , or , as each of these subsets has the element 1, 4, or 7 in common with . Then is the only remaining subset, but the subcollection doesn't cover the element 2. In conclusion, there is no exact cover containing . On the other hand, if an exact cover contains , then it doesn't contain or , as each of these subsets has the element 1 or 4 in common with . Because is the only remaining subset containing the element 5, must be part of the exact cover. If an exact cover contains , then it doesn't contain , as has the elements 3 and 6 in common with . Then is the only remaining subset, and the subcollection is indeed an exact cover. See the
example
Example may refer to:
* ''exempli gratia'' (e.g.), usually read out in English as "for example"
* .example, reserved as a domain name that may not be installed as a top-level domain of the Internet
** example.com, example.net, example.org, an ...
in the article on
Knuth's Algorithm X for a matrix-based version of this argument.
Representations
An exact cover problem is defined by the
heterogeneous relation
In mathematics, a binary relation associates some elements of one set called the ''domain'' with some elements of another set called the ''codomain''. Precisely, a binary relation over sets X and Y is a set of ordered pairs (x, y), where x i ...
''contains'' between a collection of subsets and a set of elements. But there is nothing fundamental about subsets and elements.
A representation of an exact cover problem arises whenever there is a heterogeneous relation ⊆ × between a set of choices and a set of constraints and the goal is to select a subset of such that each element in is -related to ''exactly one'' element in . Here is the
converse of .
In general,
restricted to × is a
function from to , which maps each element in to the unique element in that is -related to that element in . This function is
onto
In mathematics, a surjective function (also known as surjection, or onto function ) is a function such that, for every element of the function's codomain, there exists one element in the function's domain such that . In other words, for a f ...
, unless contains an element (akin to the empty set) that isn't -related to any element in .
Representations of an exact cover problem include an exact hitting set problem, an incidence matrix, and a bipartite graph.
Exact hitting set
In
mathematics
Mathematics is a field of study that discovers and organizes methods, Mathematical theory, theories and theorems that are developed and Mathematical proof, proved for the needs of empirical sciences and mathematics itself. There are many ar ...
, given a set and a collection of subsets of , an exact hitting set is a subset of such that each subset in contains ''exactly one'' element in . One says that each subset in ''is hit by'' exactly one element in .
The exact hitting set problem is a representation of an exact cover problem involving the relation ''is contained in'' rather than ''contains''.
For example, let = be a set and = be a collection of subsets of such that:
* =
* =
* =
* =
* =
* =
* =
Then = is an exact hitting set, since each subset in is hit by (contains) exactly one element in , as the highlighting makes clear.
This exact hitting set example is essentially the same as the
detailed example above. Displaying the relation ''is contained in'' (∈) from elements to subsets makes clear that we have simply replaced lettered subsets with elements and numbered elements with subsets:
* ∈ , , ;
*
∈
,
;
* ∈ , , ;
*
∈
,
,
;
* ∈ , , , ; and
*
∈
,
.
Incidence matrix
The relation ''contains'' can be represented by an
incidence matrix
In mathematics, an incidence matrix is a logical matrix that shows the relationship between two classes of objects, usually called an incidence relation. If the first class is ''X'' and the second is ''Y'', the matrix has one row for each element o ...
.
The matrix includes one row for each subset in and one column for each element in .
The entry in a particular row and column is 1 if the corresponding subset contains the corresponding element, and is 0 otherwise.
In the matrix representation, an exact cover is a selection of rows such that each column contains a 1 in exactly one selected row. Each row represents a choice and each column represents a constraint.
For example, the relation ''contains'' in the
detailed example above can be represented by a 6×7 incidence matrix:
:
Again, the subcollection = is an exact cover, since each column contains a 1 in exactly one selected row, as the highlighting makes clear.
See the
example
Example may refer to:
* ''exempli gratia'' (e.g.), usually read out in English as "for example"
* .example, reserved as a domain name that may not be installed as a top-level domain of the Internet
** example.com, example.net, example.org, an ...
in the article on
Knuth's Algorithm X for a matrix-based solution to the detailed example above.
Hypergraph
In turn, the incidence matrix can be seen also as describing a
hypergraph
In mathematics, a hypergraph is a generalization of a Graph (discrete mathematics), graph in which an graph theory, edge can join any number of vertex (graph theory), vertices. In contrast, in an ordinary graph, an edge connects exactly two vert ...
. The hypergraph includes one node for each element in and one edge for each subset in ; each node is included in exactly one of the edges forming the cover.
Bipartite graph
The relation ''contains'' can be represented by a
bipartite graph
In the mathematics, mathematical field of graph theory, a bipartite graph (or bigraph) is a Graph (discrete mathematics), graph whose vertex (graph theory), vertices can be divided into two disjoint sets, disjoint and Independent set (graph theo ...
.
The vertices of the graph are divided into two disjoint sets, one representing the subsets in and another representing the elements in . If a subset contains an element, an edge connects the corresponding vertices in the graph.
In the graph representation, an exact cover is a selection of vertices corresponding to subsets such that each vertex corresponding to an element is connected to exactly one selected vertex.
For example, the relation ''contains'' in the
detailed example above can be represented by a bipartite graph with 6+7 = 13 vertices:

Again, the subcollection = is an exact cover, since the vertex corresponding to each element in is connected to exactly one selected vertex, as the highlighting makes clear.
Finding solutions
Algorithm X
Algorithm X is an algorithm for solving the exact cover problem. It is a straightforward Recursion (computer science), recursive, Nondeterministic algorithm, nondeterministic, depth-first, backtracking algorithm used by Donald Knuth to demonstrate ...
is the name
Donald Knuth
Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist and mathematician. He is a professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of comp ...
gave for "the most obvious trial-and-error approach" for finding all solutions to the exact cover problem.
Technically, Algorithm X is a
recursive
Recursion occurs when the definition of a concept or process depends on a simpler or previous version of itself. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in m ...
,
nondeterministic,
depth-first
Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible al ...
,
backtracking
Backtracking is a class of algorithms for finding solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons a candidate ("backtracks") as soon as it de ...
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
.
When Algorithm X is implemented efficiently using
Donald Knuth
Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist and mathematician. He is a professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of comp ...
's
Dancing Links technique on a computer, Knuth calls it DLX. It uses the matrix representation of the problem, implemented as a series of
doubly linked list
In computer science, a doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains three fields: two link fields (references to the previous and to the next node in the se ...
s of the 1s of the matrix: each 1 element has a link to the next 1 above, below, to the left, and to the right of itself. Because exact cover problems tend to be sparse, this representation is usually much more efficient in both size and processing time required. DLX then uses the
Dancing Links technique to quickly select
permutation
In mathematics, a permutation of a set can mean one of two different things:
* an arrangement of its members in a sequence or linear order, or
* the act or process of changing the linear order of an ordered set.
An example of the first mean ...
s of rows as possible solutions and to efficiently backtrack (undo) mistaken guesses.
Generalized exact cover
In a standard exact cover problem, each constraint must be satisfied exactly once.
It is a simple generalization to relax this requirement slightly and allow for the possibility that some ''primary'' constraints must be satisfied by ''exactly one'' choice but other ''secondary'' constraints can be satisfied by ''at most one'' choice.
As Knuth explains, a generalized exact cover problem can be converted to an equivalent exact cover problem by simply appending one row for each secondary column, containing a single 1 in that column. If in a particular candidate solution a particular secondary column is satisfied, then the added row isn't needed.
But if the secondary column isn't satisfied, as is allowed in the generalized problem but not the standard problem, then the added row can be selected to ensure the column is satisfied.
But Knuth goes on to explain that it is better working with the generalized problem directly, because the generalized algorithm is simpler and faster: A simple change to his Algorithm X allows secondary columns to be handled directly.
The
N queens problem is an example of a generalized exact cover problem, as the constraints corresponding to the diagonals of the chessboard have a maximum rather than an exact queen count.
Noteworthy examples
Due to its NP-completeness, any problem in NP can be
reduced to exact cover problems, which then can be solved with techniques such as Dancing Links. However, for some well known problems, the reduction is particularly direct. For instance, the problem of tiling a board with
pentominoes, and solving
Sudoku
Sudoku (; ; originally called Number Place) is a logic puzzle, logic-based, combinatorics, combinatorial number-placement puzzle. In classic Sudoku, the objective is to fill a 9 × 9 grid with digits so that each column, each row, and ...
can both be viewed as exact cover problems.
Pentomino tiling
The problem of tiling a 60-square board with the 12 different free
pentominoes is an example of an exact cover problem, as
Donald Knuth
Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist and mathematician. He is a professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of comp ...
explains in his paper "Dancing links."
For example, consider the problem of tiling with pentominoes an 8×8 chessboard with the 4 central squares removed:
:
The problem involves two kinds of constraints:
: Pentomino: For each of the 12 pentominoes, there is the constraint that it must be placed exactly once. Name these constraints after the corresponding pentominoes: F I L P N T U V W X Y Z.
: Square: For each of the 60 squares, there is the constraint that it must be covered by a pentomino exactly once. Name these constraints after the corresponding squares in the board: ''ij'', where ''i'' is the rank and ''j'' is the file.
Thus there are 12+60 = 72 constraints in all.
As both kinds of constraints are ''exactly-once'' constraints, the problem is an exact cover problem.
The problem involves many choices, one for each way to place a pentomino on the board.
It is convenient to consider each choice as satisfying a set of 6 constraints: 1 constraint for the pentomino being placed and 5 constraints for the five squares where it is placed.
In the case of an 8×8 chessboard with the 4 central squares removed, there are 1568 such choices, for example:
*
*
* …
*
*
* …
*
*
* …
One of many solutions of this exact cover problem is the following set of 12 choices:
*
*
*
*
*
*
*
*
*
*
*
*
This set of choices corresponds to the following solution to the pentomino tiling problem:

A pentomino tiling problem is more naturally viewed as an exact cover problem than an exact hitting set problem, because it is more natural to view each choice as a set of constraints than each constraint as a set of choices.
Each choice relates to just 6 constraints, which are easy to enumerate. On the other hand, each constraint relates to many choices, which are harder to enumerate.
Whether viewed as an exact cover problem or an exact hitting set problem, the matrix representation is the same, having 1568 rows corresponding to choices and 72 columns corresponding to constraints. Each row contains a single 1 in the column identifying the pentomino and five 1s in the columns identifying the squares covered by the pentomino.
Using the matrix, a computer can find all solutions relatively quickly, for example, using
Dancing Links.
Sudoku
''Main articles:
Sudoku
Sudoku (; ; originally called Number Place) is a logic puzzle, logic-based, combinatorics, combinatorial number-placement puzzle. In classic Sudoku, the objective is to fill a 9 × 9 grid with digits so that each column, each row, and ...
,
Mathematics of Sudoku
Mathematics can be used to study Sudoku puzzles to answer questions such as "How many filled Sudoku grids are there?", "What is the minimal number of clues in a valid puzzle?" and "In what ways can Sudoku grids be symmetric?" through the use o ...
,
Sudoku solving algorithms
A standard Sudoku contains 81 cells, in a 9×9 grid, and has 9 boxes, each box being the intersection of the first, middle, or last 3 rows, and the first, middle, or last 3 columns. Each cell may contain a number from one to nine, and each number ...
''
The problem in
Sudoku
Sudoku (; ; originally called Number Place) is a logic puzzle, logic-based, combinatorics, combinatorial number-placement puzzle. In classic Sudoku, the objective is to fill a 9 × 9 grid with digits so that each column, each row, and ...
is to assign numbers (or digits, values, symbols) to cells (or squares) in a grid so as to satisfy certain constraints.
In the standard 9×9 Sudoku variant, there are four kinds of constraints:
: Row-Column: Each intersection of a row and column, i.e, each cell, must contain exactly one number.
: Row-Number: Each row must contain each number exactly once
: Column-Number: Each column must contain each number exactly once.
: Box-Number: Each box must contain each number exactly once.
While the first constraint might seem trivial, it is nevertheless needed to ensure there is only one number per cell. Naturally, placing a number into a cell prohibits ''placing any other number'' into the now occupied cell.
Solving Sudoku is an exact cover problem.
More precisely, solving Sudoku is an
exact hitting set problem, which is equivalent to an exact cover problem, when viewed as a problem to select possibilities such that each constraint set contains (i.e., is hit by) exactly one selected possibility.
Each possible assignment of a particular number to a particular cell is a possibility (or candidate). When Sudoku is played with pencil and paper, possibilities are often called pencil marks.
In the standard 9×9 Sudoku variant, in which each of 9×9 cells is assigned one of 9 numbers, there are 9×9×9=729 possibilities.
Using obvious notation for rows, columns and numbers, the possibilities can be labeled
: R1C1#1, R1C1#2, …, R9C9#9.
The fact that each kind of constraint involves exactly one of something is what makes Sudoku an exact hitting set problem. The constraints can be represented by constraint sets. The problem is to select possibilities such that each constraint set contains (i.e., is hit by) exactly one selected possibility.
In the standard 9×9 Sudoku variant, there are four kinds of constraints sets corresponding to the four kinds of constraints:
: Row-Column: A row-column constraint set contains all the possibilities for the intersection of a particular row and column, i.e., for a cell. For example, the constraint set for row 1 and column 1, which can be labeled R1C1, contains the 9 possibilities for row 1 and column 1 but different numbers:
:: R1C1 = .
: Row-Number: A row-number constraint set contains all the possibilities for a particular row and number. For example, the constraint set for row 1 and number 1, which can be labeled R1#1, contains the 9 possibilities for row 1 and number 1 but different columns:
:: R1#1 = .
: Column-Number: A column-number constraint set contains all the possibilities for a particular column and number. For example, the constraint set for column 1 and number 1, which can be labeled C1#1, contains the 9 possibilities for column 1 and number 1 but different rows:
:: C1#1 = .
: Box-Number: A box-number constraint set contains all the possibilities for a particular box and number. For example, the constraint set for box 1 (in the upper lefthand corner) and number 1, which can be labeled B1#1, contains the 9 possibilities for the cells in box 1 and number 1:
:: B1#1 = .
Since there are 9 rows, 9 columns, 9 boxes and 9 numbers, there are 9×9=81 row-column constraint sets, 9×9=81 row-number constraint sets, 9×9=81 column-number constraint sets, and 9×9=81 box-number constraint sets: 81+81+81+81=324 constraint sets in all.
In brief, the standard 9×9 Sudoku variant is an exact hitting set problem with 729 possibilities and 324 constraint sets.
Thus the problem can be represented by a 729×324 matrix.
Although it is difficult to present the entire 729×324 matrix, the general nature of the matrix can be seen from several snapshots:
The complete 729×324 matrix is available from Robert Hanson.
The set of possibilities R''x''C''y''#''z'' can be arranged as a 9×9×9 cube in a 3-dimensional space with coordinates ''x'', ''y'', and ''z''. Then each row R''x'', column C''y'', or number #''z'' is a 9×9×1 "slice" of possibilities; each box B''w'' is a 9x3×3 "tube" of possibilities; each row-column constraint set R''x''C''y'', row-number constraint set R''x''#''z'', or column-number constraint set C''y''#''z'' is a 9x1×1 "strip" of possibilities; each box-number constraint set B''w''#''z'' is a 3x3×1 "square" of possibilities; and each possibility R''x''C''y''#''z'' is a 1x1×1 "cubie" consisting of a single possibility. Moreover, each constraint set or possibility is the
intersection
In mathematics, the intersection of two or more objects is another object consisting of everything that is contained in all of the objects simultaneously. For example, in Euclidean geometry, when two lines in a plane are not parallel, their ...
of the component sets. For example, R1C2#3 = R1 ∩ C2 ∩ #3, where ∩ denotes set intersection.
Although other Sudoku variations have different numbers of rows, columns, numbers and/or different kinds of constraints, they all involve possibilities and constraint sets, and thus can be seen as exact hitting set problems.
N queens problem
The
N queens problem is the problem of placing n
chess
Chess is a board game for two players. It is an abstract strategy game that involves Perfect information, no hidden information and no elements of game of chance, chance. It is played on a square chessboard, board consisting of 64 squares arran ...
queens
Queens is the largest by area of the Boroughs of New York City, five boroughs of New York City, coextensive with Queens County, in the U.S. state of New York (state), New York. Located near the western end of Long Island, it is bordered by the ...
on an n×n
chessboard
A chessboard is a game board used to play chess. It consists of 64 squares, 8 rows by 8 columns, on which the chess pieces are placed. It is square in shape and uses two colours of squares, one light and one dark, in a chequered pattern. During p ...
so that no two queens threaten each other. A solution requires that no two queens share the same row, column, or diagonal. It is an example of a
generalized exact cover problem.
The problem involves four kinds of constraints:
: Rank: For each of the ''N'' ranks, there must be exactly one queen.
: File: For each of the ''N'' files, there must be exactly one queen.
: Diagonals: For each of the 2''N'' − 1 diagonals, there must be at most one queen.
: Reverse diagonals: For each of the 2''N'' − 1 reverse diagonals, there must be at most one queen.
The 2''N'' ranks and files form the primary constraints, while the 4''N'' − 2 diagonal and reverse diagonals form the secondary constraints. Further, because each of first and last diagonals and reverse diagonals involves only one square on the chessboard, these can be omitted and thus one can reduce the number of secondary constraints to 4''N'' − 6. The matrix for the N queens problem then has ''N''
2 rows and 6''N'' − 6 columns, each row for a possible queen placement on each square on the chessboard, and each column for each constraint.
See also
*
Constraint satisfaction problem
Constraint satisfaction problems (CSPs) are mathematical questions defined as a set of objects whose state must satisfy a number of constraints or limitations. CSPs represent the entities in a problem as a homogeneous collection of finite const ...
*
Dancing Links
*
Difference map algorithm
*
Karp's 21 NP-complete problems
*
Knuth's Algorithm X
*
List of NP-complete problems
This is a list of some of the more commonly known problems that are NP-complete when expressed as decision problems. As there are thousands of such problems known, this list is in no way comprehensive. Many problems of this type can be found in ...
*
Partition of a set
In mathematics, a partition of a set is a grouping of its elements into Empty set, non-empty subsets, in such a way that every element is included in exactly one subset.
Every equivalence relation on a Set (mathematics), set defines a partitio ...
*
Perfect matching
In graph theory, a perfect matching in a graph is a matching that covers every vertex of the graph. More formally, given a graph with edges and vertices , a perfect matching in is a subset of , such that every vertex in is adjacent to exact ...
and
3-dimensional matching are special cases of the exact cover problem
References
External links
Free Software implementation of an Exact Cover solver in C- uses Algorithm X and Dancing Links. Includes examples for Sudoku and logic grid puzzles.
Exact Cover solver in Golang- uses Algorithm X and Dancing Links. Includes examples for Sudoku and N queens.
- Math Reference Project
{{DEFAULTSORT:Exact Cover
Theoretical computer science
NP-complete problems