41 static constexpr int NORMAL = 0;
42 static constexpr int STAR = 1;
43 static constexpr int PRIME = 2;
58 const size_t rows = m.rows(), columns = m.columns(), size = std::max(rows, columns);
61 std::cout <<
"Munkres input: " << m << std::endl;
72 matrix.resize(size, size, matrix.max());
76 mask_matrix.resize(size, size);
78 row_mask =
new bool[size];
79 col_mask =
new bool[size];
80 for (
size_t i = 0; i < size; i++)
85 for (
size_t i = 0; i < size; i++)
94 replace_infinites(matrix);
96 minimize_along_direction(matrix, rows >= columns);
97 minimize_along_direction(matrix, rows < columns);
129 for (
size_t row = 0; row < size; row++)
131 for (
size_t col = 0; col < size; col++)
133 if (mask_matrix(row, col) == STAR)
135 matrix(row, col) = 0;
139 matrix(row, col) = -1;
145 std::cout <<
"Munkres output: " << matrix << std::endl;
149 matrix.resize(rows, columns);
159 const size_t rows = matrix.rows(), columns = matrix.columns();
160 assert(rows > 0 && columns > 0);
161 double max = matrix(0, 0);
162 constexpr auto infinity = std::numeric_limits<double>::infinity();
165 for (
size_t row = 0; row < rows; row++)
167 for (
size_t col = 0; col < columns; col++)
169 if (matrix(row, col) != infinity)
173 max = matrix(row, col);
177 max = std::max<double>(max, matrix(row, col));
194 for (
size_t row = 0; row < rows; row++)
196 for (
size_t col = 0; col < columns; col++)
198 if (matrix(row, col) == infinity)
200 matrix(row, col) = max;
206 static void minimize_along_direction(
Matrix<Data> &matrix,
const bool over_columns)
208 const size_t outer_size = over_columns ? matrix.columns() : matrix.rows(),
209 inner_size = over_columns ? matrix.rows() : matrix.columns();
213 for (
size_t i = 0; i < outer_size; i++)
215 double min = over_columns ? matrix(0, i) : matrix(i, 0);
220 for (
size_t j = 1; j < inner_size && min > 0; j++)
222 min = std::min<double>(min, over_columns ? matrix(j, i) : matrix(i, j));
227 for (
size_t j = 0; j < inner_size; j++)
243 inline bool find_uncovered_in_matrix(
const double item,
size_t &row,
size_t &col)
const
245 const size_t rows = matrix.rows(), columns = matrix.columns();
247 for (row = 0; row < rows; row++)
251 for (col = 0; col < columns; col++)
255 if (matrix(row, col) == item)
267 bool pair_in_list(
const std::pair<size_t, size_t> &needle,
const std::list<std::pair<size_t, size_t>> &haystack)
269 for (std::list<std::pair<size_t, size_t>>::const_iterator i = haystack.begin(); i != haystack.end(); i++)
282 const size_t rows = matrix.rows(), columns = matrix.columns();
284 for (
size_t row = 0; row < rows; row++)
286 for (
size_t col = 0; col < columns; col++)
288 if (0 == matrix(row, col))
290 for (
size_t nrow = 0; nrow < row; nrow++)
291 if (STAR == mask_matrix(nrow, col))
294 mask_matrix(row, col) = STAR;
307 const size_t rows = matrix.rows(), columns = matrix.columns();
308 size_t covercount = 0;
310 for (
size_t row = 0; row < rows; row++)
311 for (
size_t col = 0; col < columns; col++)
312 if (STAR == mask_matrix(row, col))
314 col_mask[col] =
true;
318 if (covercount >= matrix.minsize())
321 std::cout <<
"Final cover count: " << covercount << std::endl;
327 std::cout <<
"Munkres matrix has " << covercount <<
" of " << matrix.minsize()
328 <<
" Columns covered:" << std::endl;
329 std::cout << matrix << std::endl;
344 if (find_uncovered_in_matrix(0, saverow, savecol))
346 mask_matrix(saverow, savecol) = PRIME;
353 for (
size_t ncol = 0; ncol < matrix.columns(); ncol++)
355 if (mask_matrix(saverow, ncol) == STAR)
357 row_mask[saverow] =
true;
358 col_mask[ncol] =
false;
368 const size_t rows = matrix.rows(), columns = matrix.columns();
372 std::list<std::pair<size_t, size_t>> seq;
374 std::pair<size_t, size_t> z0(saverow, savecol);
375 seq.insert(seq.end(), z0);
378 std::pair<size_t, size_t> z1(-1, -1);
379 std::pair<size_t, size_t> z2n(-1, -1);
381 size_t row, col = savecol;
398 for (row = 0; row < rows; row++)
400 if (mask_matrix(row, col) == STAR)
404 if (pair_in_list(z1, seq))
410 seq.insert(seq.end(), z1);
420 for (col = 0; col < columns; col++)
422 if (mask_matrix(row, col) == PRIME)
426 if (pair_in_list(z2n, seq))
431 seq.insert(seq.end(), z2n);
437 for (std::list<std::pair<size_t, size_t>>::iterator i = seq.begin(); i != seq.end(); i++)
440 if (mask_matrix(i->first, i->second) == STAR)
441 mask_matrix(i->first, i->second) = NORMAL;
445 if (mask_matrix(i->first, i->second) == PRIME)
446 mask_matrix(i->first, i->second) = STAR;
450 for (
size_t row = 0; row < mask_matrix.rows(); row++)
452 for (
size_t col = 0; col < mask_matrix.columns(); col++)
454 if (mask_matrix(row, col) == PRIME)
456 mask_matrix(row, col) = NORMAL;
461 for (
size_t i = 0; i < rows; i++)
466 for (
size_t i = 0; i < columns; i++)
477 const size_t rows = matrix.rows(), columns = matrix.columns();
486 double h = std::numeric_limits<double>::max();
487 for (
size_t row = 0; row < rows; row++)
491 for (
size_t col = 0; col < columns; col++)
495 if (h > matrix(row, col) && matrix(row, col) != 0)
497 h = matrix(row, col);
504 for (
size_t row = 0; row < rows; row++)
508 for (
size_t col = 0; col < columns; col++)
510 matrix(row, col) += h;
515 for (
size_t col = 0; col < columns; col++)
519 for (
size_t row = 0; row < rows; row++)
521 matrix(row, col) -= h;
533 size_t saverow = 0, savecol = 0;