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 inline void *get_init_mem(struct obstack *obst, size_t sz) {
61 void *p = obstack_alloc(obst, sz);
66 static void hungarian_dump_f(FILE *f, int **C, int rows, int cols, int width) {
70 for (i = 0; i < rows; i++) {
72 for (j = 0; j < cols; j++) {
73 fprintf(f, "%*d", width, C[i][j]);
80 void hungarian_print_costmatrix(hungarian_problem_t *p, int width) {
81 hungarian_dump_f(stderr, p->cost, p->num_rows, p->num_cols, width);
85 * Create the object and allocate memory for the data structures.
87 hungarian_problem_t *hungarian_new(int rows, int cols, int match_type) {
89 hungarian_problem_t *p = XMALLOCZ(hungarian_problem_t);
91 FIRM_DBG_REGISTER(p->dbg, "firm.hungarian");
94 Is the number of cols not equal to number of rows ?
95 If yes, expand with 0 - cols / 0 - cols
97 rows = MAX(cols, rows);
100 obstack_init(&p->obst);
104 p->match_type = match_type;
107 In case of normal matching, we have to keep
108 track of nodes without edges to kill them in
109 the assignment later.
111 if (match_type == HUNGARIAN_MATCH_NORMAL) {
112 p->missing_left = bitset_obstack_alloc(&p->obst, rows);
113 p->missing_right = bitset_obstack_alloc(&p->obst, cols);
114 bitset_set_all(p->missing_left);
115 bitset_set_all(p->missing_right);
118 /* allocate space for cost matrix */
119 p->cost = (int **)get_init_mem(&p->obst, rows * sizeof(p->cost[0]));
120 for (i = 0; i < p->num_rows; i++)
121 p->cost[i] = (int *)get_init_mem(&p->obst, cols * sizeof(p->cost[0][0]));
127 * Prepare the cost matrix.
129 void hungarian_prepare_cost_matrix(hungarian_problem_t *p, int mode) {
132 if (mode == HUNGARIAN_MODE_MAXIMIZE_UTIL) {
133 for (i = 0; i < p->num_rows; i++) {
134 for (j = 0; j < p->num_cols; j++) {
135 p->cost[i][j] = p->max_cost - p->cost[i][j];
139 else if (mode == HUNGARIAN_MODE_MINIMIZE_COST) {
143 fprintf(stderr, "Unknown mode. Mode was set to HUNGARIAN_MODE_MINIMIZE_COST.\n");
147 * Set cost[left][right] to cost.
149 void hungarian_add(hungarian_problem_t *p, int left, int right, int cost) {
150 assert(p->num_rows > left && "Invalid row selected.");
151 assert(p->num_cols > right && "Invalid column selected.");
154 p->cost[left][right] = cost;
155 p->max_cost = MAX(p->max_cost, cost);
157 if (p->match_type == HUNGARIAN_MATCH_NORMAL) {
158 bitset_clear(p->missing_left, left);
159 bitset_clear(p->missing_right, right);
164 * Set cost[left][right] to 0.
166 void hungarian_remv(hungarian_problem_t *p, int left, int right) {
167 assert(p->num_rows > left && "Invalid row selected.");
168 assert(p->num_cols > right && "Invalid column selected.");
170 p->cost[left][right] = 0;
172 if (p->match_type == HUNGARIAN_MATCH_NORMAL) {
173 bitset_set(p->missing_left, left);
174 bitset_set(p->missing_right, right);
179 * Frees all allocated memory.
181 void hungarian_free(hungarian_problem_t* p) {
182 obstack_free(&p->obst, NULL);
189 int hungarian_solve(hungarian_problem_t* p, int *assignment, int *final_cost, int cost_threshold) {
190 int i, j, m, n, k, l, s, t, q, unmatched, cost;
204 col_mate = XMALLOCNZ(int, p->num_rows);
205 unchosen_row = XMALLOCNZ(int, p->num_rows);
206 row_dec = XMALLOCNZ(int, p->num_rows);
207 slack_row = XMALLOCNZ(int, p->num_rows);
209 row_mate = XMALLOCNZ(int, p->num_cols);
210 parent_row = XMALLOCNZ(int, p->num_cols);
211 col_inc = XMALLOCNZ(int, p->num_cols);
212 slack = XMALLOCNZ(int, p->num_cols);
214 memset(assignment, -1, m * sizeof(assignment[0]));
216 /* Begin subtract column minima in order to start with lots of zeros 12 */
217 DBG((p->dbg, LEVEL_1, "Using heuristic\n"));
219 for (l = 0; l < n; ++l) {
222 for (k = 1; k < m; ++k) {
223 if (p->cost[k][l] < s)
230 for (k = 0; k < m; ++k)
234 /* End subtract column minima in order to start with lots of zeros 12 */
236 /* Begin initial state 16 */
238 for (l = 0; l < n; ++l) {
245 for (k = 0; k < m; ++k) {
248 for (l = 1; l < n; ++l) {
249 if (p->cost[k][l] < s)
255 for (l = 0; l < n; ++l) {
256 if (s == p->cost[k][l] && row_mate[l] < 0) {
259 DBG((p->dbg, LEVEL_1, "matching col %d == row %d\n", l, k));
265 DBG((p->dbg, LEVEL_1, "node %d: unmatched row %d\n", t, k));
266 unchosen_row[t++] = k;
269 /* End initial state 16 */
271 /* Begin Hungarian algorithm 18 */
277 DBG((p->dbg, LEVEL_1, "Matched %d rows.\n", m - t));
282 /* Begin explore node q of the forest 19 */
286 for (l = 0; l < n; ++l) {
288 int del = p->cost[k][l] - s + col_inc[l];
290 if (del < slack[l]) {
297 DBG((p->dbg, LEVEL_1, "node %d: row %d == col %d -- row %d\n", t, row_mate[l], l, k));
298 unchosen_row[t++] = row_mate[l];
307 /* End explore node q of the forest 19 */
311 /* Begin introduce a new zero into the matrix 21 */
313 for (l = 0; l < n; ++l) {
314 if (slack[l] && slack[l] < s)
318 for (q = 0; q < t; ++q)
319 row_dec[unchosen_row[q]] += s;
321 for (l = 0; l < n; ++l) {
325 /* Begin look at a new zero 22 */
327 DBG((p->dbg, LEVEL_1, "Decreasing uncovered elements by %d produces zero at [%d, %d]\n", s, k, l));
328 if (row_mate[l] < 0) {
329 for (j = l + 1; j < n; ++j) {
337 DBG((p->dbg, LEVEL_1, "node %d: row %d == col %d -- row %d\n", t, row_mate[l], l, k));
338 unchosen_row[t++] = row_mate[l];
340 /* End look at a new zero 22 */
347 /* End introduce a new zero into the matrix 21 */
350 /* Begin update the matching 20 */
351 DBG((p->dbg, LEVEL_1, "Breakthrough at node %d of %d.\n", q, t));
357 DBG((p->dbg, LEVEL_1, "rematching col %d == row %d\n", l, k));
364 /* End update the matching 20 */
366 if (--unmatched == 0)
369 /* Begin get ready for another stage 17 */
371 for (l = 0; l < n; ++l) {
376 for (k = 0; k < m; ++k) {
377 if (col_mate[k] < 0) {
378 DBG((p->dbg, LEVEL_1, "node %d: unmatched row %d\n", t, k));
379 unchosen_row[t++] = k;
382 /* End get ready for another stage 17 */
386 /* Begin double check the solution 23 */
387 for (k = 0; k < m; ++k) {
388 for (l = 0; l < n; ++l) {
389 if (p->cost[k][l] < row_dec[k] - col_inc[l])
394 for (k = 0; k < m; ++k) {
396 if (l < 0 || p->cost[k][l] != row_dec[k] - col_inc[l])
400 for (k = l = 0; l < n; ++l) {
407 /* End double check the solution 23 */
409 /* End Hungarian algorithm 18 */
411 /* collect the assigned values */
412 for (i = 0; i < m; ++i) {
413 if (cost_threshold > 0 && p->cost[i][col_mate[i]] >= cost_threshold)
414 assignment[i] = -1; /* remove matching having cost > threshold */
416 assignment[i] = col_mate[i];
419 /* In case of normal matching: remove impossible ones */
420 if (p->match_type == HUNGARIAN_MATCH_NORMAL) {
421 for (i = 0; i < m; ++i) {
422 if (bitset_is_set(p->missing_left, i) || bitset_is_set(p->missing_right, col_mate[i]))
427 for (k = 0; k < m; ++k) {
428 for (l = 0; l < n; ++l) {
429 p->cost[k][l] = p->cost[k][l] - row_dec[k] + col_inc[l];
433 for (i = 0; i < m; ++i)
436 for (i = 0; i < n; ++i)
439 DBG((p->dbg, LEVEL_1, "Cost is %d\n", cost));