- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDancingLinks.java
More file actions
Latest commit
170 lines (127 loc) · 4.71 KB
/
Copy pathDancingLinks.java
File metadata and controls
170 lines (127 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Data structures implemented from pg. 5 of Dancing Links paper
// Ok, just so it makes sense to me:
// Data Object = a dancing link. It has 2 circularly linked lists and a list header.
// Column Object = a column (a set of 1s that we may want to have in our exact cover). It contains DOs and has a name and # of DOs.
publicclassDancingLinks {
classDataObject {
DataObjectleft, right, up, down;
ColumnObjectC; // Knuth calls this property a "list header" in his paper but I am conflating the two objects into one for simplicity
DataObject() {
left = right = up = down = this;
}
DataObject(ColumnObjectinitC) {
this();
C = initC;
// TODO: Stick on bottom of col by default?
}
DataObjectappendToRow(DataObjectnewDO) { // Append new DO to the right and return it
this.right.left = newDO;
newDO.left = this;
newDO.right = this.right;
this.right = newDO;
returnnewDO;
}
DataObjectappendToColumn(DataObjectnewDO) { // Append new DO below and return it
this.down.up = newDO;
newDO.up = this;
newDO.down = this.down;
this.down = newDO;
returnnewDO;
}
voidunlinkFromRow() {
this.left.right = this.right;
this.right.left = this.left;
}
voidrelinkToRow() {
this.left.right = this.right.left = this;
}
voidunlinkFromColumn() {
this.up.down = this.down;
this.down.up = this.up;
this.C.size--;
}
voidrelinkToColumn() {
this.up.down = this.down.up = this;
this.C.size++;
}
}
classColumnObjectextendsDataObject {
// DataObject left, right, up, down;
// ColumnObject C;
Stringname;
intsize; // Number of 1s in the column
ColumnObject(StringinitName) {
super(); // Inherit left,right,up,down,C from dataobject
C = this;
name = initName;
size = 0;
}
voidcover() {
this.unlinkFromRow();
for (DancingNodei = this.down; i != this.C; i = i.down) {
for (DancingNodej = i.right; j != i; j = j.right) {
j.unlinkFromColumn();
}
}
}
voiduncover() {
for (DancingNodei = this.up; i != this.C; i = i.up) {
for (DancingNodej = i.left; j != i; j = j.left) {
j.relinkToColumn();
}
}
this.relinkToRow();
}
}
privateColumnObjectroot; // Special CO, labeled "h" in the paper
privateList<DancingNode> solutions;
privateintnumSolutionsFound = 0;
privatevoidsearch(intK) { // Deterministic algorithm to find all exact covers
if (root.right == root) {
numSolutionsFound++;
// TODO: Print the current solution
return;
}
ColumnObjectc = getSmallestColumnObject();
c.cover();
for (DataObjectr = c.down; r != c; r = r.down) {
solutions.add(r);
for (DataObjectj = r.right; j != r; j = j.right) {
j.C.cover();
}
search(K + 1);
r = solutions.remove(solutions.size() - 1);
c = r.C;
for (DataObjectj = r.left; j != r; j = j.left) {
j.C.uncover();
}
}
c.uncover();
return;
// Pseudocode from the paper:
// If R[h] = h, print the current solution (see below) and return.
// Otherwise choose a column object c (see below).
// Cover column c (see below).
// For each r ← D[c], D[D[c]], ... , while r != c,
// set Ok ← r;
// for each j ← R[r], R[R[r]], ... , while j != r,
// cover column j (see below);
// search(k + 1);
// set r ← Ok and c ← C[r];
// for each j ← L[r], L[L[r]], ... , while j != r,
// uncover column j (see below).
// Uncover column c (see below) and return.
}
privateColumnObjectgetSmallestColumnObject() {
intmin = Integer.MAX_VALUE;
ColumnObjectsmallestCO = null;
// Search for the min size CO by iterating through all COs by moving right until we end up back at the header
for (ColumnObjectcol = (ColumnObject) header.right; col != header; col = (ColumnObject) col.right) {
if (col.size < min) {
min = col.size;
smallestCO = col;
}
}
returnsmallestCO;
}
}