void bipartite_matching(bipartite_t const *const gr, int *const matching) { bitset_t *const matched_left = bitset_alloca(gr->n_left); bitset_t *const matched_right = bitset_alloca(gr->n_right); memset(matching, -1, gr->n_left * sizeof(int)); while (apply_alternating_path(gr, matching, matched_left, matched_right)) { } }
static int apply_alternating_path(bipartite_t const *const gr, int *const matching, bitset_t *const matched_left, bitset_t *const matched_right) { bool done_something = false; bitset_t *const tmp = bitset_alloca(gr->n_right); for (unsigned left = 0; left < gr->n_left; ++left) { bitset_t *left_adj = gr->adj[left]; bitset_copy(tmp, left_adj); if (matching[left] >= 0) { int old_right = matching[left]; /* Check of all neighbors of the left node are already matched. * We cannot improve this edge then. */ if (bitset_contains(left_adj, matched_right)) continue; bitset_andnot(tmp, matched_right); unsigned right = bitset_next_set(tmp, 0); assert(right != ~0u); /* We have to find another left node which has the old right one as * a neighbor. This node must not be part of a matching */ unsigned i; for (i = 0; i < gr->n_left; ++i) if (i != left && bitset_is_set(gr->adj[i], old_right) && !bitset_is_set(matched_left, i)) break; /* If no such node can be found, exit. */ if (i >= gr->n_left) continue; /* Else, we can improve this edge. */ matching[left] = right; matching[i] = old_right; bitset_set(matched_left, i); bitset_set(matched_right, right); done_something = true; } else { /* We have to create a new single edge */ assert(!bitset_is_set(matched_left, left)); bitset_andnot(tmp, matched_right); if (bitset_is_empty(tmp)) continue; unsigned right = bitset_next_set(tmp, 0); assert(!bitset_is_set(matched_right, right)); matching[left] = right; bitset_set(matched_left, left); bitset_set(matched_right, right); done_something = true; } } return done_something; }
/* kill all double-entries (Mij and Mji are set) */ matrix_foreach(m, e) { double t_val; assert(e->row != e->col && "Root has itself as arg. Ok. But the arg (=root) will always have the same color as root"); t_val = matrix_get(m, e->col, e->row); if (fabs(t_val) > 1e-10) { matrix_set(m, e->col, e->row, 0); matrix_set(m, e->row, e->col, e->val + t_val); } } c = ALLOCAN(int, size); redo = 1; fullrow = bitset_alloca(size); /* kill 'all' rows containing only 1 entry */ while (redo) { redo = 0; /* count elements in rows */ memset(c, 0, size * sizeof(*c)); matrix_foreach(m, e) c[e->row]++; for (i = 0; i<size; ++i) if (c[i] == 1 && ! bitset_is_set(fullrow, i)) { redo = 1; /* if the other row isn't empty move the e in there, else fill e's row */ matrix_elem_t const *const e = matrix_row_first(m, i);