Assemblies using dancing links and dancing cells: how the Solver finds every way the pieces fill a shape

The puzzle as an exact cover problem, Donald Knuth’s dancing links, the counted cover used when several pieces share a shape, the vector search, and why dancing cells was measured and left out

22 November 2026 · Sources: core/shapelogic/solve/dlx_matrix.{h,cpp}, assembler_0.cpp, assembler_1.cpp, simd_exact_cover.cpp and simd_huang_cover.cpp; counts from a small program that builds and searches the Solver’s own matrices, run on puzzles from ShapeLogic’s test and benchmark files; timings from the benchmark notes in tests/bench/README.md (ShapeLogic 2.0, MacBook Air M4, 32 GB)

Where this page fits

The Solver works on a puzzle in two stages. This page is about the first: finding the assemblies, every way the pieces can fill the shape. At this stage the pieces are simply set in their places, as if they could pass through each other. Whether an assembly can really be put together is the second stage, the disassembly, which takes each assembly apart move by move. That stage is described in Puzzle Solving: Disassembly using Breadth First and Graphs.

Summary

  1. The puzzle as an exact cover problem
  2. One column at a time
  3. Dancing links
  4. Turned and mirrored copies
  5. Assembler 0: what ShapeLogic adds
  6. Several pieces of one shape: Assembler 1
  7. Dancing cells
  8. Which puzzle gets which search
  9. Where it comes from

1. The puzzle as an exact cover problem

Before the Solver can take anything apart, it has to know which arrangements of the pieces fill the shape. Each piece can sit in the shape in a number of ways: in each of its orientations, at every position where all its cells land on cells of the shape. Each of these is a placement. An assembly is a choice of placements, one for each piece, that fills every cell of the shape exactly once.

Mathematicians call this an exact cover problem, and it is easiest to see as a table, which the Solver calls the matrix. There is one row for each placement and one column for each piece and for each cell of the shape. A row has a mark in the column of its piece and in the columns of the cells it fills. An assembly is a set of rows that, between them, have exactly one mark in every column. The piece columns make sure each piece is used once. The cell columns make sure each cell is filled once, with no gaps and no overlaps.

Figure 1 does this for a puzzle small enough to see whole: a box of 4 × 2 cells and three pieces. A is a single cell, B an L of three cells and C an L of four. A can go in 8 places, B in 12 and C in 8, which gives 28 rows and 3 + 8 = 11 columns. The figures and animations below all use this puzzle.

The box12345678The piecesA: one cell8 placementsB: an L of three cells12 placementsC: an L of four cells8 placementsThe matrix: one row per placement, one column per piece and per cellpiecescells of the boxABC123456788 rows for A12 rows for B8 rows for C
Figure 1. The small puzzle and its matrix. Each row is one placement, drawn small on the left and as its marks on the right: one in its piece’s column and one in each cell it fills. An assembly is a set of rows with exactly one mark in every column.

Real puzzles give the same kind of table, only bigger. The twelve pentominoes in a 6 × 10 box need 12 piece columns, 60 cell columns and 1,864 rows. The matrix of Dracula’s Dental Disaster has over 1,600 cell columns. For a puzzle in three dimensions, an orientation is any of the turns the grid allows, so a piece is tried in all 24 orientations of a cube.

2. One column at a time

Donald Knuth called the plain way of searching such a table Algorithm X. Every column has to be used by some row, so the search can pick any column and try each of its rows in turn:

  1. If no columns are left, the rows chosen so far are an assembly.
  2. Otherwise pick a column. If it has no rows, this branch is a dead end: go back.
  3. For each row of that column: choose it, take out every column the row uses and every other row that needs one of those columns, search what is left, then put it all back.

Which column to pick matters a great deal. The best is the one with the fewest rows, a rule Knuth’s paper calls the S heuristic. A column with one row is a forced move, and a column with none ends the branch before any more work is done on it. ShapeLogic’s plain search takes the column with the fewest rows among the piece columns and the cell columns that must be filled, and the first of them in order when there is a tie.

