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 ********************************************************************/
43 #include "hungarian.h"
45 #define INF (0x7FFFFFFF)
47 struct _hungarian_problem_t {
48 int num_rows; /**< number of rows */
49 int num_cols; /**< number of columns */
50 int **cost; /**< the cost matrix */
51 int width; /**< the width for cost matrix dumper */
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, long 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) {
81 hungarian_dump_f(stderr, p->cost, p->num_rows, p->num_cols, p->width);
85 * Create the object and allocate memory for the data structures.
87 hungarian_problem_t *hungarian_new(int rows, int cols, int width, int match_type) {
89 hungarian_problem_t *p = xmalloc(sizeof(*p));
91 memset(p, 0, sizeof(p[0]));
93 FIRM_DBG_REGISTER(p->dbg, "firm.hungarian");
96 Is the number of cols not equal to number of rows ?
97 If yes, expand with 0 - cols / 0 - cols
99 rows = MAX(cols, rows);
102 obstack_init(&p->obst);
107 p->match_type = match_type;
110 In case of normal matching, we have to keep
111 track of nodes without edges to kill them in
112 the assignment later.
114 if (match_type == HUNGARIAN_MATCH_NORMAL) {
115 p->missing_left = bitset_obstack_alloc(&p->obst, rows);
116 p->missing_right = bitset_obstack_alloc(&p->obst, cols);
117 bitset_set_all(p->missing_left);
118 bitset_set_all(p->missing_right);
121 /* allocate space for cost matrix */
122 p->cost = (int **)get_init_mem(&p->obst, rows * sizeof(p->cost[0]));
123 for (i = 0; i < p->num_rows; i++)
124 p->cost[i] = (int *)get_init_mem(&p->obst, cols * sizeof(p->cost[0][0]));
130 * Prepare the cost matrix.
132 void hungarian_prepare_cost_matrix(hungarian_problem_t *p, int mode) {
135 if (mode == HUNGARIAN_MODE_MAXIMIZE_UTIL) {
136 for (i = 0; i < p->num_rows; i++) {
137 for (j = 0; j < p->num_cols; j++) {
138 p->cost[i][j] = p->max_cost - p->cost[i][j];
142 else if (mode == HUNGARIAN_MODE_MINIMIZE_COST) {
146 fprintf(stderr, "Unknown mode. Mode was set to HUNGARIAN_MODE_MINIMIZE_COST.\n");
150 * Set cost[left][right] to cost.
152 void hungarian_add(hungarian_problem_t *p, int left, int right, int cost) {
153 assert(p->num_rows > left && "Invalid row selected.");
154 assert(p->num_cols > right && "Invalid column selected.");
157 p->cost[left][right] = cost;
158 p->max_cost = MAX(p->max_cost, cost);
160 if (p->match_type == HUNGARIAN_MATCH_NORMAL) {
161 bitset_clear(p->missing_left, left);
162 bitset_clear(p->missing_right, right);
167 * Set cost[left][right] to 0.
169 void hungarian_remv(hungarian_problem_t *p, int left, int right) {
170 assert(p->num_rows > left && "Invalid row selected.");
171 assert(p->num_cols > right && "Invalid column selected.");
173 p->cost[left][right] = 0;
175 if (p->match_type == HUNGARIAN_MATCH_NORMAL) {
176 bitset_set(p->missing_left, left);
177 bitset_set(p->missing_right, right);
182 * Frees all allocated memory.
184 void hungarian_free(hungarian_problem_t* p) {
185 obstack_free(&p->obst, NULL);
192 int hungarian_solve(hungarian_problem_t* p, int *assignment, int *final_cost, int cost_threshold) {
193 int i, j, m, n, k, l, s, t, q, unmatched, cost;
207 col_mate = xcalloc(p->num_rows, sizeof(col_mate[0]));
208 unchosen_row = xcalloc(p->num_rows, sizeof(unchosen_row[0]));
209 row_dec = xcalloc(p->num_rows, sizeof(row_dec[0]));
210 slack_row = xcalloc(p->num_rows, sizeof(slack_row[0]));
212 row_mate = xcalloc(p->num_cols, sizeof(row_mate[0]));
213 parent_row = xcalloc(p->num_cols, sizeof(parent_row[0]));
214 col_inc = xcalloc(p->num_cols, sizeof(col_inc[0]));
215 slack = xcalloc(p->num_cols, sizeof(slack[0]));
217 memset(assignment, -1, m * sizeof(assignment[0]));
219 /* Begin subtract column minima in order to start with lots of zeros 12 */
220 DBG((p->dbg, LEVEL_1, "Using heuristic\n"));
222 for (l = 0; l < n; ++l) {
225 for (k = 1; k < m; ++k) {
226 if (p->cost[k][l] < s)
233 for (k = 0; k < m; ++k)
237 /* End subtract column minima in order to start with lots of zeros 12 */
239 /* Begin initial state 16 */
241 for (l = 0; l < n; ++l) {
248 for (k = 0; k < m; ++k) {
251 for (l = 1; l < n; ++l) {
252 if (p->cost[k][l] < s)
258 for (l = 0; l < n; ++l) {
259 if (s == p->cost[k][l] && row_mate[l] < 0) {
262 DBG((p->dbg, LEVEL_1, "matching col %d == row %d\n", l, k));
268 DBG((p->dbg, LEVEL_1, "node %d: unmatched row %d\n", t, k));
269 unchosen_row[t++] = k;
272 /* End initial state 16 */
274 /* Begin Hungarian algorithm 18 */
280 DBG((p->dbg, LEVEL_1, "Matched %d rows.\n", m - t));
285 /* Begin explore node q of the forest 19 */
289 for (l = 0; l < n; ++l) {
291 int del = p->cost[k][l] - s + col_inc[l];
293 if (del < slack[l]) {
300 DBG((p->dbg, LEVEL_1, "node %d: row %d == col %d -- row %d\n", t, row_mate[l], l, k));
301 unchosen_row[t++] = row_mate[l];
310 /* End explore node q of the forest 19 */
314 /* Begin introduce a new zero into the matrix 21 */
316 for (l = 0; l < n; ++l) {
317 if (slack[l] && slack[l] < s)
321 for (q = 0; q < t; ++q)
322 row_dec[unchosen_row[q]] += s;
324 for (l = 0; l < n; ++l) {
328 /* Begin look at a new zero 22 */
330 DBG((p->dbg, LEVEL_1, "Decreasing uncovered elements by %d produces zero at [%d, %d]\n", s, k, l));
331 if (row_mate[l] < 0) {
332 for (j = l + 1; j < n; ++j) {
340 DBG((p->dbg, LEVEL_1, "node %d: row %d == col %d -- row %d\n", t, row_mate[l], l, k));
341 unchosen_row[t++] = row_mate[l];
343 /* End look at a new zero 22 */
350 /* End introduce a new zero into the matrix 21 */
353 /* Begin update the matching 20 */
354 DBG((p->dbg, LEVEL_1, "Breakthrough at node %d of %d.\n", q, t));
360 DBG((p->dbg, LEVEL_1, "rematching col %d == row %d\n", l, k));
367 /* End update the matching 20 */
369 if (--unmatched == 0)
372 /* Begin get ready for another stage 17 */
374 for (l = 0; l < n; ++l) {
379 for (k = 0; k < m; ++k) {
380 if (col_mate[k] < 0) {
381 DBG((p->dbg, LEVEL_1, "node %d: unmatched row %d\n", t, k));
382 unchosen_row[t++] = k;
385 /* End get ready for another stage 17 */
389 /* Begin double check the solution 23 */
390 for (k = 0; k < m; ++k) {
391 for (l = 0; l < n; ++l) {
392 if (p->cost[k][l] < row_dec[k] - col_inc[l])
397 for (k = 0; k < m; ++k) {
399 if (l < 0 || p->cost[k][l] != row_dec[k] - col_inc[l])
403 for (k = l = 0; l < n; ++l) {
410 /* End double check the solution 23 */
412 /* End Hungarian algorithm 18 */
414 /* collect the assigned values */
415 for (i = 0; i < m; ++i) {
416 if (cost_threshold > 0 && p->cost[i][col_mate[i]] >= cost_threshold)
417 assignment[i] = -1; /* remove matching having cost > threshold */
419 assignment[i] = col_mate[i];
422 /* In case of normal matching: remove impossible ones */
423 if (p->match_type == HUNGARIAN_MATCH_NORMAL) {
424 for (i = 0; i < m; ++i) {
425 if (bitset_is_set(p->missing_left, i) || bitset_is_set(p->missing_right, col_mate[i]))
430 for (k = 0; k < m; ++k) {
431 for (l = 0; l < n; ++l) {
432 p->cost[k][l] = p->cost[k][l] - row_dec[k] + col_inc[l];
436 for (i = 0; i < m; ++i)
439 for (i = 0; i < n; ++i)
442 DBG((p->dbg, LEVEL_1, "Cost is %d\n", cost));