Skip to main content

Munkres

Munkres

Functions

Name
template <template< typename, typename… > class Container,typename T ,typename… Args>
std::enable_if<!std::is_convertible< Container< T, Args… >, std::string >::value &&!std::is_constructible< Container< T, Args… >, std::string >::value, std::ostream & >::type
operator<<(std::ostream & os, Container< T, Args… > const & con)
template <typename T >
void
handle_negatives(std::vector< std::vector< T > > & matrix, bool allowed =true)
template <typename T >
void
pad_matrix(std::vector< std::vector< T > > & matrix)
template <typename T >
void
step1(std::vector< std::vector< T > > & matrix, int & step)
void clear_covers(std::vector< int > & cover)
template <typename T >
void
step2(std::vector< std::vector< T > > const & matrix, std::vector< std::vector< int > > & M, std::vector< int > & RowCover, std::vector< int > & ColCover, int & step)
void step3(std::vector< std::vector< int > > const & M, std::vector< int > & ColCover, int & step)
template <typename T >
void
find_a_zero(int & row, int & col, std::vector< std::vector< T > > const & matrix, std::vector< int > const & RowCover, std::vector< int > const & ColCover)
bool star_in_row(int row, std::vector< std::vector< int > > const & M)
void find_star_in_row(int row, int & col, std::vector< std::vector< int > > const & M)
template <typename T >
void
step4(std::vector< std::vector< T > > const & matrix, std::vector< std::vector< int > > & M, std::vector< int > & RowCover, std::vector< int > & ColCover, int & path_row_0, int & path_col_0, int & step)
void find_star_in_col(int c, int & r, std::vector< std::vector< int > > const & M)
void find_prime_in_row(int r, int & c, std::vector< std::vector< int > > const & M)
void augment_path(std::vector< std::vector< int > > & path, int path_count, std::vector< std::vector< int > > & M)
void erase_primes(std::vector< std::vector< int > > & M)
void step5(std::vector< std::vector< int > > & path, int path_row_0, int path_col_0, std::vector< std::vector< int > > & M, std::vector< int > & RowCover, std::vector< int > & ColCover, int & step)
template <typename T >
void
find_smallest(T & minval, std::vector< std::vector< T > > const & matrix, std::vector< int > const & RowCover, std::vector< int > const & ColCover)
template <typename T >
void
step6(std::vector< std::vector< T > > & matrix, std::vector< int > const & RowCover, std::vector< int > const & ColCover, int & step)
template <template< typename, typename… > class Container,typename T ,typename… Args>
T
output_solution(Container< Container< T, Args… > > const & original, std::vector< std::vector< int > > const & M)
template <template< typename, typename… > class Container,typename T ,typename… Args>
std::enable_if< std::is_integral< T >::value, T >::type
hungarian(Container< Container< T, Args… > > const & original, bool allow_negatives =true)
Hungarian algorithm (Munkres algorithm) implementation.
template <template< typename, typename… > class Container,typename T ,typename… Args>
std::enable_if< std::is_integral< T >::value, T >::type
hungarian_with_assignment(Container< Container< T, Args… > > const & original, std::vector< std::vector< int > > & assignment_matrix, bool allow_negatives =true)
Hungarian algorithm with assignment matrix output.
int hungarian(std::vector< std::vector< int > > const & original, bool allow_negatives =true)
int hungarian_with_assignment(std::vector< std::vector< int > > const & original, std::vector< std::vector< int > > & assignment_matrix, bool allow_negatives =true)

Functions Documentation

function operator<<

template <template< typename, typename... > class Container,
typename T ,
typename... Args>
std::enable_if<!std::is_convertible< Container< T, Args... >, std::string >::value &&!std::is_constructible< Container< T, Args... >, std::string >::value, std::ostream & >::type operator<<(
    std::ostream & os,
    Container< T, Args... > const & con
)

function handle_negatives

template <typename T >
void handle_negatives(
    std::vector< std::vector< T > > & matrix,
    bool allowed =true
)

function pad_matrix

template <typename T >
void pad_matrix(
    std::vector< std::vector< T > > & matrix
)

function step1

template <typename T >
void step1(
    std::vector< std::vector< T > > & matrix,
    int & step
)

function clear_covers

inline void clear_covers(
    std::vector< int > & cover
)

function step2