In the small puzzle the corner cells have the fewest rows, 7 each, because A, B and C can each cover a corner in only a few ways. Cell 1 is the first of them, so the search tries its 7 rows in turn. Figure 2 shows the whole search. Four branches end in assemblies. Four end in dead ends: after A on 1 and B on 2 5 6, for instance, piece C has nowhere left to go.

cell 1: 7 rowscell 5: 2 rowspiece C: 0 rowspiece B: 0 rowspiece C: 0 rowspiece C: 1 rowpiece A: 1 rowassemblypiece C: 1 rowpiece A: 1 rowassemblypiece B: 1 rowpiece A: 1 rowassemblypiece B: 1 rowpiece A: 1 rowassemblypiece B: 0 rows
Figure 2. The complete search of the small puzzle, from the empty box at the top. Under each box is the column the search chose next and how many rows it had. A red frame is a dead end, where a column had no rows left. A green frame is an assembly.

3. Dancing links

Almost all the work of the search is taking rows and columns out of the matrix and putting them back, millions or billions of times. In 2000 Knuth published a way to do this that he called dancing links, after a trick of Hitotumatu and Noshita from 1979. Each mark of the matrix is a small record, a node, that sits in two circular lists at once: across, in its row, and down, in its column. Each column also has a header that counts its rows.

The trick is in how a node leaves a list and comes back (Figure 3). To take node x out, its left neighbor is pointed past it to its right neighbor, and the right neighbor back to the left one. That is two writes. Nothing is written to x itself, so it still points to the neighbors it had. To put it back, the same two writes are made the other way round, using x’s own links to find where it goes. As long as nodes are put back in the reverse of the order they were taken out, every list comes back exactly as it was. No copy of the matrix is made, and no memory is allocated or freed during the search. Knuth’s name comes from watching the links go out and back in.

1. In the listpxqR: the link to the rightL: the link to the left2. Taken outpxqp and q now skip xx still knows p and qR[L[x]] ← R[x]L[R[x]] ← L[x]3. Put backpxqx’s own links lead back inR[L[x]] ← xL[R[x]] ← x
Figure 3. A node x leaves a doubly linked list and comes back. R is the link to the right and L the link to the left. While x is out, its neighbors skip it, but x keeps its own links (dashed), and they are all that is needed to put it back.

Covering a column uses this twice over. The column’s header leaves the list of headers, and then every row that has a mark in the column leaves all the other columns it is in, with each of those columns’ counts going down by one. Choosing a row covers each of its columns in turn. Uncovering does the same steps backwards. A covered row never needs to be looked at again until it comes back.

The animation shows the whole search of the small puzzle on its matrix. The column the search is working on is shaded, the rows it has chosen are outlined, and pale rows and columns are the ones currently linked out.

Animation: the dancing links search of the small puzzle, step by step, on its matrix and in the box
Figure 4. The dancing links search of the small puzzle, step by step (45 steps, about 90 seconds at 1×, then it starts again). Each step says what the search did. Going back several levels at once is shown as one step. The arrows (or the arrow keys) go a step back or forward, and clicking the picture pauses or plays it.

ShapeLogic keeps the nodes in plain arrays, one each for left, right, up and down links and one for the column (DlxNodes in dlx_matrix.h), with the up and down links side by side so that the cover loops need one register fewer. The plain search is written as a loop over an explicit list of the rows chosen, not as a function that calls itself. It can therefore stop after any step, be saved in the puzzle file, and carry on later from the same place, which is how the Solver pauses and how its autosave works.

4. Turned and mirrored copies

The small puzzle’s four assemblies are really one arrangement (Figure 5). The other three are the first one mirrored or turned half way round. In three dimensions, turning a flat arrangement over is just another rotation, so all four are the same puzzle seen from different sides. The same happens in every puzzle whose shape has symmetries. A cube has 24, so each assembly of a puzzle that fills a cube could turn up 24 times.

