-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmunkres.hpp
363 lines (320 loc) · 11.7 KB
/
munkres.hpp
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#pragma once
// @see Tutorial on Implementation of Munkres' Assignment Algorithm
// Robert Pilgram, Murray State University
#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <limits>
#include <vector>
#include <algorithm>
namespace detail
{
template<typename T> struct MunkresData
{
static constexpr T zero = T(0);
enum MunkresState : uint8_t { NONE, STAR, PRIME };
using edge = std::pair<unsigned, unsigned>;
unsigned n_rows_ = 0;
unsigned n_cols_ = 0;
unsigned side_ = 0;
std::vector<T> data;
std::vector<MunkresState> marks;
std::vector<bool> row_mask;
std::vector<bool> col_mask;
// ------------------------------------------------------------- Construction
//
MunkresData(const unsigned n_rows,
const unsigned n_cols,
std::function<T(unsigned r, unsigned c)> edge_cost) noexcept
: n_rows_(n_rows)
, n_cols_(n_cols)
, side_(std::max(n_rows, n_cols))
, data(side_ * side_)
, marks(side_ * side_)
, row_mask(side_)
, col_mask(side_)
{
assert(n_rows_ > 0);
assert(n_cols_ > 0);
// Populate weight matrix... keep track of maximum for next step
T max_val = std::numeric_limits<T>::lowest();
for(auto r = 0u; r < n_rows; ++r)
for(auto c = 0u; c < n_cols; ++c) {
auto val = edge_cost(r, c);
C(r, c) = val;
if(max_val < val) max_val = val;
}
// The weight matrix is always square... fill in the empty
// spots with 'max-val'
for(auto r = n_rows; r < side(); ++r)
for(auto c = n_cols; c < side(); ++c) C(r, c) = max_val;
for(auto c = n_cols; c < side(); ++c)
for(auto r = n_rows; r < side(); ++r) C(r, c) = max_val;
// Subtract the minimum from every row and column, which
// ensures that every row and column has a '0'
subtract_min_from_all_rows_cols();
// Set up marks
std::fill(begin(marks), end(marks), MunkresState::NONE);
std::fill(begin(row_mask), end(row_mask), false);
std::fill(begin(col_mask), end(col_mask), false);
{ // ensure evey element is finite
auto ii = std::find_if(
cbegin(data), cend(data), [](auto x) { return !std::isfinite(x); });
if(ii != cend(data)) {
std::cerr << "precondition failed: non-finite edge cost" << std::endl;
assert(false);
}
}
}
// ---------------------------------------------------------- Getters/Setters
//
// Costs
T& C(int r, int c) noexcept { return data[r * side_ + c]; }
const T& C(int r, int c) const noexcept { return data[r * side_ + c]; }
// Marks
MunkresState& M(int r, int c) noexcept { return marks[r * side_ + c]; }
const MunkresState& M(int r, int c) const noexcept { return marks[r * side_ + c]; }
void cover_row(int r) noexcept { row_mask[r] = true; }
void cover_col(int c) noexcept { col_mask[c] = true; }
void uncover_row(int r) noexcept { row_mask[r] = false; }
void uncover_col(int c) noexcept { col_mask[c] = false; }
bool is_row_covered(int r) const noexcept { return row_mask[r]; }
bool is_col_covered(int c) const noexcept { return col_mask[c]; }
unsigned original_cols() const noexcept { return n_cols_; }
unsigned original_rows() const noexcept { return n_rows_; }
unsigned side() const noexcept { return side_; }
// ------------------------------------------ subtract min from all rows cols
// This prepares the data for the algorithm
void subtract_min_from_all_rows_cols()
{
auto min_val_in_row = [&](unsigned r) -> T {
auto min_val = C(r, 0);
for(auto c = 1u; c < side_; ++c)
if(C(r, c) < min_val) min_val = C(r, c);
return min_val;
};
auto min_val_in_col = [&](unsigned c) -> T {
auto min_val = C(0, c);
for(auto r = 1u; r < side_; ++r)
if(C(r, c) < min_val) min_val = C(r, c);
return min_val;
};
// Minimize each row
for(auto r = 0u; r < side_; ++r) {
const auto min_val = min_val_in_row(r);
for(auto c = 0u; c < side_; ++c) C(r, c) -= min_val;
}
// Minimize each col
for(auto c = 0u; c < side_; ++c) {
const auto min_val = min_val_in_col(c);
for(auto r = 0u; r < side_; ++r) C(r, c) -= min_val;
}
}
// ------------------------------------------------------------------- Step 1
// Iterate over each element...
// If it's 0, and there's no other zero in row/col, then STAR
int step1() noexcept
{
std::vector<bool> r_mask(side(), false);
std::vector<bool> c_mask(side(), false);
for(auto r = 0u; r < side(); ++r) {
if(r_mask[r]) continue;
for(auto c = 0u; c < side(); ++c) {
if(r_mask[r] || c_mask[c]) continue;
if(C(r, c) == zero) {
M(r, c) = STAR;
r_mask[r] = true;
c_mask[c] = true;
}
}
}
return 2;
}
// ------------------------------------------------------------------- Step 2
// Cover each column containing a STAR
int step2() noexcept
{
auto counter = 0u;
for(auto c = 0u; c < side(); ++c) assert(!is_col_covered(c));
for(auto r = 0u; r < side(); ++r) {
for(auto c = 0u; c < side(); ++c) {
if(is_col_covered(c)) continue;
if(M(r, c) == STAR) {
cover_col(c);
counter++;
}
}
}
// A complete matching
if(counter >= side()) return 0;
return 3;
}
// ------------------------------------------------------------------- Step 3
// Find a uncovered zero and PRIME it.
// Eventually get to a state where the PRIMEd row contains no STAR zeros
std::tuple<int, unsigned, unsigned> step3() noexcept
{
auto find_uncovered_row_col = [&](unsigned& r, unsigned& c) -> bool {
for(r = 0; r < side_; ++r)
if(!is_row_covered(r))
for(c = 0; c < side_; ++c)
if(!is_col_covered(c))
if(C(r, c) == zero) return true;
return false;
};
// Find an uncovered zero, and mark it PRIME
unsigned saved_row = 0, saved_col = 0;
if(find_uncovered_row_col(saved_row, saved_col))
M(saved_row, saved_col) = PRIME;
else
return std::tuple<int, unsigned, unsigned>{5, saved_row, saved_col}; // all zeros covered
// If there's a STAR in the PRIMEd row, then:
for(auto c = 0u; c < side(); ++c) {
if(M(saved_row, c) == STAR) {
cover_row(saved_row); // cover that row
uncover_col(c); // uncover the column
return std::tuple<int, unsigned, unsigned>{3, saved_row, saved_col}; // and repeat this step
}
}
// There's no STAR in the PRIMEd row, onto "augmenting path"
return std::tuple<int, unsigned, unsigned>{4, saved_row, saved_col};
}
// ------------------------------------------------------------------- Step 4
// Augmenting path algorithm
int step4(const unsigned saved_row, const unsigned saved_col) noexcept
{
auto find_star_in_col = [&](const unsigned c) -> int {
for(auto r = 0u; r < side(); ++r)
if(M(r, c) == STAR) return r;
return -1; // row not found
};
auto find_prime_in_row = [&](const unsigned r) -> int {
for(auto c = 0u; c < side(); ++c)
if(M(r, c) == PRIME) return c;
assert(false); // we should ALWAYS find this column
return -1; // col not found
};
auto make_path = [&](const edge e0) {
std::vector<edge> seq;
seq.reserve(side());
seq.push_back(e0);
int r = -1, c = -1;
while(true) {
c = seq.back().second;
r = find_star_in_col(c); // STARed zero in column of PRIMEd back()
if(r >= 0)
seq.push_back({r, c}); // Push a STAR edge
else // If it doesn't exist, then the path is done
break;
c = find_prime_in_row(r);
seq.push_back({r, c}); // Push a PRIME edge
}
return seq;
};
auto augment_path = [&](const std::vector<edge>& seq) {
// For all edges in sequence:
// 1. Erase all STARs
// 2. And convert all PRIMEs to STARs
for(const auto& e : seq) {
if(M(e.first, e.second) == STAR)
M(e.first, e.second) = NONE;
else if(M(e.first, e.second) == PRIME)
M(e.first, e.second) = STAR;
}
};
auto erase_primes = [&]() {
for(auto r = 0u; r < side(); ++r)
for(auto c = 0u; c < side(); ++c)
if(M(r, c) == PRIME) M(r, c) = NONE;
};
auto clear_covers = [&]() {
std::fill(begin(row_mask), end(row_mask), false);
std::fill(begin(col_mask), end(col_mask), false);
};
const edge e0{saved_row, saved_col}; // Uncovered primed zero from step3
auto seq = make_path(e0);
augment_path(seq);
erase_primes();
clear_covers();
return 2;
}
// ------------------------------------------------------------------- Step 5
// Find the smallest uncovered value, and:
// 1. Add it to every covered row
// 2. Subtract it from every uncovered col
int step5() noexcept
{
auto find_min_uncovered_value = [&]() {
auto minval = std::numeric_limits<T>::max();
for(auto r = 0u; r < side(); ++r) {
if(is_row_covered(r)) continue;
for(auto c = 0u; c < side(); c++) {
if(is_col_covered(c)) continue;
if(C(r, c) < minval) minval = C(r, c);
}
}
return minval;
};
const auto minval = find_min_uncovered_value();
for(auto r = 0u; r < side(); ++r) {
for(auto c = 0u; c < side(); c++) {
if(is_row_covered(r)) C(r, c) += minval; // (1) add minval
if(!is_col_covered(c)) C(r, c) -= minval; // (2) subtract minval
}
}
return 3;
}
// -------------------------------------------------------------------- Solve
//
std::vector<edge> solve() noexcept
{
// The Munkres Algorithm is described as a state machine
int step = 1;
unsigned saved_row = 0, saved_col = 0;
while(step) {
switch(step) {
case 1:
step = step1(); // => [2]
break;
case 2:
step = step2(); // => [0, 3]
break;
case 3:
std::tie(step, saved_row, saved_col) = step3(); // => [3, 4, 5]
break;
case 4:
step = step4(saved_row, saved_col); // => [2]
break;
case 5:
step = step5(); // => [3]
break;
}
}
// Collate the results
std::vector<edge> out;
out.reserve(side());
for(auto r = 0u; r < original_rows(); ++r)
for(auto c = 0u; c < original_cols(); ++c)
if(M(r, c) == STAR) out.push_back({r, c});
return out;
}
};
} // namespace detail
//
// @param n_lhs_verts Number of left-hand-side vertices (in bipartite graph)
// @param n_rhs_verts Number of right-hand-side verices (ibid)
// @param cost Cost between vertices 'l' and 'r'. Use of function to abstract
// away storage details of input graph.
// @see example.cpp
//
template<typename T>
std::vector<std::pair<unsigned, unsigned>> inline munkres_algorithm(
const unsigned n_lhs_verts,
const unsigned n_rhs_verts,
std::function<T(unsigned l, unsigned r)> cost) noexcept
{
detail::MunkresData<T> m_dat{n_lhs_verts, n_rhs_verts, cost};
return m_dat.solve();
}