# include <iostream>
{
cout << "=== Basic Assignment (4x4 workers-to-tasks) ===" << endl;
cout << endl;
cout << "Cost matrix:" << endl;
cout << " Task0 Task1 Task2 Task3" << endl;
cout << "W0: 82 83 69 92" << endl;
cout << "W1: 77 37 49 92" << endl;
cout << "W2: 11 69 5 86" << endl;
cout << "W3: 8 9 98 23" << endl;
cout << endl;
{82, 83, 69, 92},
{77, 37, 49, 92},
{11, 69, 5, 86},
{ 8, 9, 98, 23}
});
cout << "Assignments:" << endl;
for (
auto [
r, c] : ha.get_assignments())
cout <<
" Worker " <<
r <<
" -> Task " << c << endl;
cout << endl;
constexpr int costs[4][4] = {
{82, 83, 69, 92},
{77, 37, 49, 92},
{11, 69, 5, 86},
{ 8, 9, 98, 23}
};
cout << "Detailed:" << endl;
for (
auto [
r, c] : ha.get_assignments())
cout <<
" Worker " <<
r <<
" -> Task " << c
<<
" (cost " << costs[
r][c] <<
")" << endl;
cout << endl;
}
{
cout << "=== Maximization (3x3 profit matrix) ===" << endl;
cout << endl;
cout << "Profit matrix:" << endl;
cout << " Job0 Job1 Job2" << endl;
cout << "W0: 10 5 13" << endl;
cout << "W1: 3 9 18" << endl;
cout << "W2: 10 6 12" << endl;
cout << endl;
mat.allocate();
int data[3][3] = {{10, 5, 13}, {3, 9, 18}, {10, 6, 12}};
for (size_t i = 0; i < 3; ++i)
for (size_t j = 0; j < 3; ++j)
mat(i, j) = data[i][j];
cout << "Maximum total profit: " << result.total_cost << endl;
cout << "Assignments:" << endl;
for (
auto [
r, c] : result.get_pairs())
cout <<
" Worker " <<
r <<
" -> Job " << c
<<
" (profit " << data[
r][c] <<
")" << endl;
cout << endl;
}
{
cout << "=== Rectangular (3 workers, 5 tasks) ===" << endl;
cout << endl;
cout << "Cost matrix:" << endl;
cout << " T0 T1 T2 T3 T4" << endl;
cout << "W0: 10 3 7 2 8" << endl;
cout << "W1: 5 9 1 6 4" << endl;
cout << "W2: 12 11 6 3 7" << endl;
cout << endl;
{10, 3, 7, 2, 8},
{ 5, 9, 1, 6, 4},
{12, 11, 6, 3, 7}
});
const size_t unassigned_count = 5 - ha.get_assignments().size();
cout << "Assignments (" << unassigned_count << " tasks left unassigned):" << endl;
for (
auto [
r, c] : ha.get_assignments())
cout <<
" Worker " <<
r <<
" -> Task " << c << endl;
cout << endl;
}
{
return 0;
}
Hungarian (Kuhn-Munkres) algorithm for the optimal assignment problem.
Dynamic matrix with sparse storage.
Implementation of the Hungarian (Munkres) algorithm.
Cost_Type get_total_cost() const noexcept
Get the optimal total cost.
Hungarian_Result< Cost_Type > hungarian_max_assignment(const DynMatrix< Cost_Type > &cost)
Compute maximum-profit assignment (free function).
void example_rectangular()
Demonstrates rectangular assignment (3 workers, 5 tasks) using Hungarian_Assignment.
void example_basic_assignment()
Demonstrates a 4x4 workers-to-tasks assignment using Hungarian_Assignment.
void example_maximization()
Demonstrates maximizing total profit for a 3x3 profit matrix and prints the results.
Main namespace for Aleph-w library functions.
Dynamic matrix with lazy allocation.