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.
46 #include "hungarian.h"
48 #define INF (0x7FFFFFFF)
50 struct _hungarian_problem_t {
51 int num_rows; /**< number of rows */
52 int num_cols; /**< number of columns */
53 int **cost; /**< the cost matrix */
54 int width; /**< the width for cost matrix dumper */
55 int max_cost; /**< the maximal costs in the matrix */
56 int match_type; /**< PERFECT or NORMAL matching */
57 bitset_t *missing_left; /**< left side nodes having no edge to the right side */
58 bitset_t *missing_right; /**< right side nodes having no edge to the left side */
60 DEBUG_ONLY(firm_dbg_module_t *dbg);
63 static INLINE void *get_init_mem(struct obstack *obst, long sz) {
64 void *p = obstack_alloc(obst, sz);
69 static void hungarian_dump_f(FILE *f, int **C, int rows, int cols, int width) {
73 for (i = 0; i < rows; i++) {
75 for (j = 0; j < cols; j++) {
76 fprintf(f, "%*d", width, C[i][j]);
83 void hungarian_print_costmatrix(hungarian_problem_t *p) {
84 hungarian_dump_f(stderr, p->cost, p->num_rows, p->num_cols, p->width);
88 * Create the object and allocate memory for the data structures.
90 hungarian_problem_t *hungarian_new(int rows, int cols, int width, int match_type) {
92 hungarian_problem_t *p = xmalloc(sizeof(*p));
94 memset(p, 0, sizeof(p[0]));
96 FIRM_DBG_REGISTER(p->dbg, "firm.hungarian");
99 Is the number of cols not equal to number of rows ?
100 If yes, expand with 0 - cols / 0 - cols
102 rows = MAX(cols, rows);
105 obstack_init(&p->obst);
110 p->match_type = match_type;
113 In case of normal matching, we have to keep
114 track of nodes without edges to kill them in
115 the assignment later.
117 if (match_type == HUNGARIAN_MATCH_NORMAL) {
118 p->missing_left = bitset_obstack_alloc(&p->obst, rows);
119 p->missing_right = bitset_obstack_alloc(&p->obst, cols);
120 bitset_set_all(p->missing_left);
121 bitset_set_all(p->missing_right);
124 /* allocate space for cost matrix */
125 p->cost = (int **)get_init_mem(&p->obst, rows * sizeof(p->cost[0]));
126 for (i = 0; i < p->num_rows; i++)
127 p->cost[i] = (int *)get_init_mem(&p->obst, cols * sizeof(p->cost[0][0]));
133 * Prepare the cost matrix.
135 void hungarian_prepare_cost_matrix(hungarian_problem_t *p, int mode) {
138 if (mode == HUNGARIAN_MODE_MAXIMIZE_UTIL) {
139 for (i = 0; i < p->num_rows; i++) {
140 for (j = 0; j < p->num_cols; j++) {
141 p->cost[i][j] = p->max_cost - p->cost[i][j];
145 else if (mode == HUNGARIAN_MODE_MINIMIZE_COST) {
149 fprintf(stderr, "Unknown mode. Mode was set to HUNGARIAN_MODE_MINIMIZE_COST.\n");
153 * Set cost[left][right] to cost.
155 void hungarian_add(hungarian_problem_t *p, int left, int right, int cost) {
156 assert(p->num_rows > left && "Invalid row selected.");
157 assert(p->num_cols > right && "Invalid column selected.");
160 p->cost[left][right] = cost;
161 p->max_cost = MAX(p->max_cost, cost);
163 if (p->match_type == HUNGARIAN_MATCH_NORMAL) {
164 bitset_clear(p->missing_left, left);
165 bitset_clear(p->missing_right, right);
170 * Set cost[left][right] to 0.
172 void hungarian_remv(hungarian_problem_t *p, int left, int right) {
173 assert(p->num_rows > left && "Invalid row selected.");
174 assert(p->num_cols > right && "Invalid column selected.");
176 p->cost[left][right] = 0;
178 if (p->match_type == HUNGARIAN_MATCH_NORMAL) {
179 bitset_set(p->missing_left, left);
180 bitset_set(p->missing_right, right);
185 * Frees all allocated memory.
187 void hungarian_free(hungarian_problem_t* p) {
188 obstack_free(&p->obst, NULL);
195 int hungarian_solve(hungarian_problem_t* p, int *assignment, int *final_cost, int cost_threshold) {
196 int i, j, m, n, k, l, s, t, q, unmatched, cost;
210 col_mate = xcalloc(p->num_rows, sizeof(col_mate[0]));
211 unchosen_row = xcalloc(p->num_rows, sizeof(unchosen_row[0]));
212 row_dec = xcalloc(p->num_rows, sizeof(row_dec[0]));
213 slack_row = xcalloc(p->num_rows, sizeof(slack_row[0]));
215 row_mate = xcalloc(p->num_cols, sizeof(row_mate[0]));
216 parent_row = xcalloc(p->num_cols, sizeof(parent_row[0]));
217 col_inc = xcalloc(p->num_cols, sizeof(col_inc[0]));
218 slack = xcalloc(p->num_cols, sizeof(slack[0]));
220 memset(assignment, -1, m * sizeof(assignment[0]));
222 /* Begin subtract column minima in order to start with lots of zeros 12 */
223 DBG((p->dbg, LEVEL_1, "Using heuristic\n"));
225 for (l = 0; l < n; ++l) {
228 for (k = 1; k < m; ++k) {
229 if (p->cost[k][l] < s)
236 for (k = 0; k < m; ++k)
240 /* End subtract column minima in order to start with lots of zeros 12 */
242 /* Begin initial state 16 */
244 for (l = 0; l < n; ++l) {
251 for (k = 0; k < m; ++k) {
254 for (l = 1; l < n; ++l) {
255 if (p->cost[k][l] < s)
261 for (l = 0; l < n; ++l) {
262 if (s == p->cost[k][l] && row_mate[l] < 0) {
265 DBG((p->dbg, LEVEL_1, "matching col %d == row %d\n", l, k));
271 DBG((p->dbg, LEVEL_1, "node %d: unmatched row %d\n", t, k));
272 unchosen_row[t++] = k;
275 /* End initial state 16 */
277 /* Begin Hungarian algorithm 18 */
283 DBG((p->dbg, LEVEL_1, "Matched %d rows.\n", m - t));
288 /* Begin explore node q of the forest 19 */
292 for (l = 0; l < n; ++l) {
294 int del = p->cost[k][l] - s + col_inc[l];
296 if (del < slack[l]) {
303 DBG((p->dbg, LEVEL_1, "node %d: row %d == col %d -- row %d\n", t, row_mate[l], l, k));
304 unchosen_row[t++] = row_mate[l];
313 /* End explore node q of the forest 19 */
317 /* Begin introduce a new zero into the matrix 21 */
319 for (l = 0; l < n; ++l) {
320 if (slack[l] && slack[l] < s)
324 for (q = 0; q < t; ++q)
325 row_dec[unchosen_row[q]] += s;
327 for (l = 0; l < n; ++l) {
331 /* Begin look at a new zero 22 */
333 DBG((p->dbg, LEVEL_1, "Decreasing uncovered elements by %d produces zero at [%d, %d]\n", s, k, l));
334 if (row_mate[l] < 0) {
335 for (j = l + 1; j < n; ++j) {
343 DBG((p->dbg, LEVEL_1, "node %d: row %d == col %d -- row %d\n", t, row_mate[l], l, k));
344 unchosen_row[t++] = row_mate[l];
346 /* End look at a new zero 22 */
353 /* End introduce a new zero into the matrix 21 */
356 /* Begin update the matching 20 */
357 DBG((p->dbg, LEVEL_1, "Breakthrough at node %d of %d.\n", q, t));
363 DBG((p->dbg, LEVEL_1, "rematching col %d == row %d\n", l, k));
370 /* End update the matching 20 */
372 if (--unmatched == 0)
375 /* Begin get ready for another stage 17 */
377 for (l = 0; l < n; ++l) {
382 for (k = 0; k < m; ++k) {
383 if (col_mate[k] < 0) {
384 DBG((p->dbg, LEVEL_1, "node %d: unmatched row %d\n", t, k));
385 unchosen_row[t++] = k;
388 /* End get ready for another stage 17 */
392 /* Begin double check the solution 23 */
393 for (k = 0; k < m; ++k) {
394 for (l = 0; l < n; ++l) {
395 if (p->cost[k][l] < row_dec[k] - col_inc[l])
400 for (k = 0; k < m; ++k) {
402 if (l < 0 || p->cost[k][l] != row_dec[k] - col_inc[l])
406 for (k = l = 0; l < n; ++l) {
413 /* End double check the solution 23 */
415 /* End Hungarian algorithm 18 */
417 /* collect the assigned values */
418 for (i = 0; i < m; ++i) {
419 if (cost_threshold > 0 && p->cost[i][col_mate[i]] >= cost_threshold)
420 assignment[i] = -1; /* remove matching having cost > threshold */
422 assignment[i] = col_mate[i];
425 /* In case of normal matching: remove impossible ones */
426 if (p->match_type == HUNGARIAN_MATCH_NORMAL) {
427 for (i = 0; i < m; ++i) {
428 if (bitset_is_set(p->missing_left, i) || bitset_is_set(p->missing_right, col_mate[i]))
433 for (k = 0; k < m; ++k) {
434 for (l = 0; l < n; ++l) {
435 p->cost[k][l] = p->cost[k][l] - row_dec[k] + col_inc[l];
439 for (i = 0; i < m; ++i)
442 for (i = 0; i < n; ++i)
445 DBG((p->dbg, LEVEL_1, "Cost is %d\n", cost));