AI Glossary / Graphs and symbolic AI / Advanced

Dancing links

Definition

Dancing links is a technique for efficiently removing and restoring elements in linked data structures during backtracking. It is best known through Donald Knuth's DLX implementation of Algorithm X for exact-cover problems.

What it means

Backtracking repeatedly makes a choice, simplifies the remaining problem, and reverses those changes if the choice fails or when searching for another solution. Copying the entire problem at every branch can be expensive.

Dancing links keeps the structure in place and temporarily disconnects elements while retaining enough information to restore them. The removals and restorations follow a carefully controlled order, giving the technique its name.

For exact cover, the goal is to choose subsets so that every required item is covered exactly once. This differs from ordinary set cover, where overlaps may be allowed and the goal may be to use as few subsets as possible. Correctly expressing “exactly once” is essential before the data structure can help.

How it works

Imagine an element x in a doubly linked list, with neighbors L and R. To remove x, connect L directly to R and R directly to L. Keep x's own stored links to its former neighbors. When surrounding changes have been undone appropriately, those retained links let x be reinserted without searching for its old position.

DLX represents the 1s of a sparse exact-cover matrix as nodes linked horizontally by row and vertically by column. A column represents a required item; a row represents a candidate subset containing the items marked by its 1s.

Algorithm X chooses an uncovered column and tries each available row covering it. Selecting a row covers all its items and temporarily removes conflicting choices. Recursion searches the remaining problem. Failure triggers restoration in reverse order, allowing another candidate to be tried.

Choosing a column with few remaining candidate rows often reduces branching. An uncovered column with zero candidates proves the current branch impossible. Covering all required columns identifies a solution. The fast local updates reduce bookkeeping costs but do not remove the combinatorial difficulty of general exact cover.

Three practical examples

  • A tiling solver represents each legal piece placement as a row covering the board cells and piece identity it uses.
  • A Sudoku solver represents candidate digit placements as rows satisfying cell, row-digit, column-digit and box-digit requirements.
  • A roster experiment uses exact cover when each required slot must be assigned exactly once and candidate assignments can be encoded as compatible subsets.

Think of it this way

Think of removing a card from a carefully ordered filing system while leaving its “between these two cards” markers intact. If later changes are undone in the right order, the card can return to its original place quickly.

The analogy does not permit arbitrary concurrent editing or destruction of removed cards. Dancing links depends on preserving nodes, their restoration information and the update order required by the search.

Worked example

Finding two exact covers

Let the required items be {1,2,3,4}. Candidate rows are A={1,2}, B={3,4}, C={1,3} and D={2,4}.

Choose item 1. It has two candidate rows, A and C. Trying A covers items 1 and 2. Row C conflicts on item 1 and row D conflicts on item 2, so both become unavailable in that branch. B remains and covers items 3 and 4. The selected rows {A,B} form an exact cover.

To find further solutions, backtrack and restore the removed choices. Now try C, which covers items 1 and 3. A conflicts on item 1 and B conflicts on item 3. D remains, covering 2 and 4, so {C,D} is another exact cover.

Rows A and D together are not a solution: item 2 is covered twice and item 3 is missing. The dancing-links structure makes the temporary exclusions reversible; it does not change which combinations are mathematically valid.

Illustrative case study

A fictional puzzle publisher, Alder Puzzles, uses an exact-cover solver to generate tiling challenges and count solutions. A developer initially optimizes the solver by clearing the links of nodes removed from a column.

That seemingly tidy change destroys the information needed for restoration. Some branches then miss candidate placements after backtracking. The team restores the intended link-preservation rule and tests that covering followed by uncovering recreates the original structure.

The publisher separately checks each reported tiling against the board and piece rules. It distinguishes “a solution was found” from “exactly one solution exists,” since uniqueness requires exhausting or otherwise correctly ruling out alternative covers.

Common misconceptions and mistakes

  • Dancing links is the same thing as the exact-cover problem. It is an implementation technique used while solving that and related search problems.
  • Removed nodes can be freed immediately. Their retained state is needed for later restoration.
  • Efficient pointer changes make general exact cover polynomial-time. The search can still have exponentially many branches.
  • Finding one cover proves uniqueness. Other branches may contain additional solutions.

Business use

Dancing links can support specialist configuration, placement and scheduling tools when the requirements fit an exact-cover formulation. The first task is modeling: determine which requirements must be met exactly once and which restrictions need a different representation or an extension.

Compare the complete solver with other approaches, including constraint programming or satisfiability methods, on representative cases. Account for the size of the candidate-row generation stage, not just the speed of pointer updates. A compact search engine can still be overwhelmed if the model requires an enormous number of possible placements.

People also ask

Why are the links called dancing?

They are repeatedly rearranged and restored as the search advances and retreats.

Does DLX require a dense matrix?

No. Its linked representation is particularly natural for sparse matrices containing relatively few 1s.

Can it stop after finding one solution?

Yes, if feasibility is the question. Enumeration, counting and uniqueness checking require a search policy appropriate to those different objectives.

Check your understanding

A solver chooses A={1,2} and D={2,4} for universe {1,2,3,4}. Has it found an exact cover because every selected row is individually valid?

Answer: No. Their combination repeats item 2 and omits item 3. Exact-cover validity concerns the combined coverage.

Related terms

  • Sparse matrix
  • Constraint satisfaction
  • Algorithm X
  • exact cover
  • backtracking
  • doubly linked list
  • search heuristic

Learn next

Write the four-row example as a 0-1 matrix. Trace the rows removed after choosing A, then restore them and choose C. Finally, add a candidate row and check whether it introduces another complete cover rather than assuming it improves an existing one.

More in Graphs and symbolic AI

  • CYK algorithm - The CYK algorithm, named for Cocke, Younger and Kasami, is a dynamic programming method for recognizing and parsing strings with a context-free grammar.
  • D* - D*, or Dynamic A*, is an incremental path-planning algorithm that repairs a route when new information changes edge costs in a graph.
  • D-Separation - D-separation is a graphical criterion for determining which conditional independences are implied by a directed acyclic graph.
  • Data complexity - Data complexity measures the computational difficulty of a task as the data grows while the query, rule set or other specified logical description is held...
  • Data compression - Data compression represents information using fewer bits than a chosen original representation, when the data and method permit it.
  • Data-oriented parsing - Data-oriented parsing, or DOP, is an approach to analyzing language by combining fragments of previously analyzed utterances.

Sources

Donald E. Knuth, Dancing Links, original paper explaining reversible list updates, Algorithm X and the DLX structure: https://arxiv.org/abs/cs/0011047

Author's implementation collection: https://cs.stanford.edu/~knuth/programs.html

The four-item walkthrough and Alder Puzzles case are original examples.

Previous: DAMSL Next: Dangling tuple

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer