| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include <esl_config.h> |
| 10 |
|
| 11 |
#include <stdlib.h> |
| 12 |
#include <stdio.h> |
| 13 |
|
| 14 |
#include "easel.h" |
| 15 |
#include "esl_matrixops.h" |
| 16 |
#include "esl_vectorops.h" |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
int |
| 59 |
esl_graph_MaxBipartiteMatch(int **A, int M, int N, int ***opt_G, int *ret_nedges) |
| 60 |
{ |
| 61 |
int **G = NULL; |
| 62 |
int *Ga = NULL; |
| 63 |
int *Gz = NULL; |
| 64 |
int *parent1 = NULL; |
| 65 |
int *parent2 = NULL; |
| 66 |
int par0; |
| 67 |
int found_path; |
| 68 |
int done; |
| 69 |
int nedges = 0; |
| 70 |
int i,j; |
| 71 |
int status; |
| 72 |
|
| 73 |
|
| 74 |
if (( G = esl_mat_ICreate(M, N) ) == NULL) { status = eslEMEM; goto ERROR; } |
| 75 |
ESL_ALLOC(Ga, sizeof(int) * M); |
| 76 |
ESL_ALLOC(Gz, sizeof(int) * N); |
| 77 |
ESL_ALLOC(parent1, sizeof(int) * M); |
| 78 |
ESL_ALLOC(parent2, sizeof(int) * N); |
| 79 |
|
| 80 |
|
| 81 |
esl_vec_ISet(Ga, M, 0); |
| 82 |
esl_vec_ISet(Gz, N, 0); |
| 83 |
esl_mat_ISet(G, M, N, 0); |
| 84 |
|
| 85 |
|
| 86 |
while (1) |
| 87 |
{ |
| 88 |
found_path = FALSE; |
| 89 |
esl_vec_ISet(parent1, M, -1); |
| 90 |
for (j = 0; j < N; j++) parent2[j] = Gz[j] ? -1 : M; |
| 91 |
|
| 92 |
|
| 93 |
do { |
| 94 |
done = TRUE; |
| 95 |
|
| 96 |
for (j = 0; j < N; j++) |
| 97 |
if (parent2[j] != -1) |
| 98 |
for (i = 0; i < M; i++) |
| 99 |
if (parent1[i] == -1 && A[i][j] && ! G[i][j]) { parent1[i] = j; done = FALSE; break; } |
| 100 |
|
| 101 |
for (i = 0; i < M; i++) |
| 102 |
if (parent1[i] != -1 && ! Ga[i]) { par0 = i; found_path = TRUE; break; } |
| 103 |
|
| 104 |
for (i = 0; i < M; i++) |
| 105 |
if (parent1[i] != -1) |
| 106 |
for (j = 0; j < N; j++) |
| 107 |
if (parent2[j] == -1 && G[i][j]) { parent2[j] = i; done = FALSE; break; } |
| 108 |
} while (! found_path && ! done); |
| 109 |
|
| 110 |
if (! found_path) break; |
| 111 |
|
| 112 |
|
| 113 |
i = par0; |
| 114 |
Ga[i] = 1; |
| 115 |
while (1) |
| 116 |
{ |
| 117 |
j = parent1[i]; G[i][j] = 1; nedges++; |
| 118 |
if (parent2[j] == N) { Gz[j] = 1; break; } |
| 119 |
i = parent2[j]; G[i][j] = 0; nedges--; |
| 120 |
} |
| 121 |
} |
| 122 |
free(Ga); free(Gz); |
| 123 |
free(parent1); free(parent2); |
| 124 |
if (opt_G) *opt_G = G; else esl_mat_IDestroy(G); |
| 125 |
*ret_nedges = nedges; |
| 126 |
return eslOK; |
| 127 |
|
| 128 |
ERROR: |
| 129 |
esl_mat_IDestroy(G); |
| 130 |
free(Ga); free(Gz); |
| 131 |
free(parent1); free(parent2); |
| 132 |
if (opt_G) *opt_G = NULL; |
| 133 |
*ret_nedges = 0; |
| 134 |
return status; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
#ifdef eslGRAPH_TESTDRIVE |
| 142 |
|
| 143 |
#include "esl_mixdchlet.h" |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
static void |
| 154 |
utest_perfect(ESL_RANDOMNESS *rng) |
| 155 |
{ |
| 156 |
char msg[] = "esl_graph utest_perfect failed"; |
| 157 |
int n = 1 + esl_rnd_Roll(rng, 20); |
| 158 |
int nextra = (n == 1 ? 0 : esl_rnd_Roll(rng, n*n-n)); |
| 159 |
int *shuf = NULL; |
| 160 |
int **G0 = esl_mat_ICreate(n, n); |
| 161 |
int **A = esl_mat_ICreate(n, n); |
| 162 |
int **G = NULL; |
| 163 |
int ntot = n; |
| 164 |
int nedges; |
| 165 |
int i,j,e; |
| 166 |
|
| 167 |
if ((shuf = malloc(sizeof(int) * n)) == NULL) esl_fatal(msg); |
| 168 |
for (i = 0; i < n; i++) shuf[i] = i; |
| 169 |
esl_vec_IShuffle(rng, shuf, n); |
| 170 |
|
| 171 |
esl_mat_ISet(G0, n, n, 0); |
| 172 |
for (i = 0; i < n; i++) |
| 173 |
G0[i][shuf[i]] = TRUE; |
| 174 |
|
| 175 |
esl_mat_ICopy(G0, n, n, A); |
| 176 |
for (e = 0; e < nextra; e++) |
| 177 |
{ |
| 178 |
i = esl_rnd_Roll(rng, n); |
| 179 |
j = esl_rnd_Roll(rng, n); |
| 180 |
if (! A[i][j]) ntot++; |
| 181 |
A[i][j] = TRUE; |
| 182 |
} |
| 183 |
|
| 184 |
esl_graph_MaxBipartiteMatch(A, n, n, &G, &nedges); |
| 185 |
if (nedges != n) esl_fatal(msg); |
| 186 |
if (ntot <= n+1 && esl_mat_ICompare(G, G0, n, n) != eslOK) esl_fatal(msg); |
| 187 |
|
| 188 |
free(shuf); |
| 189 |
esl_mat_IDestroy(A); |
| 190 |
esl_mat_IDestroy(G); |
| 191 |
esl_mat_IDestroy(G0); |
| 192 |
} |
| 193 |
#endif |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
#ifdef eslGRAPH_TESTDRIVE |
| 199 |
|
| 200 |
#include "easel.h" |
| 201 |
#include "esl_getopts.h" |
| 202 |
#include "esl_random.h" |
| 203 |
|
| 204 |
static ESL_OPTIONS options[] = { |
| 205 |
|
| 206 |
{"-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0}, |
| 207 |
{"-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0}, |
| 208 |
{ 0,0,0,0,0,0,0,0,0,0}, |
| 209 |
}; |
| 210 |
static char usage[] = "[-options]"; |
| 211 |
static char banner[] = "test driver for graph module"; |
| 212 |
|
| 213 |
int |
| 214 |
main(int argc, char **argv) |
| 215 |
{ |
| 216 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 217 |
ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s")); |
| 218 |
|
| 219 |
fprintf(stderr, "## %s\n", argv[0]); |
| 220 |
fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng)); |
| 221 |
|
| 222 |
utest_perfect(rng); |
| 223 |
|
| 224 |
fprintf(stderr, "# status = ok\n"); |
| 225 |
esl_randomness_Destroy(rng); |
| 226 |
esl_getopts_Destroy(go); |
| 227 |
return 0; |
| 228 |
} |
| 229 |
#endif |
| 230 |
|
| 231 |
|