Algorithm X
   HOME

TheInfoList



OR:

Algorithm X is an
algorithm In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algorithms are used as specificat ...
for solving the
exact cover In the mathematical field of combinatorics, given a collection of subsets of a set , an exact cover is a subcollection of such that each element in is contained in ''exactly one'' subset in . In other words, is a partition of consisting of s ...
problem. It is a straightforward
recursive Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics ...
, 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 alon ...
,
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 used by
Donald Knuth Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist, mathematician, and professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of computer sc ...
to demonstrate an efficient implementation called DLX, which uses the
dancing links In computer science, dancing links (DLX) is a technique for adding and deleting a node from a circular doubly linked list. It is particularly useful for efficiently implementing backtracking algorithms, such as Knuth's Algorithm X for the exact ...
technique. The exact cover problem is represented in Algorithm X by a matrix ''A'' consisting of 0s and 1s. The goal is to select a subset of the rows such that the digit 1 appears in each column exactly once. Algorithm X works as follows: The nondeterministic choice of ''r'' means that the algorithm recurses over independent subalgorithms; each subalgorithm inherits the current matrix ''A'', but reduces it with respect to a different row ''r''. If column ''c'' is entirely zero, there are no subalgorithms and the process terminates unsuccessfully. The subalgorithms form a
search tree In computer science, a search tree is a tree data structure used for locating specific keys from within a set. In order for a tree to function as a search tree, the key for each node must be greater than any keys in subtrees on the left, and less ...
in a natural way, with the original problem at the root and with level ''k'' containing each subalgorithm that corresponds to ''k'' chosen rows. Backtracking is the process of traversing the tree in preorder, depth first. Any systematic rule for choosing column ''c'' in this procedure will find all solutions, but some rules work much better than others. To reduce the number of iterations, Knuth suggests that the column-choosing algorithm select a column with the smallest number of 1s in it.


Example

For example, consider the exact cover problem specified by the universe ''U'' = and the collection of sets = , where: :* ''A'' = ; :* ''B'' = ; :* ''C'' = ; :* ''D'' = ; :* ''E'' = ; and :* ''F'' = . This problem is represented by the matrix: : Algorithm X with Knuth's suggested heuristic for selecting columns solves this problem as follows: Level 0 Step 1—The matrix is not empty, so the algorithm proceeds. Step 2—The lowest number of 1s in any column is two. Column 1 is the first column with two 1s and thus is selected (deterministically): : Step 3—Rows ''A'' and ''B'' each have a 1 in column 1 and thus are selected (nondeterministically). The algorithm moves to the first branch at level 1… : Level 1: Select Row ''A'' : Step 4—Row ''A'' is included in the partial solution. : Step 5—Row ''A'' has a 1 in columns 1, 4, and 7: :: : Column 1 has a 1 in rows ''A'' and ''B''; column 4 has a 1 in rows ''A'', ''B'', and ''C''; and column 7 has a 1 in rows ''A'', ''C'', ''E'', and ''F''. Thus, rows ''A'', ''B'', ''C'', ''E'', and ''F'' are to be removed and columns 1, 4 and 7 are to be removed: :: : Row ''D'' remains and columns 2, 3, 5, and 6 remain: :: : Step 1—The matrix is not empty, so the algorithm proceeds. : Step 2—The lowest number of 1s in any column is zero and column 2 is the first column with zero 1s: :: : Thus this branch of the algorithm terminates unsuccessfully. : The algorithm moves to the next branch at level 1… : Level 1: Select Row ''B'' : Step 4—Row ''B'' is included in the partial solution. : Row ''B'' has a 1 in columns 1 and 4: :: : Column 1 has a 1 in rows ''A'' and ''B''; and column 4 has a 1 in rows ''A'', ''B'', and ''C''. Thus, rows ''A'', ''B'', and ''C'' are to be removed and columns 1 and 4 are to be removed: :: : Rows ''D'', ''E'', and ''F'' remain and columns 2, 3, 5, 6, and 7 remain: :: : Step 1—The matrix is not empty, so the algorithm proceeds. : Step 2—The lowest number of 1s in any column is one. Column 5 is the first column with one 1 and thus is selected (deterministically): :: : Step 3—Row ''D'' has a 1 in column 5 and thus is selected (nondeterministically). : The algorithm moves to the first branch at level 2… :: Level 2: Select Row ''D'' :: Step 4—Row ''D'' is included in the partial solution. :: Step 5—Row ''D'' has a 1 in columns 3, 5, and 6: ::: :: Column 3 has a 1 in rows ''D'' and ''E''; column 5 has a 1 in row ''D''; and column 6 has a 1 in rows ''D'' and ''E''. Thus, rows ''D'' and ''E'' are to be removed and columns 3, 5, and 6 are to be removed: ::: :: Row ''F'' remains and columns 2 and 7 remain: ::: :: Step 1—The matrix is not empty, so the algorithm proceeds. :: Step 2—The lowest number of 1s in any column is one. Column 2 is the first column with one 1 and thus is selected (deterministically). :: Row ''F'' has a 1 in column 2 and thus is selected (nondeterministically). :: The algorithm moves to the first branch at level 3… ::: Level 3: Select Row ''F'' ::: Step 4—Row ''F'' is included in the partial solution. ::: Row ''F'' has a 1 in columns 2 and 7: :::: ::: Column 2 has a 1 in row ''F''; and column 7 has a 1 in row ''F''. Thus, row ''F'' is to be removed and columns 2 and 7 are to be removed: :::: ::: Step 1—The matrix is empty, thus this branch of the algorithm terminates successfully. ::: As rows ''B'', ''D'', and ''F'' are selected, the final solution is: :::: ::: In other words, the subcollection is an exact cover, since every element is contained in exactly one of the sets ''B'' = , ''D'' = , or ''F'' = . ::: There are no more selected rows at level 3, thus the algorithm moves to the next branch at level 2… :: There are no more selected rows at level 2, thus the algorithm moves to the next branch at level 1… : There are no more selected rows at level 1, thus the algorithm moves to the next branch at level 0… There are no branches at level 0, thus the algorithm terminates. In summary, the algorithm determines there is only one exact cover: = .