found firstmirrored top to bottommirrored left to rightturned half way
Figure 5. The four assemblies the search of Figure 2 finds. They are one arrangement in four positions.

The Solver avoids most of these copies before they are found. One piece, the symmetry breaker, is placed in fewer orientations than the others, so that most turned copies of an assembly have no rows to be built from. The piece chosen is the one that shares the fewest symmetries with the shape. Any copies that still get through are compared with the assemblies’ turned versions and dropped, unless the Solver’s Keep Rotated or Keep Mirror options are on. For the twelve pentominoes in a 6 × 10 box the Solver reports 2,339 assemblies, the number Knuth gives in his paper for the different ways to fill that box.

5. Assembler 0: what ShapeLogic adds

Assembler 0 is the dancing links search above, used whenever every part of the puzzle is exactly one piece, the usual case for burrs and boxes. It has three additions.

Cells that may stay empty

A puzzle’s shape can have cells marked as allowed to stay empty. Each of these gets a column too, but a column that is never picked to branch on and need not be covered. The puzzle says how many of them, its holes, may stay empty in an assembly. A branch in which more of these columns have no rows left than there are holes allowed is a dead end. Dracula’s Dental Disaster has 720 cells that must be filled and 936 that may stay empty.

Removing placements that can never be used

Before the search, the Solver trims the matrix with two tests, repeated until nothing more changes:

Columns that turn out to be identical, filled by exactly the same placements, are then merged into one. That saves no search steps but makes each one cheaper. On a burr the trimming can remove almost everything. Pelikan Burr’s matrix starts with 3,026 placements and 519 columns and ends with 15 placements, with 291 columns merged away.

The vector search

Dancing links is fast, but every step follows pointers around memory. Modern processors can instead test hundreds of bits at once with vector instructions. So when the matrix has at most 2,048 columns, Assembler 0 uses a different search over the same table. Each row becomes a bit mask, one bit per column. At each step the search keeps the list of rows that are still possible. When it chooses a row, it makes the next list by testing every row on the current one against the chosen row’s mask. One AND per row is enough: a row that shares no column with the chosen row gives zero and stays (Figure 6).

ABC12345678B on 1 5 6the row just chosenA on 2AND gives 0: keptA on 7AND gives 0: keptB on 2 3 7shares a column: droppedC on 2 3 4 8AND gives 0: keptC on 1 5 6 7shares a column: droppedB on 1 2 5shares a column: droppedA on 4AND gives 0: kept
Figure 6. After B on 1 5 6 is chosen, each remaining row is tested with one AND against its mask. Red marks are the columns a row shares with the chosen row. Those rows leave the list. The others are kept for the next step.

The processor does the AND 128 bits at a time with NEON on Apple silicon, or 256 or 512 bits at a time with AVX2 or AVX-512 on Intel and AMD. The Solver picks the instructions when it starts, and has a plain version for processors with none of them. Column choice is the same rule as before, the fewest rows still possible. Nothing is ever unlinked, so there is nothing to put back: going back means returning to the list one level up. And because the matrix itself never changes, several threads can search different branches of the same matrix at once. The search is split into tasks by its first few choices, and a task that turns out to be large splits again while it is being searched. The vector search is the default whenever the matrix fits. The plain dancing links search runs otherwise, and in the assembler’s step-by-step debugger.

6. Several pieces of one shape: Assembler 1

Exact cover as described so far has a problem with identical pieces. If a puzzle has three dominoes, each one needs a piece column of its own, and the search then tells them apart. Every way of filling the shape is found once for each way of handing out the names (Figure 7). Three dominoes in a 3 × 2 box can be laid in 3 ways, but with named pieces the search finds 18 covers. With 32 dominoes on an 8 × 8 board each tiling would be found 32! times, about 2.6 × 1035.

