2
* Copyright 1997, Regents of the University of Minnesota
6
* This file implements the minimum cover algorithm
11
* $Id: mincover.c 9942 2011-05-17 22:09:52Z karypis $
16
/*************************************************************************
17
* Constants used by mincover algorithm
18
**************************************************************************/
29
/*************************************************************************
30
* This function returns the min-cover of a bipartite graph.
31
* The algorithm used is due to Hopcroft and Karp as modified by Duff etal
32
* adj: the adjacency list of the bipartite graph
33
* asize: the number of vertices in the first part of the bipartite graph
34
* bsize-asize: the number of vertices in the second part
35
* 0..(asize-1) > A vertices
36
* asize..bsize > B vertices
39
* cover : the actual cover (array)
40
* csize : the size of the cover
41
**************************************************************************/
42
void MinCover(idx_t *xadj, idx_t *adjncy, idx_t asize, idx_t bsize, idx_t *cover, idx_t *csize)
45
idx_t *mate, *queue, *flag, *level, *lst;
46
idx_t fptr, rptr, lstptr;
47
idx_t row, maxlevel, col;
49
mate = ismalloc(bsize, -1, "MinCover: mate");
50
flag = imalloc(bsize, "MinCover: flag");
51
level = imalloc(bsize, "MinCover: level");
52
queue = imalloc(bsize, "MinCover: queue");
53
lst = imalloc(bsize, "MinCover: lst");
55
/* Get a cheap matching */
56
for (i=0; i<asize; i++) {
57
for (j=xadj[i]; j<xadj[i+1]; j++) {
58
if (mate[adjncy[j]] == -1) {
66
/* Get into the main loop */
69
fptr = rptr = 0; /* Empty Queue */
70
lstptr = 0; /* Empty List */
71
for (i=0; i<bsize; i++) {
77
/* Insert free nodes into the queue */
78
for (i=0; i<asize; i++)
85
while (fptr != rptr) {
87
if (level[row] < maxlevel) {
89
for (j=xadj[row]; j<xadj[row+1]; j++) {
91
if (!flag[col]) { /* If this column has not been accessed yet */
93
if (mate[col] == -1) { /* Free column node was found */
94
maxlevel = level[row];
97
else { /* This column node is matched */
99
printf("\nSomething wrong, flag[%"PRIDX"] is 1",mate[col]);
100
queue[rptr++] = mate[col];
101
level[mate[col]] = level[row] + 1;
109
break; /* No free columns can be reached */
111
/* Perform restricted DFS from the free column nodes */
112
for (i=0; i<lstptr; i++)
113
MinCover_Augment(xadj, adjncy, lst[i], mate, flag, level, maxlevel);
116
MinCover_Decompose(xadj, adjncy, asize, bsize, mate, cover, csize);
118
gk_free((void **)&mate, &flag, &level, &queue, &lst, LTERM);
123
/*************************************************************************
124
* This function perfoms a restricted DFS and augments matchings
125
**************************************************************************/
126
idx_t MinCover_Augment(idx_t *xadj, idx_t *adjncy, idx_t col, idx_t *mate, idx_t *flag, idx_t *level, idx_t maxlevel)
133
for (i=xadj[col]; i<xadj[col+1]; i++) {
136
if (flag[row] == 1) { /* First time through this row node */
137
if (level[row] == maxlevel) { /* (col, row) is an edge of the G^T */
138
flag[row] = 2; /* Mark this node as being visited */
140
status = MinCover_Augment(xadj, adjncy, mate[row], mate, flag, level, maxlevel-1);
158
/*************************************************************************
159
* This function performs a coarse decomposition and determines the
161
* REF: Pothen ACMTrans. on Amth Software
162
**************************************************************************/
163
void MinCover_Decompose(idx_t *xadj, idx_t *adjncy, idx_t asize, idx_t bsize, idx_t *mate, idx_t *cover, idx_t *csize)
169
where = imalloc(bsize, "MinCover_Decompose: where");
173
for (i=0; i<asize; i++)
178
for (i=0; i<asize; i++)
180
MinCover_ColDFS(xadj, adjncy, i, mate, where, INCOL);
183
MinCover_RowDFS(xadj, adjncy, i, mate, where, INROW);
185
for (i=0; i<bsize; i++)
189
if (iabs(card[VC]+card[SC]-card[HR]) < iabs(card[VC]-card[SR]-card[HR])) { /* S = VC+SC+HR */
190
/* printf("%"PRIDX" %"PRIDX" ",vc+sc, hr); */
191
for (i=0; i<bsize; i++)
192
if (where[i] == VC || where[i] == SC || where[i] == HR)
195
else { /* S = VC+SR+HR */
196
/* printf("%"PRIDX" %"PRIDX" ",vc, hr+sr); */
197
for (i=0; i<bsize; i++)
198
if (where[i] == VC || where[i] == SR || where[i] == HR)
203
gk_free((void **)&where, LTERM);
208
/*************************************************************************
209
* This function perfoms a dfs starting from an unmatched col node
210
* forming alternate paths
211
**************************************************************************/
212
void MinCover_ColDFS(idx_t *xadj, idx_t *adjncy, idx_t root, idx_t *mate, idx_t *where, idx_t flag)
217
if (where[root] == HC)
220
for (i=xadj[root]; i<xadj[root+1]; i++)
221
MinCover_ColDFS(xadj, adjncy, adjncy[i], mate, where, INROW);
224
if (where[root] == HR)
227
if (mate[root] != -1)
228
MinCover_ColDFS(xadj, adjncy, mate[root], mate, where, INCOL);
233
/*************************************************************************
234
* This function perfoms a dfs starting from an unmatched col node
235
* forming alternate paths
236
**************************************************************************/
237
void MinCover_RowDFS(idx_t *xadj, idx_t *adjncy, idx_t root, idx_t *mate, idx_t *where, idx_t flag)
242
if (where[root] == VR)
245
for (i=xadj[root]; i<xadj[root+1]; i++)
246
MinCover_RowDFS(xadj, adjncy, adjncy[i], mate, where, INCOL);
249
if (where[root] == VC)
252
if (mate[root] != -1)
253
MinCover_RowDFS(xadj, adjncy, mate[root], mate, where, INROW);