1 /********************************************************************
2 ********************************************************************
4 ** libhungarian by Cyrill Stachniss, 2004
6 ** Added and adapted to libFirm by Christian Wuerdig, 2006
8 ** Solving the Minimum Assignment Problem using the
11 ** ** This file may be freely copied and distributed! **
13 ** Parts of the used code was originally provided by the
14 ** "Stanford GraphGase", but I made changes to this code.
15 ** As asked by the copyright node of the "Stanford GraphGase",
16 ** I hereby proclaim that this file are *NOT* part of the
17 ** "Stanford GraphGase" distrubition!
19 ** This file is distributed in the hope that it will be useful,
20 ** but WITHOUT ANY WARRANTY; without even the implied
21 ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
24 ********************************************************************
25 ********************************************************************/
29 * @brief Solving the Minimum Assignment Problem using the Hungarian Method.
44 #include "hungarian.h"
46 #define INF (0x7FFFFFFF)
48 struct _hungarian_problem_t {
49 int num_rows; /**< number of rows */
50 int num_cols; /**< number of columns */
51 int **cost; /**< the cost matrix */
52 int max_cost; /**< the maximal costs in the matrix */
53 int match_type; /**< PERFECT or NORMAL matching */
54 bitset_t *missing_left; /**< left side nodes having no edge to the right side */
55 bitset_t *missing_right; /**< right side nodes having no edge to the left side */
57 DEBUG_ONLY(firm_dbg_module_t *dbg);
60 static void hungarian_dump_f(FILE *f, int **C, int rows, int cols, int width) {
64 for (i = 0; i < rows; i++) {
66 for (j = 0; j < cols; j++) {
67 fprintf(f, "%*d", width, C[i][j]);
74 void hungarian_print_cost_matrix(hungarian_problem_t *p, int width) {
75 hungarian_dump_f(stderr, p->cost, p->num_rows, p->num_cols, width);
79 * Create the object and allocate memory for the data structures.
81 hungarian_problem_t *hungarian_new(int rows, int cols, int match_type) {
83 hungarian_problem_t *p = XMALLOCZ(hungarian_problem_t);
85 FIRM_DBG_REGISTER(p->dbg, "firm.hungarian");
88 Is the number of cols not equal to number of rows ?
89 If yes, expand with 0 - cols / 0 - cols
91 rows = MAX(cols, rows);
94 obstack_init(&p->obst);
98 p->match_type = match_type;
101 In case of normal matching, we have to keep
102 track of nodes without edges to kill them in
103 the assignment later.
105 if (match_type == HUNGARIAN_MATCH_NORMAL) {
106 p->missing_left = bitset_obstack_alloc(&p->obst, rows);
107 p->missing_right = bitset_obstack_alloc(&p->obst, cols);
108 bitset_set_all(p->missing_left);
109 bitset_set_all(p->missing_right);
112 /* allocate space for cost matrix */
113 p->cost = OALLOCNZ(&p->obst, int*, rows);
114 for (i = 0; i < p->num_rows; i++)
115 p->cost[i] = OALLOCNZ(&p->obst, int, cols);
121 * Prepare the cost matrix.
123 void hungarian_prepare_cost_matrix(hungarian_problem_t *p, int mode) {
126 if (mode == HUNGARIAN_MODE_MAXIMIZE_UTIL) {
127 for (i = 0; i < p->num_rows; i++) {
128 for (j = 0; j < p->num_cols; j++) {
129 p->cost[i][j] = p->max_cost - p->cost[i][j];
133 else if (mode == HUNGARIAN_MODE_MINIMIZE_COST) {
137 fprintf(stderr, "Unknown mode. Mode was set to HUNGARIAN_MODE_MINIMIZE_COST.\n");
141 * Set cost[left][right] to cost.
143 void hungarian_add(hungarian_problem_t *p, int left, int right, int cost) {
144 assert(p->num_rows > left && "Invalid row selected.");
145 assert(p->num_cols > right && "Invalid column selected.");
148 p->cost[left][right] = cost;
149 p->max_cost = MAX(p->max_cost, cost);
151 if (p->match_type == HUNGARIAN_MATCH_NORMAL) {
152 bitset_clear(p->missing_left, left);
153 bitset_clear(p->missing_right, right);
158 * Set cost[left][right] to 0.
160 void hungarian_remv(hungarian_problem_t *p, int left, int right) {
161 assert(p->num_rows > left && "Invalid row selected.");
162 assert(p->num_cols > right && "Invalid column selected.");
164 p->cost[left][right] = 0;
166 if (p->match_type == HUNGARIAN_MATCH_NORMAL) {
167 bitset_set(p->missing_left, left);
168 bitset_set(p->missing_right, right);
173 * Frees all allocated memory.
175 void hungarian_free(hungarian_problem_t* p) {
176 obstack_free(&p->obst, NULL);
183 int hungarian_solve(hungarian_problem_t* p, int *assignment, int *final_cost, int cost_threshold) {
184 int i, j, m, n, k, l, s, t, q, unmatched, cost;
198 col_mate = XMALLOCNZ(int, p->num_rows);
199 unchosen_row = XMALLOCNZ(int, p->num_rows);
200 row_dec = XMALLOCNZ(int, p->num_rows);
201 slack_row = XMALLOCNZ(int, p->num_rows);
203 row_mate = XMALLOCNZ(int, p->num_cols);
204 parent_row = XMALLOCNZ(int, p->num_cols);
205 col_inc = XMALLOCNZ(int, p->num_cols);
206 slack = XMALLOCNZ(int, p->num_cols);
208 memset(assignment, -1, m * sizeof(assignment[0]));
210 /* Begin subtract column minima in order to start with lots of zeros 12 */
211 DBG((p->dbg, LEVEL_1, "Using heuristic\n"));
213 for (l = 0; l < n; ++l) {
216 for (k = 1; k < m; ++k) {
217 if (p->cost[k][l] < s)
224 for (k = 0; k < m; ++k)
228 /* End subtract column minima in order to start with lots of zeros 12 */
230 /* Begin initial state 16 */
232 for (l = 0; l < n; ++l) {
239 for (k = 0; k < m; ++k) {
242 for (l = 1; l < n; ++l) {
243 if (p->cost[k][l] < s)
249 for (l = 0; l < n; ++l) {
250 if (s == p->cost[k][l] && row_mate[l] < 0) {
253 DBG((p->dbg, LEVEL_1, "matching col %d == row %d\n", l, k));
259 DBG((p->dbg, LEVEL_1, "node %d: unmatched row %d\n", t, k));
260 unchosen_row[t++] = k;
263 /* End initial state 16 */
265 /* Begin Hungarian algorithm 18 */
271 DBG((p->dbg, LEVEL_1, "Matched %d rows.\n", m - t));
276 /* Begin explore node q of the forest 19 */
280 for (l = 0; l < n; ++l) {
282 int del = p->cost[k][l] - s + col_inc[l];
284 if (del < slack[l]) {
291 DBG((p->dbg, LEVEL_1, "node %d: row %d == col %d -- row %d\n", t, row_mate[l], l, k));
292 unchosen_row[t++] = row_mate[l];
301 /* End explore node q of the forest 19 */
305 /* Begin introduce a new zero into the matrix 21 */
307 for (l = 0; l < n; ++l) {
308 if (slack[l] && slack[l] < s)
312 for (q = 0; q < t; ++q)
313 row_dec[unchosen_row[q]] += s;
315 for (l = 0; l < n; ++l) {
319 /* Begin look at a new zero 22 */
321 DBG((p->dbg, LEVEL_1, "Decreasing uncovered elements by %d produces zero at [%d, %d]\n", s, k, l));
322 if (row_mate[l] < 0) {
323 for (j = l + 1; j < n; ++j) {
331 DBG((p->dbg, LEVEL_1, "node %d: row %d == col %d -- row %d\n", t, row_mate[l], l, k));
332 unchosen_row[t++] = row_mate[l];
334 /* End look at a new zero 22 */
341 /* End introduce a new zero into the matrix 21 */
344 /* Begin update the matching 20 */
345 DBG((p->dbg, LEVEL_1, "Breakthrough at node %d of %d.\n", q, t));
351 DBG((p->dbg, LEVEL_1, "rematching col %d == row %d\n", l, k));
358 /* End update the matching 20 */
360 if (--unmatched == 0)
363 /* Begin get ready for another stage 17 */
365 for (l = 0; l < n; ++l) {
370 for (k = 0; k < m; ++k) {
371 if (col_mate[k] < 0) {
372 DBG((p->dbg, LEVEL_1, "node %d: unmatched row %d\n", t, k));
373 unchosen_row[t++] = k;
376 /* End get ready for another stage 17 */
380 /* Begin double check the solution 23 */
381 for (k = 0; k < m; ++k) {
382 for (l = 0; l < n; ++l) {
383 if (p->cost[k][l] < row_dec[k] - col_inc[l])
388 for (k = 0; k < m; ++k) {
390 if (l < 0 || p->cost[k][l] != row_dec[k] - col_inc[l])
394 for (k = l = 0; l < n; ++l) {
401 /* End double check the solution 23 */
403 /* End Hungarian algorithm 18 */
405 /* collect the assigned values */
406 for (i = 0; i < m; ++i) {
407 if (cost_threshold > 0 && p->cost[i][col_mate[i]] >= cost_threshold)
408 assignment[i] = -1; /* remove matching having cost > threshold */
410 assignment[i] = col_mate[i];
413 /* In case of normal matching: remove impossible ones */
414 if (p->match_type == HUNGARIAN_MATCH_NORMAL) {
415 for (i = 0; i < m; ++i) {
416 if (bitset_is_set(p->missing_left, i) || bitset_is_set(p->missing_right, col_mate[i]))
421 for (k = 0; k < m; ++k) {
422 for (l = 0; l < n; ++l) {
423 p->cost[k][l] = p->cost[k][l] - row_dec[k] + col_inc[l];
427 for (i = 0; i < m; ++i)
430 for (i = 0; i < n; ++i)
433 DBG((p->dbg, LEVEL_1, "Cost is %d\n", cost));
444 if (final_cost != NULL)