|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Implementation of the Hungarian (Munkres) algorithm. More...
#include <Hungarian.H>
Classes | |
| struct | Cost_Range |
Public Member Functions | |
| Hungarian_Assignment (const DynMatrix< Cost_Type > &cost) | |
| Construct and solve from a DynMatrix cost matrix. | |
| Hungarian_Assignment (std::initializer_list< std::initializer_list< Cost_Type > > rows) | |
| Construct and solve from an initializer list of rows. | |
| Hungarian_Assignment (const Hungarian_Assignment &other)=default | |
| Copy the assignment of another solver. | |
| Hungarian_Assignment (Hungarian_Assignment &&other) noexcept | |
| Take the assignment of another solver, which is left empty. | |
| Hungarian_Assignment & | operator= (const Hungarian_Assignment &other)=default |
| Copy the assignment of another solver. | |
| Hungarian_Assignment & | operator= (Hungarian_Assignment &&other) noexcept |
| Exchange this solver with another, which is left with the previous assignment of this one. | |
| void | swap (Hungarian_Assignment &other) noexcept |
| Exchange in O(1) this solver and another. | |
| Cost_Type | get_total_cost () const noexcept |
| Get the optimal total cost. | |
| long | get_assignment (const size_t row) const |
| Get the column assigned to a given row. | |
| DynList< std::pair< size_t, size_t > > | get_assignments () const |
| Get all assignment pairs. | |
| const Array< long > & | get_row_assignments () const noexcept |
| Get the row-to-column assignment array. | |
| const Array< long > & | get_col_assignments () const noexcept |
| Get the column-to-row assignment array. | |
| Array< long > | extract_row_assignments () &&noexcept |
| Move out the row-to-column assignment array. | |
| Array< long > | extract_col_assignments () &&noexcept |
| Move out the column-to-row assignment array. | |
| size_t | dimension () const noexcept |
| Get the padded square dimension. | |
| size_t | rows () const noexcept |
| Get the original number of rows. | |
| size_t | cols () const noexcept |
| Get the original number of columns. | |
Private Types | |
| using | Wide = std::conditional_t< Integer and sizeof(Cost_Type)< sizeof(long long), long long, Cost_Type > |
Private Member Functions | |
| void | solve (const DynMatrix< Cost_Type > &cost, const Cost_Range &range) |
| Core algorithm: shortest augmenting paths with dual variables. | |
Static Private Member Functions | |
| static void | throw_beyond () |
Private Attributes | |
| size_t | n_ = 0 |
| size_t | orig_rows_ = 0 |
| size_t | orig_cols_ = 0 |
| Cost_Type | total_cost_ = Cost_Type{0} |
| Array< long > | row_to_col_ |
| Array< long > | col_to_row_ |
Static Private Attributes | |
| static constexpr bool | Integer = std::is_integral_v<Cost_Type> |
Implementation of the Hungarian (Munkres) algorithm.
Computes the minimum-cost perfect matching in a bipartite graph represented as a cost matrix.
The constructor performs the entire computation. Results can then be queried using provided methods.
| Cost_Type | Numeric type for costs. Must be a signed type or floating-point to support internal reductions. |
Definition at line 141 of file Hungarian.H.
|
private |
Definition at line 160 of file Hungarian.H.
|
inlineexplicit |
Construct and solve from a DynMatrix cost matrix.
Computes the minimum-cost assignment for the given cost matrix. Rectangular matrices are padded with zeros to form a square.
| [in] | cost | The m x n cost matrix. |
| std::invalid_argument | if the matrix is empty. |
| std::overflow_error | if, with an integer cost type, the optimum does not fit in it; with long long, also if the costs come near its limits: when a bound of the costs, and twice the sum of the steps of the potentials, which is about the optimum, pass max() / 4. |
Definition at line 344 of file Hungarian.H.
References ah_invalid_argument_if, Aleph::DynMatrix< T >::allocate(), Aleph::DynMatrix< T >::cols(), Aleph::Hungarian_Assignment< Cost_Type >::n_, Aleph::Hungarian_Assignment< Cost_Type >::orig_cols_, Aleph::Hungarian_Assignment< Cost_Type >::orig_rows_, Aleph::range(), Aleph::DynMatrix< T >::read(), Aleph::DynMatrix< T >::rows(), and Aleph::Hungarian_Assignment< Cost_Type >::solve().
|
inline |
Construct and solve from an initializer list of rows.
Convenience constructor for inline cost matrices.
| [in] | rows | Initializer list of rows, each an initializer list of costs. All rows must have the same length. |
| std::invalid_argument | if rows is empty or rows have different lengths. |
| std::overflow_error | if, with an integer cost type, the optimum does not fit in it; with long long, also if the costs come near its limits: when a bound of the costs, and twice the sum of the steps of the potentials, which is about the optimum, pass max() / 4. |
Definition at line 397 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::Cost_Range::add(), ah_invalid_argument_if, Aleph::DynMatrix< T >::allocate(), Aleph::Hungarian_Assignment< Cost_Type >::n_, Aleph::Hungarian_Assignment< Cost_Type >::orig_cols_, Aleph::Hungarian_Assignment< Cost_Type >::orig_rows_, Aleph::range(), row, Aleph::Hungarian_Assignment< Cost_Type >::rows(), and Aleph::Hungarian_Assignment< Cost_Type >::solve().
|
default |
Copy the assignment of another solver.
| [in] | other | solver to copy. |
| std::bad_alloc | if there is no memory for the copy. |
|
inlinenoexcept |
Take the assignment of another solver, which is left empty.
The implicit move took the assignment arrays but copied the dimensions and the cost: the moved-from solver then reported rows that get_assignment() could not read.
| [in,out] | other | solver to move from. |
Definition at line 446 of file Hungarian.H.
|
inlinenoexcept |
Get the original number of columns.
Definition at line 582 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::orig_cols_.
Referenced by Aleph::hungarian_assignment().
|
inlinenoexcept |
Get the padded square dimension.
Definition at line 566 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::n_.
|
inlinenoexcept |
Move out the column-to-row assignment array.
Only callable on rvalue Hungarian_Assignment objects, ensuring the solver is about to be destroyed and ownership transfers safely.
Definition at line 558 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::col_to_row_.
|
inlinenoexcept |
Move out the row-to-column assignment array.
Only callable on rvalue Hungarian_Assignment objects, ensuring the solver is about to be destroyed and ownership transfers safely.
Definition at line 546 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::row_to_col_.
|
inline |
Get the column assigned to a given row.
| [in] | row | Row index (0-based, must be < original rows). |
| std::out_of_range | if row >= original number of rows. |
Definition at line 498 of file Hungarian.H.
References ah_out_of_range_error_if, Aleph::Hungarian_Assignment< Cost_Type >::orig_rows_, row, and Aleph::Hungarian_Assignment< Cost_Type >::row_to_col_.
|
inline |
Get all assignment pairs.
Returns only the pairs (row, col) where both indices are within the original (non-padded) dimensions.
Definition at line 512 of file Hungarian.H.
References Aleph::and, Aleph::DynList< T >::append(), Aleph::Hungarian_Assignment< Cost_Type >::orig_cols_, Aleph::Hungarian_Assignment< Cost_Type >::orig_rows_, and Aleph::Hungarian_Assignment< Cost_Type >::row_to_col_.
|
inlinenoexcept |
Get the column-to-row assignment array.
Definition at line 534 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::col_to_row_.
|
inlinenoexcept |
Get the row-to-column assignment array.
Definition at line 525 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::row_to_col_.
|
inlinenoexcept |
Get the optimal total cost.
Definition at line 487 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::total_cost_.
Referenced by example_basic_assignment(), example_rectangular(), Aleph::hungarian_assignment(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
default |
Copy the assignment of another solver.
| [in] | other | solver to copy. |
| std::bad_alloc | if there is no memory for the copy. |
|
inlinenoexcept |
Exchange this solver with another, which is left with the previous assignment of this one.
| [in,out] | other | solver to move from. |
Definition at line 465 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::swap().
|
inlinenoexcept |
Get the original number of rows.
Definition at line 574 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::orig_rows_.
Referenced by Aleph::Hungarian_Assignment< Cost_Type >::Hungarian_Assignment(), and Aleph::hungarian_assignment().
|
inlineprivate |
Core algorithm: shortest augmenting paths with dual variables.
Uses 1-indexed arrays with virtual column 0 trick. cost is an (n+1) x (n+1) matrix with 1-based indexing.
Definition at line 190 of file Hungarian.H.
References ah_overflow_error_if, Aleph::and, col, Aleph::Hungarian_Assignment< Cost_Type >::col_to_row_, Aleph::Hungarian_Assignment< Cost_Type >::Integer, j0(), j1(), Aleph::Hungarian_Assignment< Cost_Type >::n_, Aleph::Hungarian_Assignment< Cost_Type >::orig_cols_, Aleph::Hungarian_Assignment< Cost_Type >::orig_rows_, Aleph::range(), Aleph::DynMatrix< T >::read(), row, Aleph::Hungarian_Assignment< Cost_Type >::row_to_col_, Aleph::Hungarian_Assignment< Cost_Type >::throw_beyond(), and Aleph::Hungarian_Assignment< Cost_Type >::total_cost_.
Referenced by Aleph::Hungarian_Assignment< Cost_Type >::Hungarian_Assignment(), and Aleph::Hungarian_Assignment< Cost_Type >::Hungarian_Assignment().
|
inlinenoexcept |
Exchange in O(1) this solver and another.
| [in,out] | other | solver to exchange with. |
Definition at line 474 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::col_to_row_, Aleph::Hungarian_Assignment< Cost_Type >::n_, Aleph::Hungarian_Assignment< Cost_Type >::orig_cols_, Aleph::Hungarian_Assignment< Cost_Type >::orig_rows_, Aleph::Hungarian_Assignment< Cost_Type >::row_to_col_, Aleph::Array< T >::swap(), and Aleph::Hungarian_Assignment< Cost_Type >::total_cost_.
Referenced by Aleph::Hungarian_Assignment< Cost_Type >::operator=().
|
inlinestaticprivate |
Definition at line 179 of file Hungarian.H.
References ah_overflow_error.
Referenced by Aleph::Hungarian_Assignment< Cost_Type >::solve().
|
private |
|
staticconstexprprivate |
Definition at line 155 of file Hungarian.H.
Referenced by Aleph::Hungarian_Assignment< Cost_Type >::Cost_Range::add(), and Aleph::Hungarian_Assignment< Cost_Type >::solve().
|
private |
Definition at line 148 of file Hungarian.H.
Referenced by Aleph::Hungarian_Assignment< Cost_Type >::Hungarian_Assignment(), Aleph::Hungarian_Assignment< Cost_Type >::Hungarian_Assignment(), Aleph::Hungarian_Assignment< Cost_Type >::dimension(), Aleph::Hungarian_Assignment< Cost_Type >::solve(), and Aleph::Hungarian_Assignment< Cost_Type >::swap().
|
private |
Definition at line 150 of file Hungarian.H.
Referenced by Aleph::Hungarian_Assignment< Cost_Type >::Hungarian_Assignment(), Aleph::Hungarian_Assignment< Cost_Type >::Hungarian_Assignment(), Aleph::Hungarian_Assignment< Cost_Type >::cols(), Aleph::Hungarian_Assignment< Cost_Type >::get_assignments(), Aleph::Hungarian_Assignment< Cost_Type >::solve(), and Aleph::Hungarian_Assignment< Cost_Type >::swap().
|
private |
Definition at line 149 of file Hungarian.H.
Referenced by Aleph::Hungarian_Assignment< Cost_Type >::Hungarian_Assignment(), Aleph::Hungarian_Assignment< Cost_Type >::Hungarian_Assignment(), Aleph::Hungarian_Assignment< Cost_Type >::get_assignment(), Aleph::Hungarian_Assignment< Cost_Type >::get_assignments(), Aleph::Hungarian_Assignment< Cost_Type >::rows(), Aleph::Hungarian_Assignment< Cost_Type >::solve(), and Aleph::Hungarian_Assignment< Cost_Type >::swap().
|
private |
Definition at line 152 of file Hungarian.H.
Referenced by Aleph::Hungarian_Assignment< Cost_Type >::extract_row_assignments(), Aleph::Hungarian_Assignment< Cost_Type >::get_assignment(), Aleph::Hungarian_Assignment< Cost_Type >::get_assignments(), Aleph::Hungarian_Assignment< Cost_Type >::get_row_assignments(), Aleph::Hungarian_Assignment< Cost_Type >::solve(), and Aleph::Hungarian_Assignment< Cost_Type >::swap().
|
private |
Definition at line 151 of file Hungarian.H.
Referenced by Aleph::Hungarian_Assignment< Cost_Type >::get_total_cost(), Aleph::Hungarian_Assignment< Cost_Type >::solve(), and Aleph::Hungarian_Assignment< Cost_Type >::swap().