Implementations

Knuth's main purpose in describing Algorithm X was to demonstrate the utility of
dancing links In computer science, dancing links (DLX) is a technique for adding and deleting a node from a circular doubly linked list. It is particularly useful for efficiently implementing backtracking algorithms, such as Knuth's Algorithm X for the exact ...
. Knuth showed that Algorithm X can be implemented efficiently on a computer using dancing links in a process Knuth calls ''"DLX"''. DLX uses the matrix representation of the
exact cover In the mathematical field of combinatorics, given a collection of subsets of a set , an exact cover is a subcollection of such that each element in is contained in ''exactly one'' subset in . In other words, is a partition of consisting of s ...
problem, implemented as
doubly linked list In computer science, a doubly linked list is a linked data structure that consists of a set of sequentially linked record (computer science), records called node (computer science), nodes. Each node contains three field (computer science), fields: ...
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. (Technically, because the lists are circular, this forms a
torus In geometry, a torus (plural tori, colloquially donut or doughnut) is a surface of revolution generated by revolving a circle in three-dimensional space about an axis that is coplanar with the circle. If the axis of revolution does not tou ...
). 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 dancing links to quickly select permutations of rows as possible solutions and to efficiently backtrack (undo) mistaken guesses.


See also

*
Exact cover In the mathematical field of combinatorics, given a collection of subsets of a set , an exact cover is a subcollection of such that each element in is contained in ''exactly one'' subset in . In other words, is a partition of consisting of s ...
*
Dancing Links In computer science, dancing links (DLX) is a technique for adding and deleting a node from a circular doubly linked list. It is particularly useful for efficiently implementing backtracking algorithms, such as Knuth's Algorithm X for the exact ...


References

*.


External links


Knuth's paper
- PDF file (also )
Knuth's Paper describing the Dancing Links optimization
- Gzip'd postscript file. {{Donald Knuth navbox Search algorithms Donald Knuth