-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathnetflow.c
319 lines (245 loc) · 7.79 KB
/
netflow.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
/* netflow.c
Network flow implementation -- Ford-Fulkerson augmenting path algorithm.
by: Steven Skiena
begun: September 19, 2002
This algorithm requires its own special graph type, to allow for
flows and residual flow fields.
*/
/*
Copyright 2003 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
This program appears in my book:
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information.
This book can be ordered from Amazon.com at
http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#include <stdio.h>
#include <stdlib.h>
#include "bool.h"
#include "geometry.h"
#include "queue.h"
#define MAXV 100 /* maximum number of vertices */
#define MAXDEGREE 50 /* maximum outdegree of a vertex */
/* [[[ netflow_edgenode_cut */
typedef struct {
int v; /* neighboring vertex */
int capacity; /* capacity of edge */
int flow; /* flow through edge */
int residual; /* residual capacity of edge */
struct edgenode *next; /* next edge in list */
} edgenode;
/* ]]] */
typedef struct {
edgenode *edges[MAXV+1]; /* adjacency info */
int degree[MAXV+1]; /* outdegree of each vertex */
int nvertices; /* number of vertices in the graph */
int nedges; /* number of edges in the graph */
} flow_graph;
void initialize_graph(flow_graph *g) { /* graph to initialize */
int i; /* counter */
g->nvertices = 0;
g->nedges = 0;
for (i = 0; i < MAXV; i++) {
g->degree[i] = 0;
}
for (i = 0; i < MAXV; i++) {
g->edges[i] = NULL;
}
}
void insert_flow_edge(flow_graph *g, int x, int y, bool directed, int w) {
edgenode *p; /* temporary pointer */
p = malloc(sizeof(edgenode)); /* allocate storage for edgenode */
p->v = y;
p->capacity = w;
p->flow = 0;
p->residual = w;
p->next = (struct edgenode*)g->edges[x];
g->edges[x] = p; /* insert at head of list */
g->degree[x] ++;
if (directed == FALSE) {
insert_flow_edge(g, y, x, TRUE, w);
} else {
g->nedges++;
}
}
void read_flow_graph(flow_graph *g, bool directed) { /* graph to initialize */ /* is this graph directed? */
int i; /* counter */
int m; /* number of edges */
int x, y, w; /* placeholder for edge and weight */
initialize_graph(g);
scanf("%d %d\n", &(g->nvertices), &m);
for (i = 1; i <= m; i++) {
scanf("%d %d %d\n", &x, &y, &w);
insert_flow_edge(g, x, y, directed, w);
}
}
/* [[[ netflow_fedge_cut */
edgenode *find_edge(flow_graph *g, int x, int y) {
edgenode *p; /* temporary pointer */
p = g->edges[x];
while (p != NULL) {
if (p->v == y) {
return(p);
}
p = (edgenode*)p->next;
}
return(NULL);
}
/* ]]] */
void add_residual_edges(flow_graph *g) {
int i, j; /* counters */
edgenode *p; /* temporary pointer */
edgenode *find_edge();
for (i = 1; i <= g->nvertices; i++) {
p = g->edges[i];
while (p != NULL) {
if (find_edge(g, p->v, i) == NULL) {
insert_flow_edge(g, p->v, i, TRUE, 0);
}
p = (edgenode*)p->next;
}
}
}
void print_flow_graph(flow_graph *g) {
int i; /* counter */
edgenode *p; /* temporary pointer */
for (i = 1; i <= g->nvertices; i++) {
printf("%d: ", i);
p = g->edges[i];
while (p != NULL) {
printf(" %d(%d,%d,%d)",p->v, p->capacity, p->flow, p->residual);
p = (edgenode*)p->next;
}
printf("\n");
}
}
bool processed[MAXV+1]; /* which vertices have been processed */
bool discovered[MAXV+1]; /* which vertices have been found */
int parent[MAXV+1]; /* discovery relation */
bool finished = FALSE; /* if true, cut off search immediately */
void initialize_search(flow_graph *g) { /* graph to traverse */
int i; /* counter */
for (i = 1; i <= g->nvertices; i++) {
processed[i] = FALSE;
discovered[i] = FALSE;
parent[i] = -1;
}
}
void process_vertex_early(int v) { /* vertex to process */
}
void process_vertex_late(int v) { /* vertex to process */
}
void process_edge(int x, int y) { /* edge to process */
}
/* [[[ netflow_vedge_cut */
bool valid_edge(edgenode *e) {
if (e->residual > 0) {
return (TRUE);
}
return(FALSE);
}
/* ]]] */
void bfs(flow_graph *g, int start) {
queue q; /* queue of vertices to visit */
int v; /* current vertex */
int y; /* successor vertex */
edgenode *p; /* temporary pointer */
init_queue(&q);
enqueue(&q, start);
discovered[start] = TRUE;
while (empty_queue(&q) == FALSE) {
v = dequeue(&q);
process_vertex_early(v);
processed[v] = TRUE;
p = g->edges[v];
while (p != NULL) {
y = p->v;
if (valid_edge(p) == TRUE) {
if (processed[y] == FALSE /*|| g->directed*/) {
process_edge(v, y);
}
if (discovered[y] == FALSE) {
enqueue(&q, y);
discovered[y] = TRUE;
parent[y] = v;
}
}
p = (edgenode*)p->next;
}
process_vertex_late(v);
}
}
void find_path(int start, int end, int parents[]) {
if ((start == end) || (end == -1)) {
printf("\n%d",start);
} else {
find_path(start, parents[end], parents);
printf(" %d", end);
}
}
/* [[[ path_volume_cut */
int path_volume(flow_graph *g, int start, int end, int parents[]) {
edgenode *e; /* edge in question */
edgenode *find_edge();
if (parents[end] == -1) {
return(0);
}
e = find_edge(g, parents[end], end);
if (start == parents[end]) {
return(e->residual);
} else {
return(min(path_volume(g, start, parents[end], parents), e->residual));
}
}
/* ]]] */
/* [[[ augment_path_cut */
void augment_path(flow_graph *g, int start, int end, int parents[], int volume) {
edgenode *e; /* edge in question */
edgenode *find_edge();
if (start == end) {
return;
}
e = find_edge(g, parents[end], end);
e->flow += volume;
e->residual -= volume;
e = find_edge(g, end, parents[end]);
e->residual += volume;
augment_path(g, start, parents[end], parents, volume);
}
/* ]]] */
/* [[[ netflow_cut */
void netflow(flow_graph *g, int source, int sink) {
int volume; /* weight of the augmenting path */
add_residual_edges(g);
initialize_search(g);
bfs(g, source);
volume = path_volume(g, source, sink, parent);
while (volume > 0) {
augment_path(g, source, sink, parent, volume);
initialize_search(g);
bfs(g, source);
volume = path_volume(g, source, sink, parent);
}
}
/* ]]] */
int main(void) {
flow_graph g; /* graph to analyze */
int source, sink; /* source and sink vertices */
int flow; /* total flow */
edgenode *p; /* temporary pointer */
scanf("%d %d", &source, &sink);
read_flow_graph(&g, TRUE);
netflow(&g, source, sink);
print_flow_graph(&g);
flow = 0;
p = g.edges[source];
while (p != NULL) {
flow += p->flow;
p = (edgenode*)p->next;
}
printf("total flow = %d\n", flow);
return 0;
}