-
Notifications
You must be signed in to change notification settings - Fork 14
/
exact_cover.cpp
283 lines (219 loc) · 6.78 KB
/
exact_cover.cpp
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <bits/stdc++.h>
using namespace std;
#define MAX_ROW 100
#define MAX_COL 100
struct Node
{
struct Node *left;
struct Node *right;
struct Node *up;
struct Node *down;
struct Node *column;
int rowID;
int colID;
int nodeCount;
};
struct Node *header = new Node();
//matrix that contains nodes of linked mesh
struct Node Matrix[MAX_ROW][MAX_COL];
//binary problem matrix
bool ProbMat[MAX_ROW][MAX_COL];
//vector containing solutions
vector<struct Node*> solutions;
//number of rows and cols
int nRow = 0, nCol = 0;
//circular indices
int getRight(int i){return (i+1) % nCol;}
int getLeft(int i){return (i-1 < 0) ? nCol-1 : i-1;}
int getUp(int i){return (i-1 < 0) ? nRow : i-1;}
int getDown(int i){return (i+1) % (nRow + 1);}
Node* create_toroidal_matrix()
{
for (int i = 0; i <= nRow; ++i) //one extra row for list header nodes for each col
{
for (int j = 0; j < nCol; ++j)
{
//create a node if prob matrix has a 1
if (ProbMat[i][j])
{
int a, b;
//skip 0th row
if (i) Matrix[0][j].nodeCount += 1;
//add pointer to column header
Matrix[i][j].column = &Matrix[0][j];
//set row and col ID
Matrix[i][j].rowID = i;
Matrix[i][j].colID = j;
//link the node with neighbors
//left pointer
a = i; b = j;
do {b = getLeft(b);} while(!ProbMat[a][b] && b != j);
Matrix[i][j].left = &Matrix[i][b];
//right pointer
a = i; b = j;
do {b = getRight(b);} while(!ProbMat[a][b] && b != j);
Matrix[i][j].right = &Matrix[i][b];
//up pointer
a = i; b = j;
do {a = getUp(a);} while(!ProbMat[a][b] && a != i);
Matrix[i][j].up = &Matrix[a][j];
//down pointer
a = i; b = j;
do {a = getDown(a);} while(!ProbMat[a][b] && a != i);
Matrix[i][j].down = &Matrix[a][j];
}
}
}
//link header right pointer to column header of first column
header->right = &Matrix[0][0];
//link header left pointer to column header of last column
header->left = &Matrix[0][nCol-1];
Matrix[0][0].left = header;
Matrix[0][nCol-1].right = header;
return header;
}
//cover a given node
void cover(struct Node *targetNode)
{
struct Node *row, *rightNode;
//get the pointer to the header of column
//to which this node belongs
struct Node *colNode = targetNode->column;
//unlink column header from its neighbors
colNode->left->right = colNode->right;
colNode->right->left = colNode->left;
//move down the column and remove each row
//by traversing right
for (row = colNode->down; row != colNode; row = row->down)
{
for (rightNode = row->right; rightNode != row; rightNode = rightNode->right)
{
rightNode->up->down = rightNode->down;
rightNode->down->up = rightNode->up;
//decrement count in column header
Matrix[0][rightNode->colID].nodeCount -= 1;
}
}
}
//uncover a given node (if solution is invalid)
void uncover(struct Node *targetNode)
{
struct Node *rowNode, *leftNode;
//get the pointer to the header of column
//to which this node belongs
struct Node *colNode = targetNode->column;
//move down the column and link back each row
//by traversing left
for (rowNode = colNode->up; rowNode != colNode; rowNode = rowNode->up)
{
for (leftNode = rowNode->left; leftNode != rowNode; leftNode = leftNode->left)
{
leftNode->up->down = leftNode;
leftNode->down->up = leftNode;
//increment count in column header
Matrix[0][leftNode->colID].nodeCount += 1;
}
}
//link the column header to its neighbors
colNode->left->right = colNode;
colNode->right->left = colNode;
}
//traverse column headers right and return the column
//with minimum node count
Node *getMinColumn()
{
struct Node *h = header;
struct Node *min_col = h->right;
h = h->right->right;
do
{
if (h->nodeCount < min_col->nodeCount)
{
min_col = h;
}
h = h->right;
} while (h != header);
return min_col;
}
void printSolution()
{
cout << "solution: " << endl;
vector<struct Node*>::iterator i;
for (i = solutions.begin(); i != solutions.end(); ++i)
{
cout << (*i)->rowID << " ";
}
cout << endl;
}
//search for exact cover
void search(int k)
{
struct Node *rowNode;
struct Node *rightNode;
struct Node *leftNode;
struct Node *column;
//if no column left, we found a solution
if (header->right == header)
{
printSolution();
return;
}
//choose column deterministically
column = getMinColumn();
//cover chosen column
cover(column);
for (rowNode = column->down; rowNode != column; rowNode = rowNode->down)
{
solutions.push_back(rowNode);
for (rightNode = rowNode->right; rightNode != rowNode; rightNode = rightNode->right)
{
cover(rightNode);
}
//move to level k+1 (recursively)
search(k+1);
//if solution is not possible, backtrack (uncover)
//and remove selected row (set) from solution
solutions.pop_back();
column = rowNode->column;
for (leftNode = rowNode->left; leftNode != rowNode; leftNode = leftNode->left)
{
uncover(leftNode);
}
}
uncover(column);
}
int main()
{
//exact cover
//X = {1,2,3,4,5,6,7}
//set-1 = {1,4,7}
//set-2 = {1,4}
//set-3 = {4,5,7}
//set-4 = {3,5,6}
//set-5 = {2,3,6,7}
//set-6 = {2,7}
//set-7 = {1,4}
nRow = 7;
nCol = 7;
//init problem matrix
//row 0 corresponds to headers (init with 1)
for (int i = 0; i <= nRow; ++i)
{
for (int j = 0; j < nCol; ++j)
{
if (i == 0) ProbMat[i][j] = true;
else ProbMat[i][j] = false;
}
}
//fill in the problem matrix
ProbMat[1][0] = true; ProbMat[1][3] = true; ProbMat[1][6] = true;
ProbMat[2][0] = true; ProbMat[2][3] = true;
ProbMat[3][3] = true; ProbMat[3][4] = true; ProbMat[3][6] = true;
ProbMat[4][2] = true; ProbMat[4][4] = true; ProbMat[4][5] = true;
ProbMat[5][1] = true; ProbMat[5][2] = true; ProbMat[5][5] = true; ProbMat[5][6] = true;
ProbMat[6][1] = true; ProbMat[6][6] = true;
ProbMat[7][0] = true; ProbMat[7][3] = true;
create_toroidal_matrix();
search(0);
return 0;
}