-
Notifications
You must be signed in to change notification settings - Fork 0
/
DancingLinks.java
213 lines (167 loc) · 5.57 KB
/
DancingLinks.java
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import java.util.*;
public class DancingLinks {
int [][] M;//problem matrix
ArrayList<Set<Integer>> set_of_row=new ArrayList<Set<Integer>>();//stores set from C corresponding to each row
int nb_rows;
int nb_columns;
//we don't need to implement the column_object and data_objects separately
//a column_object is simply a data_object with the fields "size" and "col_id" defined
data_object master_header = new data_object();
data_object[][] linked_matrix;//the famous dancing links structure
public DancingLinks(int[][] M) {//constructor which builds the linked_matrix structure
this.M=M;
nb_rows=M.length;
nb_columns=M[0].length;
for (int i=0;i<nb_rows;i++) {//set up set_of_row
Set<Integer> S = new HashSet<Integer>();
for (int j=0;j<nb_columns;j++) {
if (M[i][j]==1) {
S.add(j+1);//we assume we are solving an exact cover problem for $X={1,....,n}$ and $C$ a collection of subsets of X
}
}
set_of_row.add(S);
}
linked_matrix = new data_object[nb_rows+1][nb_columns];//last row will be the headers (column_objects)
for (int i=0; i<=nb_rows;i++) {//initialize the matrix objects one-by-one
for (int j=0;j<nb_columns;j++) {
linked_matrix[i][j]=new data_object();
}
}
for (int j=0; j<nb_columns;j++) {//setting up the column headers
linked_matrix[nb_rows][j].col_id=j+1;//number of column
linked_matrix[nb_rows][j].U=linked_matrix[nb_rows][j];//column headers initially linked to themselves in U,D directions
linked_matrix[nb_rows][j].D=linked_matrix[nb_rows][j];
linked_matrix[nb_rows][j].R=linked_matrix[nb_rows][(j+1)%nb_columns];//cyclic structure in R,L directions
linked_matrix[nb_rows][j].L=linked_matrix[nb_rows][mod((j-1),nb_columns)];
}
master_header.R=linked_matrix[nb_rows][0];//setting up the master_header
linked_matrix[nb_rows][0].L=master_header;
master_header.L=linked_matrix[nb_rows][nb_columns-1];
linked_matrix[nb_rows][nb_columns-1].R=master_header;
for (int i=0; i<nb_rows;i++) {//create data_objects corresponding to elements of M
for (int j=0;j<nb_columns;j++) {
if (M[i][j]==1) {//if there is a 1, we define the data_object associated
linked_matrix[i][j].row_id=i;
linked_matrix[i][j].C=linked_matrix[nb_rows][j];
linked_matrix[nb_rows][j].size+=1;
//-----------------------RIGHT
for (int c=j+1; c<=j+nb_columns; c++) {//find first one in M to the right of this square
if (M[i][c%nb_columns]==1) {
linked_matrix[i][j].R=linked_matrix[i][c%nb_columns];
break;
}
}
//---------------------LEFT------------------------
for (int c=j-1; c>=j-nb_columns; c--) {//find first one in M to the left of this square
if (M[i][mod(c,nb_columns)]==1) {
linked_matrix[i][j].L=linked_matrix[i][mod(c,nb_columns)];
break;
}
}
//-------------------DOWN---------------------------
int r=i+1;
while (r<nb_rows) {//find first one below this square
if (M[r][j]==1) {
linked_matrix[i][j].D=linked_matrix[r][j];
break;
}
r++;
}
if (r==nb_rows) {
linked_matrix[i][j].D=linked_matrix[nb_rows][j];
linked_matrix[nb_rows][j].U=linked_matrix[i][j];
}
//-----------------UP--------------------------------
r=i-1;
while (r>=0) {//find first one below this square
if (M[r][j]==1) {
linked_matrix[i][j].U=linked_matrix[r][j];
break;
}
r--;
}
if (r==-1) {
linked_matrix[i][j].U=linked_matrix[nb_rows][j];
linked_matrix[nb_rows][j].D=linked_matrix[i][j];
}
}
}
}
}
public static void coverColumn(data_object x) {
x.R.L = x.L;
x.L.R = x.R;
data_object t=x.D;
while (!t.equals(x)) {
data_object y=t.R;
while (!y.equals(t)) {
y.D.U = y.U;
y.U.D = y.D;
y.C.size-= 1;
y=y.R;
}
t = t.D;
}
}
public static void uncoverColumn(data_object x) {
x.R.L = x;
x.L.R = x;
data_object t=x.U;
while (!t.equals(x)) {
data_object y=t.L;
while (!y.equals(t)){
y.D.U = y;
y.U.D = y;
y.C.size+= 1;
y=y.L;
}
t=t.U;
}
}
public Set<Set<data_object>> exactCover(data_object master_header){
Set<Set<data_object>> exact_cover=new HashSet<Set<data_object>>();
if (master_header.R.equals(master_header)) {
Set<data_object> empty=new HashSet<data_object>();
exact_cover.add(empty);
return exact_cover;
}
//Choose a column x with x.S minimal;
//----------------------------------------------
data_object x=master_header.R;
int min_size=Integer.MAX_VALUE;
data_object itr=master_header.R;
while (!(itr.col_id==master_header.col_id)) {
if(itr.size<min_size) {
x=itr;
min_size=x.size;
}
itr=itr.R;
}
//---------------------------------------------
coverColumn(x);
data_object t=x.U;
while (! t.equals(x)) {
data_object y=t.L;
while (!y.equals(t)) {
coverColumn(y.C);
y = y.L;
}
for (Set<data_object> P: exactCover(master_header)) {
P.add(t);
exact_cover.add(P);
}
data_object z=t.R;
while(!z.equals(t)) {
uncoverColumn(z.C);
z=z.R;
}
t=t.U;
}
uncoverColumn(x);
return exact_cover;
}
private int mod(int x,int n) {//extends x%n for x negative
int r = (x % n);
return ((r >> 31) & n) + r;
}
}