template <typename T >
void step2(
    std::vector< std::vector< T > > const & matrix,
    std::vector< std::vector< int > > & M,
    std::vector< int > & RowCover,
    std::vector< int > & ColCover,
    int & step
)

function step3

void step3(
    std::vector< std::vector< int > > const & M,
    std::vector< int > & ColCover,
    int & step
)

function find_a_zero

template <typename T >
void find_a_zero(
    int & row,
    int & col,
    std::vector< std::vector< T > > const & matrix,
    std::vector< int > const & RowCover,
    std::vector< int > const & ColCover
)

function star_in_row

bool star_in_row(
    int row,
    std::vector< std::vector< int > > const & M
)

function find_star_in_row

void find_star_in_row(
    int row,
    int & col,
    std::vector< std::vector< int > > const & M
)

function step4

template <typename T >
void step4(
    std::vector< std::vector< T > > const & matrix,
    std::vector< std::vector< int > > & M,
    std::vector< int > & RowCover,
    std::vector< int > & ColCover,
    int & path_row_0,
    int & path_col_0,
    int & step
)

function find_star_in_col

void find_star_in_col(
    int c,
    int & r,
    std::vector< std::vector< int > > const & M
)

function find_prime_in_row

void find_prime_in_row(
    int r,
    int & c,
    std::vector< std::vector< int > > const & M
)

function augment_path

void augment_path(
    std::vector< std::vector< int > > & path,
    int path_count,
    std::vector< std::vector< int > > & M
)

function erase_primes

void erase_primes(
    std::vector< std::vector< int > > & M
)

function step5

void step5(
    std::vector< std::vector< int > > & path,
    int path_row_0,
    int path_col_0,
    std::vector< std::vector< int > > & M,
    std::vector< int > & RowCover,
    std::vector< int > & ColCover,
    int & step
)

function find_smallest

template <typename T >
void find_smallest(
    T & minval,
    std::vector< std::vector< T > > const & matrix,
    std::vector< int > const & RowCover,
    std::vector< int > const & ColCover
)

function step6

template <typename T >
void step6(
    std::vector< std::vector< T > > & matrix,
    std::vector< int > const & RowCover,
    std::vector< int > const & ColCover,
    int & step
)

function output_solution

template <template< typename, typename... > class Container,
typename T ,
typename... Args>
T output_solution(
    Container< Container< T, Args... > > const & original,
    std::vector< std::vector< int > > const & M
)

function hungarian

template <template< typename, typename... > class Container,
typename T ,
typename... Args>
std::enable_if< std::is_integral< T >::value, T >::type hungarian(
    Container< Container< T, Args... > > const & original,
    bool allow_negatives =true
)

Hungarian algorithm (Munkres algorithm) implementation.

Parameters:

  • original Cost matrix where original[i][j] is the cost of assigning worker i to job j
  • allow_negatives Whether to allow negative costs in the matrix

Template Parameters:

  • Container Container type (e.g., std::vector)
  • T Element type (must be integral)
  • Args Additional template parameters for the container

Return: Total minimum cost of the optimal assignment

Note: After the algorithm completes, the original matrix is modified:

  • Zeros indicate the optimal assignment
  • Use the starred zeros to extract the actual assignments

Solves the assignment problem to find minimum cost assignment in a bipartite graph.

function hungarian_with_assignment

template <template< typename, typename... > class Container,
typename T ,
typename... Args>
std::enable_if< std::is_integral< T >::value, T >::type hungarian_with_assignment(
    Container< Container< T, Args... > > const & original,
    std::vector< std::vector< int > > & assignment_matrix,
    bool allow_negatives =true
)

Hungarian algorithm with assignment matrix output.

Parameters:

  • original Cost matrix where original[i][j] is the cost of assigning worker i to job j
  • assignment_matrix Output matrix where assignment_matrix[i][j] = 1 if worker i is assigned to job j
  • allow_negatives Whether to allow negative costs in the matrix

Return: Total minimum cost of the optimal assignment

Solves the assignment problem and returns both cost and assignment matrix.

function hungarian

inline int hungarian(
    std::vector< std::vector< int > > const & original,
    bool allow_negatives =true
)

function hungarian_with_assignment

inline int hungarian_with_assignment(
    std::vector< std::vector< int > > const & original,
    std::vector< std::vector< int > > & assignment_matrix,
    bool allow_negatives =true
)

Updated on 2026-11-13 at 20:25:41 +0000