Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Hungarian_Assignment< Cost_Type > Class Template Reference

Implementation of the Hungarian (Munkres) algorithm. More...

#include <Hungarian.H>

Collaboration diagram for Aleph::Hungarian_Assignment< Cost_Type >:
[legend]

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.
 
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 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_
 

Detailed Description

template<typename Cost_Type = double>
class Aleph::Hungarian_Assignment< 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.

Template Parameters
Cost_TypeNumeric type for costs. Must be a signed type or floating-point to support internal reductions.
Complexity:
Time \(O(n^3)\), Space \(O(n^2)\), where \(n = \max(rows, cols)\).
Examples
hungarian_example.cc.

Definition at line 141 of file Hungarian.H.

Constructor & Destructor Documentation

◆ Hungarian_Assignment() [1/2]

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.

Parameters
[in]costThe m x n cost matrix.
Exceptions
std::invalid_argumentif the matrix is empty.

Definition at line 272 of file Hungarian.H.

References ah_invalid_argument_if, Aleph::DynMatrix< T >::allocate(), Aleph::blossom_maximum_cardinality_matching(), 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::DynMatrix< T >::read(), Aleph::DynMatrix< T >::rows(), and Aleph::Hungarian_Assignment< Cost_Type >::solve().

◆ Hungarian_Assignment() [2/2]

template<typename Cost_Type = double>
Aleph::Hungarian_Assignment< Cost_Type >::Hungarian_Assignment ( std::initializer_list< std::initializer_list< Cost_Type > >  rows)
inline

Construct and solve from an initializer list of rows.

Convenience constructor for inline cost matrices.

Parameters
[in]rowsInitializer list of rows, each an initializer list of costs. All rows must have the same length.
Exceptions
std::invalid_argumentif rows is empty or rows have different lengths.
Example
{10, 5, 13},
{3, 9, 18},
{10, 6, 12}
});
Implementation of the Hungarian (Munkres) algorithm.
Definition Hungarian.H:143
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466

Definition at line 315 of file Hungarian.H.

References ah_invalid_argument_if, Aleph::DynMatrix< T >::allocate(), Aleph::blossom_maximum_cardinality_matching(), Aleph::Hungarian_Assignment< Cost_Type >::n_, Aleph::Hungarian_Assignment< Cost_Type >::orig_cols_, Aleph::Hungarian_Assignment< Cost_Type >::orig_rows_, row, Aleph::Hungarian_Assignment< Cost_Type >::rows(), and Aleph::Hungarian_Assignment< Cost_Type >::solve().

Member Function Documentation

◆ cols()

template<typename Cost_Type = double>
size_t Aleph::Hungarian_Assignment< Cost_Type >::cols ( ) const
inlinenoexcept

Get the original number of columns.

Returns
The number of columns in the input cost matrix.

Definition at line 446 of file Hungarian.H.

References Aleph::Hungarian_Assignment< Cost_Type >::orig_cols_.

◆ dimension()

template<typename Cost_Type = double>
size_t Aleph::Hungarian_Assignment< Cost_Type >::dimension ( ) const
inlinenoexcept

Get the padded square dimension.

Returns
max(original_rows, original_cols).

Definition at line 430 of file Hungarian.H.

References Aleph::Hungarian_Assignment< Cost_Type >::n_.

◆ extract_col_assignments()

template<typename Cost_Type = double>
Array< long > Aleph::Hungarian_Assignment< Cost_Type >::extract_col_assignments ( ) &&
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.

Returns
Column-to-row array by move.

Definition at line 422 of file Hungarian.H.

References Aleph::Hungarian_Assignment< Cost_Type >::col_to_row_.

◆ extract_row_assignments()

template<typename Cost_Type = double>
Array< long > Aleph::Hungarian_Assignment< Cost_Type >::extract_row_assignments ( ) &&
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.

Returns
Row-to-column array by move.

Definition at line 410 of file Hungarian.H.

References Aleph::Hungarian_Assignment< Cost_Type >::row_to_col_.

◆ get_assignment()

template<typename Cost_Type = double>
long Aleph::Hungarian_Assignment< Cost_Type >::get_assignment ( const size_t  row) const
inline

Get the column assigned to a given row.

Parameters
[in]rowRow index (0-based, must be < original rows).
Returns
Column index, or -1 if the row is unassigned (dummy).
Exceptions
std::out_of_rangeif row >= original number of rows.

Definition at line 362 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_.

◆ get_assignments()

template<typename Cost_Type = double>
DynList< std::pair< size_t, size_t > > Aleph::Hungarian_Assignment< Cost_Type >::get_assignments ( ) const
inline

Get all assignment pairs.

Returns only the pairs (row, col) where both indices are within the original (non-padded) dimensions.

Returns
List of (row, col) pairs.

Definition at line 376 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_.

◆ get_col_assignments()

template<typename Cost_Type = double>
const Array< long > & Aleph::Hungarian_Assignment< Cost_Type >::get_col_assignments ( ) const
inlinenoexcept

Get the column-to-row assignment array.

Returns
Const reference to array where entry j is the row assigned to column j (-1 if dummy).

Definition at line 398 of file Hungarian.H.

References Aleph::Hungarian_Assignment< Cost_Type >::col_to_row_.

◆ get_row_assignments()

template<typename Cost_Type = double>
const Array< long > & Aleph::Hungarian_Assignment< Cost_Type >::get_row_assignments ( ) const
inlinenoexcept

Get the row-to-column assignment array.

Returns
Const reference to array where entry i is the column assigned to row i (-1 if dummy).

Definition at line 389 of file Hungarian.H.

References Aleph::Hungarian_Assignment< Cost_Type >::row_to_col_.

◆ get_total_cost()

template<typename Cost_Type = double>
Cost_Type Aleph::Hungarian_Assignment< Cost_Type >::get_total_cost ( ) const
inlinenoexcept

Get the optimal total cost.

Returns
The minimum total assignment cost.
Examples
hungarian_example.cc.

Definition at line 351 of file Hungarian.H.

References Aleph::Hungarian_Assignment< Cost_Type >::total_cost_.

Referenced by example_basic_assignment(), and example_rectangular().

◆ rows()

template<typename Cost_Type = double>
size_t Aleph::Hungarian_Assignment< Cost_Type >::rows ( ) const
inlinenoexcept

Get the original number of rows.

Returns
The number of rows in the input cost matrix.

Definition at line 438 of file Hungarian.H.

References Aleph::Hungarian_Assignment< Cost_Type >::orig_rows_.

Referenced by Aleph::Hungarian_Assignment< Cost_Type >::Hungarian_Assignment().

◆ solve()

Member Data Documentation

◆ col_to_row_

◆ n_

◆ orig_cols_

◆ orig_rows_

◆ row_to_col_

◆ total_cost_


The documentation for this class was generated from the following file: