Source: Internet
Author: User

When I first came into contact with Dancing Links, I visited the csdn forum and found that someone was studying the sudoku program. I went in and looked at it because I had developed the sudoku game, some people found that using Dancing Links to solve Sudoku was the fastest, so I decided to learn about Dancing Links.

Dancing Links is a general optimization of a category of search problems. It has a miraculous effect on precision coverage and can also solve repeated coverage problems. The inventor of DancingLinks is DonKnuth (author of computer programming art)

2. Purpose

Precise coverage. For the next non-deterministic algorithm, since we did not think of A better name, we will call it the X algorithm, which can find all the solutions to the exact coverage problem defined by A specific 01 matrix. The X algorithm is a simple statement for implementing the experiment-an obvious method of error (indeed, in general, I don't think of other reasonable methods to do this ).

If A is empty, the problem is resolved and the problem is terminated successfully.

Otherwise, select a column c (it is determined that the column with at least 1 is selected ).

Select a row R to meet a [R, C] = 1 (which row is not sure ).

Include R into the decomposition.

For all J that meet the requirements of a [R, J] = 1,

Delete column J from matrix;

For all,

Delete row I from matrix.

Recursively repeats the preceding algorithm on the decreasing matrix.

3. Dance steps

A good way to implement the X algorithm is to use each 1 in matrix A with five domains: L [x], R [x], U [x], D [x], data Object of C [x] (dataobject) x. Each row of the matrix is a ring linked list connected by the L and R (left and right) fields. Each column of the Matrix is a chain table connected by the U and D (top and bottom) fields) two-way linked list. Each column linked list also contains a special data object called its header (list header ).

These headers are part of a large object called a column object (columnobject. Each column object y contains five fields of a common data object L [y], R [y], U [y], D [y], and C [y]. plus two domains, S [y] (size) and N [y] (name). Here the size is the number of 1 in a column, and the name is the symbol used to identify the output answer. The C field of each data object points to the column object of the corresponding column header.

The L and R of the header are connected to all columns to be overwritten. This ring linked list also contains a special column object called root, h, which is equivalent to the owner of all the active headers. In addition, it does not need U [h], D [h], C [h], S [h], and N [h.

We are looking for all the deterministic algorithms with precise coverage. Now we can take the following form of analysis and determination, that is, a recursive search (k) process. When it is called at the beginning, k = 0:

If R [h] = h, print the current solution (see below) and return

Otherwise, select a column Object c (see below ).

Overwrite column c (see below ).

For each r Branch D [c], D [D [c],..., When r! = C,

Set OK <-r;

For each j branch R [r], R [R [r],..., When j! = R,

Overwrite column j (see below );

Search (k + 1 );

Set r ready OK and c Branch C [r];

For each j ← L [r], L [L [r],..., When j! = R,

Unoverwrite column j (see below ).

Cancels column c overwrites (see below) and returns the result.

To select a column Object C, we can simply set C <-R [H]; this is the leftmost column that is not overwritten. Or if we want to minimize the branch factor, we can set S <-infinity. Next:

For each J branch R [H], R [R [H],..., When J! = H,

If s [J] <s sets C ← J and S ← s [H],

C is the column with the smallest ordinal number of 1 (if this method is not used to reduce the branch, the S domain will be useless ).

Overwrite column C is more interesting: delete column C from the header and remove all rows in the C linked list from other linked lists.

Set L [R [c] ← L [c] and R [L [c] ← R [c].

For each I Branch D [c], D [D [c],..., When I! = C,

For each j branch R [I], R [R {I],..., When j! = I,

Set U [D [j] ← U [j], D [U [j] ← D [j],

Set S [C [j] ← S [C [j]-1.

Operation (1), which I mentioned at the beginning of this article, is used to remove data objects in the horizontal and vertical directions.

Finally, we reach the cutting-edge of the entire algorithm, that is, the operation to restore the given column c. Here is the chain table dance process:

For each I ← U [c], U [U [c],..., When j! = I,

For each j ← L [I], L [L [I],…, When j! = I,

Set S [C [j] ← S [C [j] + 1,

Set U [D [j] ← j, D [U [j] ← j.

Set L [R [c] ← c and R [L [c] ← c.

Note that the restoration operation is in the opposite order of overwriting the operation. We use operation (2) to cancel the operation (1 ). (In fact, there is no need to strictly limit the "cancel before execution", because j can go through row I in any order; however, it is very important to remove the row from the bottom up, because we removed these rows from the top down. Similarly, it is also important for row r to remove columns from right to left, because we overwrite columns from left to right .)

The above part may be too professional and abstract, and you may not understand it. Anyway, I just haven't understood the above description, but I will give it through the analysis program code later, describes the dance process in detail, which is very intuitive and easy to understand. Therefore, you can skip the above steps without worrying about it.

There are two ways to implement the data structure such as Dancing Links. One is to implement the data structure using a two-way cross linked list, which is easier to understand. The other is to implement the data structure using several arrays, hard to understand

L [x] and R [x] represent the precursor and successor nodes of x respectively. Every programmer knows the following operations:

L [R [x] ← L [x], R [L [x] ← R [x] (1)

Is to delete x from the linked list; however, only a few programmers are aware of the following operations:

L [R [x] ← x, R [L [x] ← x (2)

The essence of dancingLinks is embodied in the second formula, because it facilitates and efficiently implements the restoration of the linked list.

In fact, arrays are used to simulate the structure of a two-way cross-link table. For ease of understanding, let's take an example directly.

For the following 01 matrix:

1 1
0 0

0 0
0 1

0 1
1 1

1 0
1 0

We describe its dancinglinks structure to the following four arrays: (Up, Down, Left, Right)

As follows:

Why are the four arrays of 13 in length?

Because the above 01 matrix has four columns and eight ones, we numbered the head of the head node as 0. The column numbers are 1, 2, 3, 4, respectively. The two 1 numbers in the first row are 5, 6, one 1 in the second row is 7, and the three 1 numbers in the third row are 8, 9, and 10. The fourth row contains two numbers: 1 and 12. Numbers are sequential from left to right.

In this way, the next node in the column 1 is 1 of 5, and 1 of 1 of 5 is 1 of 11, the left and right sides of 1 numbered 5 are 1 numbered 6.

For ease of understanding, see Figure 1.

Remove (5) L [R  = L ; R [L  = R ; L [R  = L ; R [L  = R ;

Remove (6) L [R  = L ; R [L  = R ; L [R  = L ; R [L  = R ;

Resume (5) L [R  = R [L  = 1 L [R  = R [L  = 11

Resume (6) L [R  = R [L  = 2 L [R  = R [L  = 8

Visible Arrays can be used to represent the structure of a two-way cross-link table.

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.