|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Hungarian (Munkres) algorithm for optimal assignment. More...
#include <Hungarian.H>
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. | |
| 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. | |
| 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 Member Functions | |
| void | solve (const DynMatrix< Cost_Type > &cost) |
| Core algorithm: shortest augmenting paths with dual variables. | |
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_ |
Hungarian (Munkres) algorithm for optimal assignment.
Given an m x n cost matrix, finds the minimum-cost assignment of rows to columns. The constructor computes the solution; query methods retrieve the results.
For rectangular matrices, the smaller dimension is padded with zero-cost rows or columns. Assignments to padded entries are reported as -1.
The algorithm uses the shortest augmenting paths variant with dual variables, running in O(n^3) time where n = max(m, cols).
| Cost_Type | Numeric type for costs (default: double). |
Definition at line 175 of file Hungarian.H.
|
inline |
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. |
Definition at line 307 of file Hungarian.H.
References ah_invalid_argument_if, Aleph::DynMatrix< T >::allocate(), Aleph::DynMatrix< T >::cols(), Aleph::divide_and_conquer_partition_dp(), Aleph::Hungarian_Assignment< Cost_Type >::n_, Aleph::Hungarian_Assignment< Cost_Type >::orig_cols_, Aleph::Hungarian_Assignment< Cost_Type >::orig_rows_, 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. |
Definition at line 344 of file Hungarian.H.
References ah_invalid_argument_if, Aleph::DynMatrix< T >::allocate(), Aleph::divide_and_conquer_partition_dp(), 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 >::rows(), and Aleph::Hungarian_Assignment< Cost_Type >::solve().
|
inlinenoexcept |
Get the original number of columns.
Definition at line 439 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::orig_cols_.
|
inlinenoexcept |
Get the padded square dimension.
Definition at line 429 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::n_.
|
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 384 of file Hungarian.H.
References ah_out_of_range_error_if, Aleph::Hungarian_Assignment< Cost_Type >::orig_rows_, 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 399 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 421 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::col_to_row_.
|
inlinenoexcept |
Get the row-to-column assignment array.
Definition at line 412 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::row_to_col_.
|
inlinenoexcept |
Get the optimal total cost.
Definition at line 373 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::total_cost_.
Referenced by example_basic_assignment(), and example_rectangular().
|
inlinenoexcept |
Get the original number of rows.
Definition at line 434 of file Hungarian.H.
References Aleph::Hungarian_Assignment< Cost_Type >::orig_rows_.
Referenced by Aleph::Hungarian_Assignment< Cost_Type >::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 193 of file Hungarian.H.
References Aleph::and, Aleph::Hungarian_Assignment< Cost_Type >::col_to_row_, Aleph::divide_and_conquer_partition_dp(), j0(), j1(), Aleph::Hungarian_Assignment< Cost_Type >::n_, Aleph::Hungarian_Assignment< Cost_Type >::orig_cols_, Aleph::Hungarian_Assignment< Cost_Type >::orig_rows_, Aleph::DynMatrix< T >::read(), Aleph::Hungarian_Assignment< Cost_Type >::row_to_col_, 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().
|
private |
Definition at line 186 of file Hungarian.H.
Referenced by Aleph::Hungarian_Assignment< Cost_Type >::get_col_assignments(), and Aleph::Hungarian_Assignment< Cost_Type >::solve().
|
private |
|
private |
Definition at line 183 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(), and Aleph::Hungarian_Assignment< Cost_Type >::solve().
|
private |
Definition at line 182 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(), and Aleph::Hungarian_Assignment< Cost_Type >::solve().
|
private |
|
private |
Definition at line 184 of file Hungarian.H.
Referenced by Aleph::Hungarian_Assignment< Cost_Type >::get_total_cost(), and Aleph::Hungarian_Assignment< Cost_Type >::solve().