Title: CS575: Dancing Links
1CS575 Dancing Links
- A backtrack data structure and algorithm
- by Donald Knuth
- (wim bohm cs.colostate.edu)
Except as otherwise noted, the content of this
presentation is licensed under the Creative
Commons Attribution 2.5 license.
2Dancing Links
x
- Doubly linked list
- Remove x
- RLxRx
- LRxLx
- Put x back
- RLxx
- LRxx
x
x
3Using Dancing Links
- Great O(1) put x back operation
- Works in following BackTrack scenario
- . we have created a space to be searched
as a global - doubly linked data structure
- . we search this space by trial and error,
taking out - certain options and putting them back in
reverse order - What does not work
- . adding completely new options
- . putting options back in other than reverse
order
4Backtrack, depth first search
- Given a matrix of 0-s and 1-s, find a subset of
rows with exactly one 1 in each column of A - Trial and error
- Pick an column c, pick a row r with 1 in c,
cancel columns j with 1 in r and rows with 1 in j - a b c d e f g
- 1 0 0 1 0 1 1 0
1,4,5 - 2 1 0 0 1 0 0 1
- 3 0 1 1 0 0 1 0
- 4 1 0 0 1 0 0 0
- 5 0 1 0 0 0 0 1
5Exact set cover
- The columns elements of a universe
- The rows subsets in the universe
- Find a set of subsets that has each element
exactly once - Union of the set is the Universe
- Intersection of any two subsets is empty
- Finding an exact cover is tough
- NP-Complete e.g. when each subset has three
elements - Great candidate for backtrack search
- Represent matrix row column doubly linked sparse
- only containing 1-s
- Use dancing links to remove and put back
candidates
6Pentominoes
- A pentomino is a size 5 n-omino composed of n
congruent squares connected orthogonally - There are 12 different pentominoes when rotation,
and mirroring are allowed - There are 18 pentominoes when only rotation is
allowed - Dana Scott wrote a backtrack program in 1958 for
the Maniac (4000 instructions / sec) tiling a
chessboard with a 2x2 hole in the middle with the
12 pentominoes, using each pentomino exactly once - The code ran in 3.5 hours (50 million
instructions) - Lets go on line
7The 12 pentominoes
F I L N P
T
U V W X
Y Z
8One of the 65 solutions
9Pentominoes and exact cover
- Tiling different board shapes
- Chessboard with 2x2 hole in middle, with 4 holes
in arbitrary places - Rectangles (6x10, 5x12, 4x15, 3x20)
- 3D boxes
- Can be formulated as exact cover problems
- Matrix with 72 columns
- 12 pentominoes and 60 positions in the boards
grid - Each row has 6 1-s
- 1 for the pentomino, 5 for its positions
- Each row is a description of a pentomino in a
certain position - There are 1568 such rows
10Translating puzzle to set cover
- Pentominoes matrix 72 columns 1568 rows
- Lets do a simpler puzzle
- 4 triominoes
- I L
- - No rotation or flip
- Rectangle 3 x 4
- Possible solution
-
11Setcover matrix for simple puzzle
- 16 columns
- 4 columns for the pieces
- 12 columns for the positions
- Rows
- 4 I placements
- I L - (1,1)(1,2)(1,3)(1,4)(2,1)(2,2
)(2,3)(2,4) (3,1)(3,2)(3,3)(3,4) - 1 0 0 0 1 0 0 0
1 0 0 0 1 0 0 0
- 1 0 0 0 0 1 0 0
0 1 0 0 0 1 0 0
- 1 0 0 0 0 0 1 0
0 0 1 0 0 0 1 0
- 1 0 0 0 0 0 0 1
0 0 0 1 0 0 0 1
12Setcover matrix cont
- I L - (1,1) (1,2)(1,3)(1,4)(2,1)
(2,2)(2,3)(2,4)(3,1)(3,2)(3,3)(3,4) - 0 1 0 0 1 0 0 0
1 1 0 0 0 0 0 0
- 0 1 0 0 0 1 0 0
0 1 1 0 0 0 0 0
- 0 1 0 0 0 0 1 0
0 0 1 1 0 0 0 0
- 0 1 0 0 0 0 0 0
1 0 0 0 1 1 0 0
- 0 1 0 0 0 0 0 0
0 1 0 0 0 1 1 0
- 0 1 0 0 0 0 0 0
0 0 1 0 0 0 1 1
-
13Setcover matrix cont
- 6 placements
- 6 placements
-
14Backtrack algorithm X sketch
- // A is the exact cover matrix
- if (empty(A)) return solution else
- choose a column c deterministically
- choose a row r with Ar,c 1 non-deterministical
ly - include r in solution
- for each column j with Ar,j 1
- delete column j from A
- for each row i with Ai,j 1 delete row i
from A -
- repeat recursively on reduced A
15X
- Non-deterministic choice of r means that the
algorithm can clone itself into independent
sub-algorithms ( parallelism ? ) - The sub-algorithms form a search tree
- Root original problem
- Level k node k subsets have been chosen and all
columns (elements) in the subsets and all
overlapping subsets (rows) have been removed - Any systematic rule for choosing columns will
find all solutions
16Choosing c
- One way
- Pick pentominoes in alphabetical order
- Not good F has 192 possible places, I then has
32, very big seach space 2x212 - Better way
- Choose lexicographically first uncovered
position, starting with (1,1), search space 107 - Even better way
- Scott realized that X has essentially three
possible places centered at (2,3), (2,4) and
(3,3). The rest leads to symmetrical solutions.
Search space 350,000 - Knuth has a more general solution pick column
with minimal number of 1-s
17Lets dance
- Represent each 1 in A by an object with 5 links
- L(x), R(x) (left right)
- U(x), D(x) (up down)
- C(x) column header
- Each row is a doubly (L,R) linked circular list
- Each column is a doubly linked (U,D) circular
list - Each column has a column header node c with
additional name N(c) and size S(c). - The column header form a circular row with a
header h - Each node points at its column header
18Our example
h
a 2
b 2
c 2
e 1
g 2
d 2
f 2
- a b c d e f g
- 1 0 0 1 0 1 1 0
- 2 1 0 0 1 0 0 1
- 3 0 1 1 0 0 1 0
- 4 1 0 0 1 0 0 0
- 5 0 1 0 0 0 0 1
19Search algorithm DLX
- search(int k) // search is called with k0 from
outside - if(Rhh) print O else // O is the set of
currently picked rows - choose column c
- cover column c // pick element c
- for each row r Dc, DDc while r!c
- Okr // pick a row with element c
- for each j Rr, RRr while j!r
- cover column Cj // all elements in
the row are now picked - search(k1)
- for each j Lr, LLr while j!r
- uncover column Cj
-
- uncover column c and return
-
20 cover column c
- // remove c from headers
- LRc Lc RLcRc
- // remove all rows in cs column
- for each row r Dc, DDc while r!c
- for each j Rr, RRr while j!r
- UDjUj DUjDj
- SCj--
-
21 uncover c
- // put rows back IN REVERSE ORDER
- // last out first back in
- for each row r Uc, UUc while r!c
- for each j Lr, LLr while j!r
- SCi
- UDjj DUjj
-
- // put header back
- LRcc RLcc
22 cover a remove header
h
a 2
b 2
c 2
e 1
g 2
d 2
f 2
b c d e f g 1 0 1 0 1 1 0
2 0 0 1 0 0 1 3 1 1 0 0 1 0
4 0 0 1 0 0 0 5 1 0 0 0 0 1
23 cover a remove row 2
h
a 2
b 2
c 2
e 1
g 1
d 1
f 2
b c d e f g 1 0 1 0 1 1 0
3 1 1 0 0 1 0 4 0 0 1 0 0 0
5 1 0 0 0 0 1
24 cover a remove row 2 and 4
b c d e f g 1 0 1 0 1 1 0
3 1 1 0 0 1 0 5 1 0 0 0 0
1 Notice the asymmetry in D2 (element D in row
2) versus D4 (element D in row 4). Putting
back D2 First would change D4s Links, but D4
is not in the set! This is why we need to
uncover in exactly the reverse order, so we
know that the element that is put back refers to
elements in the set
h
a 2
b 2
c 2
e 1
g 1
d 0
f 2
25Picking subset 2 leads to failure
a b c d e f g 1 0 0 1 0 1 1
0 2 1 0 0 1 0 0 1 3 0 1 1 0 0 1
0 4 1 0 0 1 0 0 0 5 0 1 0 0 0 0
1
b c e f 1 0 1 1 1 3
1 1 0 1
nothing left for e
select a cover a,d,g
select b cover b,c,f
26Picking subset 4 succeeds
c e f 1 1 1 1
- a b c d e f g
- 1 0 0 1 0 1 1 0
- 2 1 0 0 1 0 0 1
- 3 0 1 1 0 0 1 0
- 4 1 0 0 1 0 0 0
- 5 0 1 0 0 0 0 1
b c e f g 1 0 1 1 1 0
3 1 1 0 1 0 5 1 0 0 0 1
select c pick subset 1 empties A
select a pick subset 4 cover a,d
select b picking subset 3 fails again pick
subset 5 cover b,g
Exact cover subsets 4 a,d, 5 b,g and 1
c,e,f
27DLX in MapReduce
Using the search algorithm, go down k search
tree levels creating PREFIXES states at level
k subsets chosen on each path down to k. When
at level k, hand the prefix to a mapper. Each
mapper creates the reduced A according to the
prefix and then carries on the search algorithm
for that part of the search tree. The reducer
gathers solutions.