Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
hungarian_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31# include <iostream>
32# include <Hungarian.H>
33# include <tpl_dynMat.H>
34
35using namespace std;
36using namespace Aleph;
37
46{
47 cout << "=== Basic Assignment (4x4 workers-to-tasks) ===" << endl;
48 cout << endl;
49 cout << "Cost matrix:" << endl;
50 cout << " Task0 Task1 Task2 Task3" << endl;
51 cout << "W0: 82 83 69 92" << endl;
52 cout << "W1: 77 37 49 92" << endl;
53 cout << "W2: 11 69 5 86" << endl;
54 cout << "W3: 8 9 98 23" << endl;
55 cout << endl;
56
58 {82, 83, 69, 92},
59 {77, 37, 49, 92},
60 {11, 69, 5, 86},
61 { 8, 9, 98, 23}
62 });
63
64 cout << "Optimal total cost: " << ha.get_total_cost() << endl;
65 cout << "Assignments:" << endl;
66 for (auto [r, c] : ha.get_assignments())
67 cout << " Worker " << r << " -> Task " << c << endl;
68 cout << endl;
69
70 // Show individual costs
71 constexpr int costs[4][4] = {
72 {82, 83, 69, 92},
73 {77, 37, 49, 92},
74 {11, 69, 5, 86},
75 { 8, 9, 98, 23}
76 };
77 cout << "Detailed:" << endl;
78 for (auto [r, c] : ha.get_assignments())
79 cout << " Worker " << r << " -> Task " << c
80 << " (cost " << costs[r][c] << ")" << endl;
81 cout << endl;
82}
83
92{
93 cout << "=== Maximization (3x3 profit matrix) ===" << endl;
94 cout << endl;
95 cout << "Profit matrix:" << endl;
96 cout << " Job0 Job1 Job2" << endl;
97 cout << "W0: 10 5 13" << endl;
98 cout << "W1: 3 9 18" << endl;
99 cout << "W2: 10 6 12" << endl;
100 cout << endl;
101
102 auto mat = DynMatrix<int>(3, 3, 0);
103 mat.allocate();
104 int data[3][3] = {{10, 5, 13}, {3, 9, 18}, {10, 6, 12}};
105 for (size_t i = 0; i < 3; ++i)
106 for (size_t j = 0; j < 3; ++j)
107 mat(i, j) = data[i][j];
108
109 auto result = hungarian_max_assignment(mat);
110
111 cout << "Maximum total profit: " << result.total_cost << endl;
112 cout << "Assignments:" << endl;
113 for (auto [r, c] : result.get_pairs())
114 cout << " Worker " << r << " -> Job " << c
115 << " (profit " << data[r][c] << ")" << endl;
116 cout << endl;
117}
118
127{
128 cout << "=== Rectangular (3 workers, 5 tasks) ===" << endl;
129 cout << endl;
130 cout << "Cost matrix:" << endl;
131 cout << " T0 T1 T2 T3 T4" << endl;
132 cout << "W0: 10 3 7 2 8" << endl;
133 cout << "W1: 5 9 1 6 4" << endl;
134 cout << "W2: 12 11 6 3 7" << endl;
135 cout << endl;
136
138 {10, 3, 7, 2, 8},
139 { 5, 9, 1, 6, 4},
140 {12, 11, 6, 3, 7}
141 });
142
143 cout << "Optimal total cost: " << ha.get_total_cost() << endl;
144 const size_t unassigned_count = 5 - ha.get_assignments().size();
145 cout << "Assignments (" << unassigned_count << " tasks left unassigned):" << endl;
146 for (auto [r, c] : ha.get_assignments())
147 cout << " Worker " << r << " -> Task " << c << endl;
148 cout << endl;
149}
150
159int main()
160{
164
165 return 0;
166}
Hungarian (Munkres) algorithm for the assignment problem.
Dynamic matrix with sparse storage.
Definition tpl_dynMat.H:120
Hungarian (Munkres) algorithm for optimal assignment.
Definition Hungarian.H:176
Cost_Type get_total_cost() const noexcept
Get the optimal total cost.
Definition Hungarian.H:373
Hungarian_Result< Cost_Type > hungarian_max_assignment(const DynMatrix< Cost_Type > &cost)
Compute maximum-profit assignment (free function).
Definition Hungarian.H:499
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.
int main()
Runs the three Hungarian algorithm example demonstrations.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
STL namespace.
gsl_rng * r
Dynamic matrix with lazy allocation.