-
Notifications
You must be signed in to change notification settings - Fork 0
/
mincut.c
423 lines (332 loc) · 9.31 KB
/
mincut.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <string.h>
#include <assert.h>
#include <limits.h>
#include "mincut.h"
#include "macros.h"
#include "containers.h"
struct MinCArc
{
int v;
int cap;
int rpos; // position of reverse arc
char original; // if arc exists in original graph
};
struct _MinCut
{
int n;
// start[u] indicates the starting position outbond arcs from u
int *start;
struct MinCArc *arcs;
// maps to original node indexes,
// just to output the solution
int *orig;
// incidence vector and list of visited nodes
char *ivVisited;
int nVisited;
int *visited;
// queue of unvisited nodes
int *queue;
// path from t to s
int *parent;
// to quickly search for an (u,v) arc
Dict_int *arcDict;
// source node
int s;
// target node
int t;
// minimum cut answer
int nCut;
int *cutU;
int *cutV;
};
static char *arcName( char *str, int u, int v )
{
sprintf( str, "(%d,%d)",u,v );
return str;
}
static int arcPos( const MinCut *minc, int u, int v )
{
char str[256];
const Dict_int *arcDict = minc->arcDict;
const char *aname = arcName( str, u, v );
return dict_int_get( arcDict, aname );
}
MinCut *minc_create( int nArcs, const int _tail[], const int _head[], const int _cap[], int s, int t )
{
assert( s!=t );
int maxN = -1;
for ( int i=0 ; (i<nArcs) ; ++i )
maxN = MAX( maxN, _tail[i] );
for ( int i=0 ; (i<nArcs) ; ++i )
maxN = MAX( maxN, _head[i] );
// maps original nodes to new pre-processed nodes
int *ppnode;
ALLOCATE_VECTOR( ppnode, int, (maxN+1) );
for ( int i=0 ; (i<maxN+1) ; ++i )
ppnode[i] = -1;
int n=0;
for ( int i=0 ; (i<nArcs) ; ++i )
if (ppnode[_tail[i]]==-1)
ppnode[_tail[i]] = n++;
for ( int i=0 ; (i<nArcs) ; ++i )
if (ppnode[_head[i]]==-1)
ppnode[_head[i]] = n++;
// may be larger due to insertion of reverse arcs
int *tail;
int *head;
int *cap;
ALLOCATE_VECTOR( tail, int, (6*nArcs) );
head = tail + 2*nArcs;
cap = head + 2*nArcs;
// inverse arcs must be added if needed
Dict_int *arcDict = dict_int_create( nArcs*2, -1 );
// storing positions of existing arcs
char str[256];
for ( int i=0 ; i<nArcs ; ++i )
{
tail[i] = ppnode[_tail[i]];
head[i] = ppnode[_head[i]];
if (tail[i] == head[i] )
{
fprintf( stderr, "minc ERROR: arc (%d,%d) specified (self arc).\n", _tail[i], _head[i] );
abort();
}
cap[i] = _cap[i];
const char *aname = arcName(str, tail[i], head[i] );
if ( dict_int_get( arcDict, aname ) != -1 )
{
fprintf( stderr, "minc ERROR: arc (%d,%d) specified twice.\n", _tail[i], _head[i] );
abort();
}
dict_int_set( arcDict, aname, i );
}
int *orig;
ALLOCATE_VECTOR( orig, int, n );
for ( int i=0 ; (i<maxN+1) ; ++i )
if (ppnode[i]!=-1)
orig[ppnode[i]] = i;
MinCut *minc;
ALLOCATE( minc, MinCut );
minc->s = ppnode[s];
minc->t = ppnode[t];
free( ppnode );
// adding missing arcs
int nOrigArcs = nArcs;
for ( int i=0 ; i<nOrigArcs ; ++i )
{
int u = tail[i];
int v = head[i];
const char *rname = arcName(str, v, u );
if ( dict_int_get( arcDict, rname ) == -1 )
{
dict_int_set( arcDict, rname, nArcs );
tail[nArcs] = v;
head[nArcs] = u;
cap[nArcs] = 0;
++nArcs;
}
}
int *start, *nNeigh;
struct MinCArc *arcs;
ALLOCATE_VECTOR( arcs, struct MinCArc, nArcs );
ALLOCATE_VECTOR( start, int, (n+1) );
ALLOCATE_VECTOR_INI( nNeigh, int, n );
// counting neighbors per node
for ( int i=0 ; (i<nArcs) ; ++i )
nNeigh[tail[i]]++;
// setting up start
start[0] = 0;
for ( int i=1 ; (i<n+1) ; ++i )
start[i] = start[i-1] + nNeigh[i-1];
memset( nNeigh, 0, sizeof(int)*n );
// storing arcs in positions
// organized by tail
for ( int i=0 ; (i<nArcs) ; ++i )
{
const int ctail = tail[i];
const int chead = head[i];
const int pos = start[ctail]+nNeigh[ctail];
arcs[pos].v = chead;
arcs[pos].cap = cap[i];
const char *aname = arcName( str, ctail, chead );
dict_int_set( arcDict, aname, pos );
++(nNeigh[ctail]);
}
free( nNeigh );
// filling reverse arcs positions
for ( int u=0 ; (u<n) ; ++u )
{
for ( int j=start[u] ; j<start[u+1] ; ++j )
{
int v = arcs[j].v;
const char *rname = arcName( str, v, u );
int rpos = dict_int_get( arcDict, rname );
assert( rpos >= 0 && rpos < nArcs && rpos != j && rpos >= start[v] );
assert( arcs[rpos].v == u );
arcs[j].rpos = rpos;
} // all v
} // all us
minc->n = n;
minc->orig = orig;
minc->arcs = arcs;
minc->start = start;
ALLOCATE_VECTOR_INI( minc->ivVisited, char, minc->n );
ALLOCATE_VECTOR( minc->visited, int, minc->n );
minc->nVisited = 0;
ALLOCATE_VECTOR( minc->queue, int, n );
ALLOCATE_VECTOR( minc->parent, int, n );
minc->arcDict = arcDict;
free( tail );
ALLOCATE_VECTOR( minc->cutU, int, 2*n );
minc->cutV = minc->cutU + n;
minc->nCut = 0;
/* info about original arcs */
for ( int i=0 ; i<nArcs ; ++i )
arcs[i].original = arcs[i].cap>0;
return minc;
}
static void addVisited( MinCut *minc, int node )
{
#ifdef DEBUG
assert( minc->ivVisited[node]==False );
#endif
minc->ivVisited[node] = True;
minc->visited[minc->nVisited++] = node;
}
static void clearVisited( MinCut *minc )
{
for ( int i=0 ; (i<minc->nVisited) ; ++i )
minc->ivVisited[minc->visited[i]] = False;
minc->nVisited = 0;
#ifdef DEBUG
for ( int i=0 ; (i<minc->n) ; ++i )
{
assert( minc->ivVisited[i]==False );
}
#endif
}
static char bfs( MinCut *minc )
{
minc->nVisited = 0;
const int s = minc->s;
const int t = minc->t;
int *queue = minc->queue;
int *parent = minc->parent;
const int *start = minc->start;
char *ivVisited = minc->ivVisited;
const struct MinCArc *arcs = minc->arcs;
queue[0] = s;
int nQueue = 1;
addVisited( minc, s );
parent[s] = -1;
while ( nQueue>0 )
{
int u = queue[--nQueue];
// exploring neighbors of u
for ( int p=start[u] ; p<start[u+1] ; ++p )
{
int v = arcs[p].v;
if (ivVisited[v]==False && arcs[p].cap>0 )
{
queue[nQueue++] = v;
parent[v] = u;
addVisited( minc, v );
}
}
}
char reachedT = ivVisited[t];
// clear visited
clearVisited( minc );
return reachedT;
}
static void dfs( MinCut *minc, int s )
{
const int *start = minc->start;
char *ivVisited = minc->ivVisited;
addVisited( minc, s );
// checking neighbors
for ( int j=start[s] ; (j<start[s+1]) ; ++j )
{
const struct MinCArc *arc = minc->arcs+j;
if ( ivVisited[arc->v]==False && arc->cap )
dfs( minc, arc->v );
}
}
int minc_optimize( MinCut *minc )
{
const int s = minc->s;
const int t = minc->t;
const int *parent = minc->parent;
struct MinCArc *arcs = minc->arcs;
const int *start = minc->start;
int totalFlow = 0;
while ( bfs( minc ) )
{
int flow = INT_MAX;
for ( int v=t; (v!=s) ; v=parent[v] )
{
int u = parent[v];
int apos = arcPos( minc, u, v );
flow = MIN( flow, arcs[apos].cap );
} // checking path capacity
assert( flow > 0 );
totalFlow += flow;
// updating residual capacities
for ( int v=t; (v!=s) ; v=parent[v] )
{
int u = parent[v];
int apos = arcPos( minc, u, v );
arcs[apos].cap -= flow;
int rpos = arcPos( minc, v, u );
arcs[rpos].cap += flow;
}
} // while found a path
if (totalFlow)
{
dfs( minc, minc->s );
const char *ivVisited = minc->ivVisited;
// checking arc cuts
for ( int u=0 ; (u<minc->n) ; ++u )
{
if (!ivVisited[u])
continue;
for ( int pos=start[u] ; (pos<start[u+1]) ; ++pos )
{
const struct MinCArc *arc = arcs+pos;
if (ivVisited[arc->v] || !arc->original)
continue;
minc->cutU[minc->nCut] = minc->orig[u];
minc->cutV[minc->nCut] = minc->orig[arc->v];
++minc->nCut;
} // destination side
} // source side
}
return totalFlow;
}
int minc_n_cut( MinCut *minc )
{
return minc->nCut;
}
int minc_cut_arc_source( MinCut *minc, int i )
{
return minc->cutU[i];
}
int minc_cut_arc_destination( MinCut *minc, int i )
{
return minc->cutV[i];
}
void minc_free( MinCut **_minc )
{
MinCut *minc = *_minc;
free( minc->start );
free( minc->arcs );
free( minc->orig );
free( minc->ivVisited );
free( minc->visited );
free( minc->queue );
free( minc->parent );
dict_int_free( &minc->arcDict );
free( minc->cutU );
free( minc );
*_minc = NULL;
}