Three dominoes as three pieces D1, D2, D3a column for each: the same tiling is found once per order of the namesD1D2D3D1D3D2D2D1D3D2D3D1D3D1D2D3D2D1This tiling 6 times; all 3 tilings: 18 coversOne shape column: dominoes, at least 3, at most 3each row adds 1 to the column; the pieces are told apart by placeEach tiling once: 3 coversWith 32 dominoes on an 8 x 8 board the first way wouldfind each of the 12,988,816 tilings 32! (2.6 × 1035) times.
Figure 7. Three dominoes in a 3 × 2 box. Left: with a column for each domino, one tiling is found once for each of the 6 orders of the names. Right: with one counted column for the shape, each of the 3 tilings is found once.

Assembler 1 handles every problem Assembler 0 cannot, using a cover with counts after an idea of Wei-Hwa Huang’s. Instead of “exactly once”, each column has a minimum and a maximum:

The pieces of a shape have no names, so a set of placements is one cover and is found once. The search branches on the column that leaves the fewest possibilities, judged by its number of rows and how much more it may take. Knuth’s own book treats the same extension of exact cover, as covering with multiplicities. Assembler 1 has a vector search of its own too, with bit masks of up to 32,768 cells, used when its tables fit in a memory budget (256 MB unless set otherwise).

The counts bear this out. For 32 dominoes on an 8 × 8 board the search finds 12,988,816 covers, the known number of ways to tile a chessboard with dominoes, each exactly once. Turned and mirrored copies are then dropped, which leaves 1,629,189 assemblies. Identical pieces cannot serve as a symmetry breaker, so here every cover is found in each of its positions and has to be checked. Section 7 shows that this check, not the search, was where the time went. Solid Six Piece Burrs is another Assembler 1 puzzle: it lets each of 25 shapes be used anywhere from 0 to 6 times.

7. Dancing cells

Knuth has since described a second way to take things out of a search and put them back, which he calls dancing cells. It is part of the newer, still unfinished sections of The Art of Computer Programming (section 7.2.2.3), and it replaces the linked lists with sparse sets, a structure Preston Briggs and Linda Torczon described in 1993. Each item, for example a cell, keeps its options in an array, with the ones still in play first and a count of how many there are. To take an option out of play, it is swapped with the last one in play and the count drops by one. A second table records where each option sits, so it can be found without a search. Going back means only putting the saved counts back. The options are then in a different order, but the part in play holds the same ones (Figure 8). The arrays sit together in memory, which suits processor caches better than lists of nodes scattered around.

1. The item’s options, all liver1r2r3r4r5r6size = 62. r2 and r5 hidden: swapped to the endr1r6r3r4r5r2size = 43. Back: the size is set to 6 againr1r6r3r4r5r2size = 6An option is hidden from an item by swapping it with the last liveoption and taking 1 off the size. A table of where each option sitsfinds it without a search. Nothing outside the array changes.Going back puts the old sizes back. The order inside the array isdifferent, but the live part holds the same options, so the searchgoes on exactly as it would have. Dancing links must insteadrelink every node it unlinked, in the reverse order.
Figure 8. One item’s options as a sparse set. The line marks the count of options in play. Hiding r2 and r5 swaps them behind it, and going back moves only the line.
Animation: the options of cell 1 in the small puzzle as a sparse set, hidden one by one when A is placed on cell 2, and brought back by resetting the size
Figure 9. Dancing cells on the small puzzle. When A goes on cell 2, the five options of cell 1 that need piece A or cell 2 are swapped past the line one by one. Taking A off again only moves the line back.

ShapeLogic has met dancing cells twice.

BoxCoversAssembler 1, vectorAssembler 1, plainV2 dancing cellsV2 dancing links
20 dominoes, 2 × 4 × 5535,2290.094 s0.187 s0.137 s0.049 s
12 L-trominoes, 3 × 3 × 4471,2960.110 s0.282 s0.249 s0.117 s
Ranges, 3 × 3 × 3716,8350.485 s1.681 s1.424 s0.877 s
3 of each tetromino, 6 × 10023.3 s55.9 s36.7 s31.1 s
32 dominoes, 8 × 812,988,8162.13 s5.05 s3.73 s1.32 s
Ranges, 6 × 622,553,2805.27 s21.1 s15.1 s6.89 s

