Algorithm X

Algorithm X是一种基于递归和回溯的算法,用于从01矩阵中选择行,确保新矩阵每列只有一个1。适用场景包括数独等精确覆盖问题。算法步骤包括:矩阵为空则成功结束;选择1个数最少的列;选择该列中有1的行并加入解决方案;删除与选中行有相同1的其他行和列,然后对剩余矩阵递归执行该过程。通过Knuth的dance link (dlx)实现,该算法能解决如给定集合覆盖问题的例子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Algorithm X 是一种递归的,深度优先的回溯的算法,算法的目的是将一个仅由0 1构成的矩阵提取其中的几行,使得这几行构成的新矩阵每列包含且仅包含一个1,适用于精确覆盖问题,如数独,Knuth推荐dance link (dlx)的实现方式

算法具体步骤

  1. If the matrix A is empty, the problem is solved; terminate successfully.(矩阵为空递归结束)
  2. Otherwise choose a column c (deterministically).找出1的个数最小的列
  3. Choose a row r such that Arc = 1 (nondeterministically).找出该列中值为1的位置所在的行
  4. Include row r in the partial solution.提取该行r
  5. For each column j such that Arj = 1,
    for each row  i such that  A ij = 1,
    delete row  i from matrix  A;与行r值为1的同在一列的行被删除(保证每一列仅有一个1)
    delete column  j from matrix  A.(r中值为1的列被删除,否则下一次递归时将会因为此列全为0而中断)
  6. Repeat this algorithm recursively on the reduced matrix A
若递归时某一列全为零说明这种情况无解

算法例子

For example, consider the exact cover problem specified by the universe U = {1, 2, 3, 4, 5, 6, 7} and the collection of sets \mathcal{S} = {AB,CDEF}, where:

  • A = {1, 4, 7};
  • B = {1, 4};
  • C = {4, 5, 7};
  • D = {3, 5, 6};
  • E = {2, 3, 6, 7}; and
  • F = {2, 7}.

This problem is represented by the matrix:

 1234567
A1001001
B1001000
C0001101
D0010110
E0110011
F0100001

Algorithm X with Knuth's suggested heuristic for selecting columns solves this problem as follows:

Level 0

Step 1—The matrix is not empty, so the algorithm proceeds.

Step 2—The lowest number of 1s in any column is two. Column 1 is the first column with two 1s and thus is selected (deterministically):

 1234567
A1001001
B1001000
C0001101
D0010110
E0110011
F0100001

Step 3—Rows A and B each have a 1 in column 1 and thus are selected (nondeterministically).

The algorithm moves to the first branch at level 1…

Level 1: Select Row A
Step 4—Row  A is included in the partial solution.
Step 5—Row  A has a 1 in columns 1, 4, and 7:
 1234567
A1001001
B1001000
C0001101
D0010110
E0110011
F0100001
Column 1 has a 1 in rows  A and  B; column 4 has a 1 in rows  AB, and  C; and column 7 has a 1 in rows  ACE, and  F. Thus rows  AB, CE, and  F are to be removed and columns 1, 4 and 7 are to be removed:
 1234567
A1001001
B1001000
C0001101
D0010110
E0110011
F0100001
Row  D remains and columns 2, 3, 5, and 6 remain:
 2356
D0111
Step 1—The matrix is not empty, so the algorithm proceeds.
Step 2—The lowest number of 1s in any column is zero and column 2 is the first column with zero 1s:
 2356
D0111
Thus this branch of the algorithm terminates unsuccessfully.
The algorithm moves to the next branch at level 1…
Level 1: Select Row B
Step 4—Row  B is included in the partial solution.
Row  B has a 1 in columns 1 and 4:
 1234567
A1001001
B1001000
C0001101
D0010110
E0110011
F0100001
Column 1 has a 1 in rows  A and  B; and column 4 has a 1 in rows  AB, and  C. Thus rows  AB, and  C are to be removed and columns 1 and 4 are to be removed:
 1234567
A1001001
B1001000
C0001101
D0010110
E0110011
F0100001
Rows  DE, and  F remain and columns 2, 3, 5, 6, and 7 remain:
 23567
D01110
E11011
F10001
Step 1—The matrix is not empty, so the algorithm proceeds.
Step 2—The lowest number of 1s in any column is one. Column 5 is the first column with one 1 and thus is selected (deterministically):
 23567
D01110
E11011
F10001
Step 3—Row  D has a 1 in column 5 and thus is selected (nondeterministically).
The algorithm moves to the first branch at level 2…
Level 2: Select Row D
Step 4—Row  D is included in the partial solution.
Step 5—Row  D has a 1 in columns 3, 5, and 6:
 23567
D01110
E11011
F10001
Column 3 has a 1 in rows  D and  E; column 5 has a 1 in row  D; and column 6 has a 1 in rows  D and  E. Thus rows  D and  E are to be removed and columns 3, 5, and 6 are to be removed:
 23567
D01110
E11011
F10001
Row  F remains and columns 2 and 7 remain:
 27
F11
Step 1—The matrix is not empty, so the algorithm proceeds.
Step 2—The lowest number of 1s in any column is one. Column 2 is the first column with one 1 and thus is selected (deterministically).
Row  F has a 1 in column 2 and thus is selected (nondeterministically).
The algorithm moves to the first branch at level 3…
Level 3: Select Row F
Step 4—Row  F is included in the partial solution.
Row  F has a 1 in columns 2 and 7:
 27
F11
Column 2 has a 1 in row  F; and column 7 has a 1 in row  F. Thus row  F is to be removed and columns 2 and 7 are to be removed:
 27
F11
Step 1—The matrix is empty, thus this branch of the algorithm terminates successfully.
As rows  BD, and  F are selected, the final solution is:
 1234567
B1001000
D0010110
F0100001
In other words, the subcollection { BDF} is an exact cover, since every element is contained in exactly one of the sets  B = {1, 4},  D = {3, 5, 6}, or  F = {2, 7}.
There are no more selected rows at level 3, thus the algorithm moves to the next branch at level 2…
There are no more selected rows at level 2, thus the algorithm moves to the next branch at level 1…
There are no more selected rows at level 1, thus the algorithm moves to the next branch at level 0…

There are no branches at level 0, thus the algorithm terminates.

In summary, the algorithm determines there is only one exact cover: \mathcal{S}^* = {BDF}.

from wiki


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值