Commit e0d42011 authored by Matthias Braun's avatar Matthias Braun
Browse files

bipartite: Cleanup

parent 731fe730
...@@ -29,28 +29,28 @@ typedef struct bipartite_t bipartite_t; ...@@ -29,28 +29,28 @@ typedef struct bipartite_t bipartite_t;
/** Create new bipartite matching problem with @p n_left elements on left side /** Create new bipartite matching problem with @p n_left elements on left side
* and @p n_right elements on right side */ * and @p n_right elements on right side */
FIRM_API bipartite_t *bipartite_new(int n_left, int n_right); FIRM_API bipartite_t *bipartite_new(unsigned n_left, unsigned n_right);
/** Free memory occupied by bipartite matching problem */ /** Free memory occupied by bipartite matching problem */
FIRM_API void bipartite_free(bipartite_t *gr); FIRM_API void bipartite_free(bipartite_t *gr);
/** Add edge from @p i (on the left side) to @p j (on the right side) */ /** Add edge from @p i (on the left side) to @p j (on the right side) */
FIRM_API void bipartite_add(bipartite_t *gr, int i, int j); FIRM_API void bipartite_add(bipartite_t *gr, unsigned i, unsigned j);
/** Remove edge from @p i (on the left side) to @p j (on the right side) */ /** Remove edge from @p i (on the left side) to @p j (on the right side) */
FIRM_API void bipartite_remv(bipartite_t *gr, int i, int j); FIRM_API void bipartite_remv(bipartite_t *gr, unsigned i, unsigned j);
/** Return 1 if edge from @p i (on the left side) to @p j (on the right side) /** Return 1 if edge from @p i (on the left side) to @p j (on the right side)
* exists, 0 otherwise */ * exists, 0 otherwise */
FIRM_API int bipartite_adj(const bipartite_t *gr, int i, int j); FIRM_API int bipartite_adj(bipartite_t const *gr, unsigned i, unsigned j);
/** Solve bipartite matching problem */ /** Solve bipartite matching problem */
FIRM_API void bipartite_matching(const bipartite_t *gr, int *matching); FIRM_API void bipartite_matching(bipartite_t const *gr, int *matching);
/** /**
* Dumps a bipartite graph to a file stream. * Dumps a bipartite graph to a file stream.
*/ */
FIRM_API void bipartite_dump_f(FILE *f, const bipartite_t *gr); FIRM_API void bipartite_dump_f(FILE *f, bipartite_t const *gr);
/** /**
* Dumps a bipartite graph to file name. * Dumps a bipartite graph to file name.
*/ */
FIRM_API void bipartite_dump(const char *name, const bipartite_t *gr); FIRM_API void bipartite_dump(char const *name, bipartite_t const *gr);
/** @} */ /** @} */
......
...@@ -16,61 +16,59 @@ ...@@ -16,61 +16,59 @@
#include "xmalloc.h" #include "xmalloc.h"
struct bipartite_t { struct bipartite_t {
int n_left, n_right; unsigned n_left;
bitset_t *adj[1]; unsigned n_right;
bitset_t *adj[];
}; };
bipartite_t *bipartite_new(int n_left, int n_right) bipartite_t *bipartite_new(unsigned const n_left, unsigned const n_right)
{ {
bipartite_t *gr = XMALLOCFZ(bipartite_t, adj, n_left); bipartite_t *gr = XMALLOCFZ(bipartite_t, adj, n_left);
int i; gr->n_left = n_left;
gr->n_left = n_left;
gr->n_right = n_right; gr->n_right = n_right;
for (i = 0; i < n_left; ++i) for (unsigned i = 0; i < n_left; ++i)
gr->adj[i] = bitset_malloc(n_right); gr->adj[i] = bitset_malloc(n_right);
return gr; return gr;
} }
void bipartite_free(bipartite_t *gr) void bipartite_free(bipartite_t *const gr)
{ {
int i; for (unsigned i = 0; i < gr->n_left; ++i)
for (i = 0; i < gr->n_left; ++i)
free(gr->adj[i]); free(gr->adj[i]);
free(gr); free(gr);
} }
void bipartite_add(bipartite_t *gr, int i, int j) void bipartite_add(bipartite_t *const gr, unsigned const i, unsigned const j)
{ {
assert(i < gr->n_left && j < gr->n_right); assert(i < gr->n_left && j < gr->n_right);
bitset_set(gr->adj[i], j); bitset_set(gr->adj[i], j);
} }
void bipartite_remv(bipartite_t *gr, int i, int j) void bipartite_remv(bipartite_t *const gr, unsigned const i, unsigned const j)
{ {
assert(i < gr->n_left && j < gr->n_right); assert(i < gr->n_left && j < gr->n_right);
bitset_clear(gr->adj[i], j); bitset_clear(gr->adj[i], j);
} }
int bipartite_adj(const bipartite_t *gr, int i, int j) int bipartite_adj(bipartite_t const *const gr, unsigned const i,
unsigned const j)
{ {
assert(i < gr->n_left && j < gr->n_right); assert(i < gr->n_left && j < gr->n_right);
return bitset_is_set(gr->adj[i], j); return bitset_is_set(gr->adj[i], j);
} }
static int apply_alternating_path(const bipartite_t *gr, int *matching, static int apply_alternating_path(bipartite_t const *const gr,
bitset_t *matched_left, bitset_t *matched_right) int *const matching,
bitset_t *const matched_left,
bitset_t *const matched_right)
{ {
int left, right; bool done_something = false;
int done_something = 0; bitset_t *const tmp = bitset_alloca(gr->n_right);
bitset_t *tmp = bitset_alloca(gr->n_right);
for (left = 0; left < gr->n_left; ++left) { for (unsigned left = 0; left < gr->n_left; ++left) {
bitset_t *left_adj = gr->adj[left]; bitset_t *left_adj = gr->adj[left];
int i;
bitset_copy(tmp, left_adj); bitset_copy(tmp, left_adj);
if (matching[left] >= 0) { if (matching[left] >= 0) {
...@@ -82,14 +80,12 @@ static int apply_alternating_path(const bipartite_t *gr, int *matching, ...@@ -82,14 +80,12 @@ static int apply_alternating_path(const bipartite_t *gr, int *matching,
continue; continue;
bitset_andnot(tmp, matched_right); bitset_andnot(tmp, matched_right);
right = bitset_next_set(tmp, 0); unsigned right = bitset_next_set(tmp, 0);
assert(right != ~0u);
assert(right != -1);
/* /* We have to find another left node which has the old right one as
We have to find another left node which has the old right one as a neighbor. * a neighbor. This node must not be part of a matching */
This node must not be part of a matching unsigned i;
*/
for (i = 0; i < gr->n_left; ++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)) if (i != left && bitset_is_set(gr->adj[i], old_right) && !bitset_is_set(matched_left, i))
break; break;
...@@ -103,7 +99,7 @@ static int apply_alternating_path(const bipartite_t *gr, int *matching, ...@@ -103,7 +99,7 @@ static int apply_alternating_path(const bipartite_t *gr, int *matching,
matching[i] = old_right; matching[i] = old_right;
bitset_set(matched_left, i); bitset_set(matched_left, i);
bitset_set(matched_right, right); bitset_set(matched_right, right);
done_something = 1; done_something = true;
} else { } else {
/* We have to create a new single edge */ /* We have to create a new single edge */
assert(!bitset_is_set(matched_left, left)); assert(!bitset_is_set(matched_left, left));
...@@ -112,44 +108,41 @@ static int apply_alternating_path(const bipartite_t *gr, int *matching, ...@@ -112,44 +108,41 @@ static int apply_alternating_path(const bipartite_t *gr, int *matching,
if (bitset_is_empty(tmp)) if (bitset_is_empty(tmp))
continue; continue;
right = bitset_next_set(tmp, 0); unsigned right = bitset_next_set(tmp, 0);
assert(!bitset_is_set(matched_right, right)); assert(!bitset_is_set(matched_right, right));
matching[left] = right; matching[left] = right;
bitset_set(matched_left, left); bitset_set(matched_left, left);
bitset_set(matched_right, right); bitset_set(matched_right, right);
done_something = 1; done_something = true;
} }
} }
return done_something; return done_something;
} }
void bipartite_matching(const bipartite_t *gr, int *matching) void bipartite_matching(bipartite_t const *const gr, int *const matching)
{ {
bitset_t *matched_left = bitset_alloca(gr->n_left); bitset_t *const matched_left = bitset_alloca(gr->n_left);
bitset_t *matched_right = bitset_alloca(gr->n_right); bitset_t *const matched_right = bitset_alloca(gr->n_right);
memset(matching, -1, gr->n_left * sizeof(int)); memset(matching, -1, gr->n_left * sizeof(int));
while (apply_alternating_path(gr, matching, matched_left, matched_right)) { while (apply_alternating_path(gr, matching, matched_left, matched_right)) {
} }
} }
void bipartite_dump_f(FILE *f, const bipartite_t *gr) void bipartite_dump_f(FILE *const f, bipartite_t const *gr)
{ {
int i; for (unsigned i = 0; i < gr->n_left; ++i) {
for (i = 0; i < gr->n_left; ++i) {
fprintf(f, "%d: ", i); fprintf(f, "%d: ", i);
bitset_fprint(f, gr->adj[i]); bitset_fprint(f, gr->adj[i]);
fprintf(f, "\n"); fprintf(f, "\n");
} }
} }
void bipartite_dump(const char *name, const bipartite_t *gr) void bipartite_dump(const char *const name, bipartite_t const *gr)
{ {
FILE *f = fopen(name, "w"); FILE *const f = fopen(name, "w");
if (f != NULL) {
if (f) {
bipartite_dump_f(f, gr); bipartite_dump_f(f, gr);
fclose(f); fclose(f);
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment