ArticleslgStudy

computer science

Knuth's Algorithm X

Knuth's Algorithm X is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Knuth's Algorithm X rather than just read about it. In short: Algorithm X is an algorithm for solving the exact cover problem. 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.

Key takeaways

  • Knuth's Algorithm X belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Knuth's Algorithm X to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Knuth's Algorithm X from memory before moving on to harder problems.

Reference excerpt

Algorithm X is an algorithm for solving the exact cover problem. 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.

Algorithm The exact cover problem is represented in Algorithm X by an incidence 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:

If the matrix A has no columns, the current partial solution is a valid solution; terminate successfully. Otherwise choose a column c (deterministically). Choose a row r such that Ar, c = 1 (nondeterministically). Include row r in the partial solution. For each column j such that Ar, j = 1, for each row i such that Ai, j = 1, delete row i from matrix A. delete column j from matrix A. Repeat this algorithm recursively on the reduced matrix A.

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 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 = {1, 2, 3, 4, 5, 6, 7} and the collection of sets S = {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}. 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:

No rows and no columns remain:

Step 1—The matrix is empty, thus this branch of the algorithm terminates successfully. 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}. 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: S* = {B, D, F}.

Implementations Knuth's main purpose in describing Algorithm X was to demonstrate the utility of dancing links. 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 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. (Technically, because the lists are circular, this forms a torus). 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 Dancing Links

References

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Knuth's Algorithm X

Start with the simplest possible case. Write down what Knuth's Algorithm X claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Knuth's Algorithm X before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Knuth's Algorithm X ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Knuth's Algorithm X

In research
Knuth's Algorithm X appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Knuth's Algorithm X in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Knuth's Algorithm X is common in secondary-school and first-year university syllabi. It links to neighbouring topics Donald Knuth, Search algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Knuth's Algorithm X outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Knuth's Algorithm X in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Knuth's Algorithm X means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Knuth's Algorithm X out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Knuth's Algorithm X in simple terms?

Algorithm X is an algorithm for solving the exact cover problem. 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.

Why does Knuth's Algorithm X matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Knuth's Algorithm X?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Knuth's Algorithm X.

Tags

  • Donald Knuth
  • Search algorithms

Keep exploring