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;
/** Create new bipartite matching problem with @p n_left elements on left 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 */
FIRM_API void bipartite_free(bipartite_t *gr);
/** 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) */
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)
* 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 */
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.
*/
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.
*/
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 @@
#include "xmalloc.h"
struct bipartite_t {
int n_left, n_right;
bitset_t *adj[1];
unsigned n_left;
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);
int i;
gr->n_left = n_left;
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);
return gr;
}
void bipartite_free(bipartite_t *gr)
void bipartite_free(bipartite_t *const gr)
{
int i;
for (i = 0; i < gr->n_left; ++i)
for (unsigned i = 0; i < gr->n_left; ++i)
free(gr->adj[i]);
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);
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);
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);
return bitset_is_set(gr->adj[i], j);
}
static int apply_alternating_path(const bipartite_t *gr, int *matching,
bitset_t *matched_left, bitset_t *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)
{
int left, right;
int done_something = 0;
bitset_t *tmp = bitset_alloca(gr->n_right);
bool done_something = false;
bitset_t *const 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];
int i;
bitset_copy(tmp, left_adj);
if (matching[left] >= 0) {
......@@ -82,14 +80,12 @@ static int apply_alternating_path(const bipartite_t *gr, int *matching,
continue;
bitset_andnot(tmp, matched_right);
right = bitset_next_set(tmp, 0);
assert(right != -1);
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
*/
/* 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;
......@@ -103,7 +99,7 @@ static int apply_alternating_path(const bipartite_t *gr, int *matching,
matching[i] = old_right;
bitset_set(matched_left, i);
bitset_set(matched_right, right);
done_something = 1;
done_something = true;
} else {
/* We have to create a new single edge */
assert(!bitset_is_set(matched_left, left));
......@@ -112,44 +108,41 @@ static int apply_alternating_path(const bipartite_t *gr, int *matching,
if (bitset_is_empty(tmp))
continue;
right = bitset_next_set(tmp, 0);
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 = 1;
done_something = true;
}
}
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 *matched_right = bitset_alloca(gr->n_right);
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)) {
}
}
void bipartite_dump_f(FILE *f, const bipartite_t *gr)
void bipartite_dump_f(FILE *const f, bipartite_t const *gr)
{
int i;
for (i = 0; i < gr->n_left; ++i) {
for (unsigned i = 0; i < gr->n_left; ++i) {
fprintf(f, "%d: ", i);
bitset_fprint(f, gr->adj[i]);
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");
if (f) {
FILE *const f = fopen(name, "w");
if (f != NULL) {
bipartite_dump_f(f, gr);
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