encyclopegia.org

Knuth's Algorithm X

Algorithm X is an algorithm for solving the exact cover problem.[src] It is a straightforward recursive, nondeterministic, depth-first, backtracking algorithm used by Donald Knuth to demonstrate an efficient implementation called DLX, which uses the dancing links technique.[src]

Algorithm X is an algorithm for solving the exact cover problem.[src] It is a straightforward recursive, nondeterministic, depth-first, backtracking algorithm used by Donald Knuth to demonstrate an efficient implementation called DLX, which uses the dancing links technique.[src] ==Algorithm== The exact cover problem is represented in Algorithm X by an incidence matrix A consisting of 0s and 1s.[src] The goal is to select a subset of the rows such that the digit 1 appears in each column exactly once.[src] Algorithm X works as follows: If the matrix A has no columns, the current partial solution is a valid solution; terminate successfully.[src] Otherwise choose a column c (deterministically).[src] Choose a row r such that A r, c = 1 (nondeterministically).[src] Include row r in the partial solution.[src] For each column j such that A r, j = 1, for each row i such that A i, j = 1, delete row i from matrix A.[src] delete column j from matrix A.[src] Repeat this algorithm recursively on the reduced matrix A.[src] 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.[src] If column c is entirely zero, there are no subalgorithms and the process terminates unsuccessfully.[src] The subalgorithms form a search tree in a natural way, with the original problem at the root and with level k containing each subalgorithm that corresponds to k chosen rows.[src] Backtracking is the process of traversing the tree in preorder, depth first.[src] Any systematic rule for choosing column c in this procedure will find all solutions, but some rules work much better than others.[src] To reduce the number of iterations, Knuth suggests that the column-choosing algorithm select a column with the smallest number of 1s in it.[src] ==Example== For example, consider the exact cover problem specified by the universe U = {1, 2, 3, 4, 5, 6, 7} and the collection of sets = {A, B, C, D, E, F}, where: A = {1, 4, 7}; B = {1, 4}; C = {4, 5, 7}; D = {3, 5, 6}; E = {2, 3, 6, 7}; and F = {2, 7}.[src] 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.[src] Step 2—The lowest number of 1s in any column is two.[src] 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).[src] 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.[src] 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.[src] 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.[src] 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.[src] 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.[src] 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.[src] 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.[src] Step 2—The lowest number of 1s in any column is one.[src] 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).[src] 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.[src] 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.[src] 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.[src] Step 2—The lowest number of 1s in any column is one.[src] 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).[src] 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.[src] 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.[src] Thus, row F is to be removed and columns 2 and 7 are to be removed: No rows and no columns remain: Step 1—The matrix is empty, thus this branch of the algorithm terminates successfully.[src] As rows B, D, and F have been selected (step 4), the final solution in this branch is: In other words, the subcollection {B, D, F} is an exact cover, since every element is contained in exactly one of the sets B = {1, 4}, D = {3, 5, 6}, or F = {2, 7}.[src] 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.[src] In summary, the algorithm determines there is only one exact cover: = {B, D, F}.[src] ==Implementations== Knuth's main purpose in describing Algorithm X was to demonstrate the utility of dancing links.[src] Knuth showed that Algorithm X can be implemented efficiently on a computer using dancing links in a process Knuth calls "DLX".[src] DLX uses the matrix representation of the exact cover problem, implemented as doubly linked lists 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.[src] (Technically, because the lists are circular, this forms a torus).[src] Because exact cover problems tend to be sparse, this representation is usually much more efficient in both size and processing time required.[src] DLX then uses dancing links to quickly select permutations of rows as possible solutions and to efficiently backtrack (undo) mistaken guesses.[src] ==See also== Exact cover Dancing Links ==References== .[src] ==External links== Knuth's paper - PDF file (also ) Knuth's Paper describing the Dancing Links optimization - Gzip'd postscript file.[src]

51 sentences · 51 cited inline · 0 references stored

References

No reference rows published for this article.

Cite this article

MLA

"Knuth's Algorithm X." encyclopegia.org, 2025, https://encyclopegia.org/wiki/knuth-s-algorithm-x. Accessed 30 November 2026.

Chicago

"Knuth's Algorithm X." encyclopegia.org. Accessed November 30, 2026. https://encyclopegia.org/wiki/knuth-s-algorithm-x.

APA

encyclopegia.org. (2025). Knuth's Algorithm X. Retrieved November 30, 2026, from https://encyclopegia.org/wiki/knuth-s-algorithm-x

Accessed November 30, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

μ-law algorithm

stub · references available

bitap algorithm

stub · references available

QR algorithm

stub · references available

Prim's algorithm

stub · references available

greedy algorithm

stub · references available

Apriori algorithm

stub · references available

Frederik Marcus Knuth

stub · references available

Baum–Welch algorithm

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1267470602 · 0.5 · snapshot enwiki-001267470602

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools
  • Careers — open roles: frontier engineers and sales partners
  • Index — every corpus, paper and record
  • Estate guide — what a public data estate is
  • Founder — Lois-Kleinner Alpasan
  • Records — DOIs and external deposits
  • Documents — one-pagers, PDFs and QR codes
  • Brand kit — logos and share images
  • Papers — plain-language briefs with citations
  • Browser — search engine, PWA and bookmarklet
  • Anticloud — the operator and its projects

As seen in Business Insider · 336 outlets · evidence DOIs · plain-language papers

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.