Dancing cells was slower than Assembler 1’s vector search on every box, by 1.5 to 3 times, and slower than V2’s own dancing links on every box too. V2’s dancing links beat the vector search on the two domino boxes, where each step does very little. On these boxes, though, the search turned out to be a small part of the solve. Of the 58 seconds ShapeLogic then took for 32 dominoes on 8 × 8, the search was 2.1 seconds. Building an assembly for each of the 13 million covers took 16.6 seconds, and checking each against its turned versions took 39.

So no dancing cells assembler was built. That check was rewritten instead to work on the rows of the matrix, so that only the covers that are kept are ever built into assemblies. Times on one thread:

PuzzleBeforeFaster sort onlyCheck on the rows
20 dominoes, 2 × 4 × 51.13 s0.75 s0.19 s
Ranges, 3 × 3 × 31.32 s1.29 s0.57 s
32 dominoes, 8 × 858.1 s36.7 s8.9 s
Ranges, 6 × 640.2 s36.0 s9.9 s
Burr-Glar21.4 s21.4 s15.4 s

8. Which puzzle gets which search

The Solver chooses the assembler from the puzzle itself. There is no setting for it, and the solver type, Complete or Fast, does not change it, only the take-apart.

The puzzleAssemblerSearch
Every part is exactly one pieceAssembler 0: exact cover with dancing linksThe vector search when the matrix has at most 2,048 columns, otherwise the plain dancing links search. Either one splits over threads.
Several pieces of a shape, or ranges of piecesAssembler 1: cover with countsIts vector search when its tables fit the memory budget, otherwise its plain search. Either one splits over threads.

The table below shows what the matrices of some real puzzles look like. Placements are counted once for each shape. “After reduce” is what is left once the placements that can never be used are removed, which the Solver does by default. Search nodes are the nodes the vector search entered on one thread (the plain search counts its steps differently, so the two are not comparable).

PuzzleAssemblerParts / piecesCellsPlacementsAfter reduceAssembliesSearch nodes
The twelve pentominoes in a 6 × 10 box012601,8641,7382,3391,828,522
Bermuda012721,2641,21411,877
Pelikan Burr07296 + 216 may stay empty3,026151237
Dracula’s Dental Disaster09720 + 936 may stay empty643284190
Solid Six Piece Burrs125 / 1501041,593330588204,159
20 dominoes in a 2 × 4 × 5 box11 / 2040828283,4421,837,949
A 3 × 3 × 3 box from three shapes, each in a range of counts13 / 242731531514,9925,609,985
32 dominoes on an 8 × 8 board11 / 32641121121,629,18950,102,176

The burrs show how much the trimming of section 5 can do. Pelikan Burr’s 15 remaining placements give 12 assemblies in 37 nodes. A box of pentominoes has no such help: its pieces fit in many places, and 1.8 million nodes are needed to find its 2,339 assemblies. The boxes of identical pieces are where Assembler 1’s counting matters, and where most of the time goes on the covers rather than the search.

9. Where it comes from

Donald Knuth, professor emeritus at Stanford University and author of The Art of Computer Programming, described Algorithm X and dancing links in his paper “Dancing Links” in 2000, using pentomino puzzles as one of his examples. The book treats them fully in Volume 4B (2022), section 7.2.2.1, together with exact cover with multiplicities. Dancing cells belongs to the sections he is still writing.

ShapeLogic’s two assemblers come from BurrTools, the puzzle solver written by Andreas Röver, from which ShapeLogic was ported and whose answers it is tested against. Assembler 0 is Knuth’s algorithm. Assembler 1 is the cover with counts after Wei-Hwa Huang. The vector searches and the dancing cells solver came from the BurrTools version that ShapeLogic was first ported from. The check of rotated copies on the matrix rows, and tasks that split while they are searched, were added in ShapeLogic in November 2026, as a result of the dancing cells measurements.

Sources

← All research