Shallow into dancing links x (dance chain algorithm)

Source: Internet
Author: User

Abastract: Use dancing links to solve accurate coverage problems, such as Sudoku, n Queens.

To learn the dacning links algorithm, first of all to understand the algorithm applicable to the problem, that is, accurate coverage problem, the following first understand the exact coverage problem.

Exact coverage issues for exact coverage issues

In a complete set X, the collection of several subsets is S, and the exact overlay (Exactcover) refers to the subset of S s*, which satisfies every element in x exactly once in s*.

Define a collection that meets the following criteria for an exact override:
Any two sets in s* have no intersection, that is, the elements in x appear at most once in s*
The complete set of collections in S* is X, which is the element in X that appears at least once in s*
One, that is, the elements in x appear exactly once in the s*. For example, ={n,o,e,p} is a subset of the collection x={1,2,3,4} and satisfies:
n={}
o={1,3}
e={2,4}
p={2,3}.
One of the subsets {o,e} is an exact overlay of x because o={1,3} and e={2,4} have a set that is exactly x={1,2,3,4}. Similarly, {n,o,e} is also an exact overlay of X. The empty set does not affect the conclusion. How to express the exact coverage problem

In general, we use a one-way relationship with a set S containing elements in s to represent an exact coverage problem. Commonly used in the following two ways:

    • Matrix notation

The containment relationship can be represented by a relational matrix: each row of the matrix represents a subset of S, and each column represents an element in X. The Matrix row and column intersection element is 1 to indicate that the corresponding element is in the corresponding set, not 0.

By means of this matrix representation, a set of rows that are accurately covered into a matrix is obtained, so that each column has only one 1. At the same time, this problem is one of the typical examples of accurate coverage.

The following table is an example:

S*={B,D,F} is an exact overlay.

    • Graph-Theory notation

  The exact coverage problem can be transformed into a binary graph, the left side is a set, the right side is an element, and the left-hand set has an inclusion relationship with the right element, and the edge is matched by the right side of each node in the left-hand node by solving its remaining edges.

Next is the dancing links x algorithm.

  Dancing Links X algorithm History

The x algorithm is an algorithm proposed by Gartner to solve the exact coverage problem, while the dancing links x algorithm is an efficient implementation of the X algorithm proposed by Donknuth (the author of Computer programming Art), which is based on the matrix notation described above.

Algorithmic Thinking

By using the matrix notation of the exact overlay problem, we know that dancing links x is used to solve a 01 matrix to select which rows can make each column of these rows have only one 1 (that is, each element has and appears only once in these subsets).

Regardless of his practical significance, what we need to do is to select a few of the 01 matrices to exercise in accordance with the above conditions.

It's easy to think of enumerations, and then the judges don't meet the criteria, but it's too time-consuming.

Dacing links x is an efficient algorithm for solving this kind of problem, and this algorithm is based on the data structure of the cross-cross-loop bidirectional chain (which sounds very tall, actually a bunch of linked lists).

For example, the following matrix

Contains such a collection (lines 1th, 4, 5)

How can a given matrix be used to find the corresponding set of rows? We use the Backtracking method

  

Let's assume that line 1th is selected as follows:

As shown in, the red line is the selected row with 3 1 in the row, the 3rd, 5, and 6 columns.

Since these 3 columns already contain 1, the three columns are marked down and the blue part of the figure. The blue section contains 3 1, respectively, in 2 rows, the 2 lines are marked with purple

By definition, 1 of the same column can have 1, so the purple two lines, and the red line of 1 conflict.

So in the next solution, the red part, the blue part, and the purple part are all useless, remove the parts and get a new matrix

  

rows correspond to lines 2nd, 4, 5 in Matrix 1, respectively

columns correspond to Columns 1th, 2, 4, 7 in Matrix 1, respectively

Then the problem is converted to a small point of the exact coverage problem

Select Line 1th in the new matrix, as shown in

or follow the previous steps to mark it. The red, blue, and purple sections are all removed, resulting in a new empty matrix, and a red line of 0 (with 0 indicating that the column has no 1 coverage). Description, line 1th selection is wrong

Then go back and select Line 2nd, as shown in

Follow the previous steps to mark it. Remove the red, blue, and purple sections to get a new matrix

  

Row corresponds to line 3rd in Matrix 2, line 5th in Matrix 1

columns correspond to columns 2nd and 4 in Matrix 2, columns 2nd and 7 in Matrix 1

Since the remaining matrices are only 1 rows and are all 1, selecting this line will solve the problem

So the solution to this problem is the 1th line in Matrix 1, the 2nd line in Matrix 2, and the 1th line in Matrix 3. 1th, 4, 5 lines in Matrix 1 ()

(examples quoted from http://www.cnblogs.com/grenet/p/3145800.html)

Shallow into dancing links x (dance chain algorithm)

Contact Us

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.