-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlx.h
297 lines (226 loc) · 6.66 KB
/
dlx.h
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Dancing Links solver for the exact cover problem.
// Finds subset of rows in a binary matrix such that each row is
// disjoint, and whose union covers each column.
#ifndef DLX_H
#define DLX_H
#include <algorithm>
#include <iostream>
#include <vector>
#include "memory_pool.h"
namespace Dlx {
using Index=size_t;
using BinaryMatrix=std::vector<std::vector<bool>>;
class Node
{
public:
Node* left;
Node* right;
Node* up;
Node* down;
Node* column;
size_t size; // column size
Index row_idx; // useful for generating solution
// ctor
Node() :
left(this), right(this), up(this), down(this), column(this),
size(0), row_idx(-1)
{}
};
class DancingLinks
{
public:
using Row=size_t;
using Solution=std::vector<Row>;
public:
// ctor
DancingLinks(const BinaryMatrix& bin_mat)
{
if (bin_mat.size() == 0 or bin_mat[0].size() == 0) {
// \todo throw error
return;
}
size_t nrows = bin_mat.size();
size_t ncols = bin_mat[0].size();
// Determine how many nodes (nonzero entries in bin_mat)
// Concurrently, determine which columns each row covers
size_t node_count = 0;
m_rows.reserve(nrows);
for (size_t i = 0; i < nrows; ++i) {
std::vector<size_t> row;
for (size_t j = 0; j < ncols; ++j) {
if (bin_mat[i][j]) {
++node_count;
row.push_back(j);
}
}
m_rows.push_back(std::move(row));
}
// Initialize memory pool and root node
m_pool.Resize(node_count + 1 /* for root */ + ncols);
m_root = m_pool.New();
m_root->size = (size_t)(-1);
// For keeping track of last node in column
std::vector<Node*> prev_row(ncols);
// Initialize column nodes
Node* last_node = m_root;
for (size_t j = 0; j < ncols; ++j) {
Node* column_node = m_pool.New();
prev_row[j] = column_node;
last_node->right = column_node;
column_node->left = last_node;
last_node = column_node;
}
last_node->right = m_root;
m_root->left = last_node;
// Construct a node for each nonzero element in binary matrix
for (size_t i = 0; i < nrows; ++i) {
Node* first_in_row = nullptr;
Node* last_in_row = nullptr;
for (size_t j : m_rows[i]) {
Node* cur_node = m_pool.New();
cur_node->row_idx = i;
// Up
Node* up_node = prev_row[j];
prev_row[j] = cur_node;
cur_node->up = up_node;
up_node->down = cur_node;
// Column
Node* column_node = up_node->column;
cur_node->column = column_node;
++column_node->size;
// Left (if not the first seen in row thus far)
if (last_in_row == nullptr) {
first_in_row = cur_node;
}
else {
cur_node->left = last_in_row;
last_in_row->right = cur_node;
}
last_in_row = cur_node;
}
if (first_in_row != nullptr) {
first_in_row->left = last_in_row;
last_in_row->right = first_in_row;
}
}
// Connect last nodes in columns to respective columns
for (size_t j = 0; j < ncols; ++j) {
Node* column_node = prev_row[j]->column;
prev_row[j]->down = column_node;
column_node->up = prev_row[j];
}
}
// Find all solutions and store in memory
void Search()
{
Search_(0);
}
// Get all solutions
const std::vector<Solution>& GetSolutions() const { return m_solutions;}
// Return number of solutions
const size_t SolutionCount() const { return m_solutions.size(); }
const std::vector<size_t>& RowToColumns(size_t r) const { return m_rows.at(r);}
const Solution& GetSolution(size_t idx) const { return m_solutions.at(idx);}
// Helper function for converting a solution (subset of rows) to
// whatever the user defines the covered columns to be
template<class T>
std::vector<std::vector<T>> DecodeSolution(const std::vector<T>& columns,
const Solution& solution) const
{
std::vector<std::vector<T>> decoded_solution;
for (size_t row : solution) {
std::vector<T> decoded_row;
const auto& cols = RowToColumns(row);
for (size_t col : cols) {
decoded_row.push_back(columns.at(col));
}
decoded_solution.push_back(std::move(decoded_row));
}
return decoded_solution;
}
private:
/*
dlx specific functions
*/
// cover column and remove conflicting rows
void Cover(Node* col_node)
{
col_node->right->left = col_node->left;
col_node->left->right = col_node->right;
for (Node* i_node = col_node->down; i_node != col_node; i_node = i_node->down) {
for (Node* j_node = i_node->right; j_node != i_node; j_node = j_node->right) {
j_node->down->up = j_node->up;
j_node->up->down = j_node->down;
--col_node->size;
}
}
}
// undo effects of Cover()
void Uncover(Node* col_node)
{
for (Node* i_node = col_node->up; i_node != col_node; i_node = i_node->up) {
for (Node* j_node = i_node->left; j_node != i_node; j_node = j_node->left) {
++col_node->size;
j_node->down->up = j_node;
j_node->up->down = j_node;
}
}
col_node->right->left = col_node;
col_node->left->right = col_node;
}
// heuristic for choosing column
Node* ChooseColumn()
{
Node* best_node = m_root;
for (Node* col_node = m_root->right; col_node != m_root; col_node = col_node->right) {
if (col_node->size < best_node->size) {
best_node = col_node;
}
}
return best_node;
}
// Recursively search for solutions
void Search_(size_t k)
{
Node* col_node = ChooseColumn();
// Potential solution found
if (col_node == m_root) {
Solution solution;
std::transform(m_cur_solution.begin(), m_cur_solution.begin() + k,
std::back_inserter(solution),
[](Node* r) -> Index { return r->row_idx;});
if (solution.size()) {
m_solutions.push_back(std::move(solution));
}
return;
}
Cover(col_node);
for (Node* r_node = col_node->down; r_node != col_node; r_node = r_node->down) {
if (k >= m_cur_solution.size()) {
m_cur_solution.resize(k * 2 + 1);
}
m_cur_solution[k] = r_node;
for (Node* c_node = r_node->right; c_node != r_node; c_node = c_node->right) {
Cover(c_node->column);
}
Search_(k+1);
// \todo These two lines necessary?
r_node = m_cur_solution[k];
col_node = r_node->column;
for (Node* c_node = r_node->left; c_node != r_node; c_node = c_node->left) {
Uncover(c_node->column);
}
}
Uncover(col_node);
}
/*
Member variables
*/
MemoryPool<Node> m_pool; // For managing linked list memory
Node* m_root;
std::vector<Solution> m_solutions; // soltuions
std::vector<Node*> m_cur_solution; // for building current solution
std::vector<std::vector<size_t>> m_rows; // map from row to covered columns
};
} // namespace
#endif // DLX_H