-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglpapi16.c
329 lines (319 loc) · 10.9 KB
/
glpapi16.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
/* glpapi16.c (graph and network analysis routines) */
/***********************************************************************
* This code is part of GLPK (GNU Linear Programming Kit).
*
* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
* reserved. E-mail: <[email protected]>.
*
* GLPK is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GLPK is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/
#include "glpapi.h"
#include "glpnet.h"
/***********************************************************************
* NAME
*
* glp_weak_comp - find all weakly connected components of graph
*
* SYNOPSIS
*
* int glp_weak_comp(glp_graph *G, int v_num);
*
* DESCRIPTION
*
* The routine glp_weak_comp finds all weakly connected components of
* the specified graph.
*
* The parameter v_num specifies an offset of the field of type int
* in the vertex data block, to which the routine stores the number of
* a (weakly) connected component containing that vertex. If v_num < 0,
* no component numbers are stored.
*
* The components are numbered in arbitrary order from 1 to nc, where
* nc is the total number of components found, 0 <= nc <= |V|.
*
* RETURNS
*
* The routine returns nc, the total number of components found. */
int glp_weak_comp(glp_graph *G, int v_num)
{ glp_vertex *v;
glp_arc *a;
int f, i, j, nc, nv, pos1, pos2, *prev, *next, *list;
if (v_num >= 0 && v_num > G->v_size - (int)sizeof(int))
xerror("glp_weak_comp: v_num = %d; invalid offset\n", v_num);
nv = G->nv;
if (nv == 0)
{ nc = 0;
goto done;
}
/* allocate working arrays */
prev = xcalloc(1+nv, sizeof(int));
next = xcalloc(1+nv, sizeof(int));
list = xcalloc(1+nv, sizeof(int));
/* if vertex i is unlabelled, prev[i] is the index of previous
unlabelled vertex, and next[i] is the index of next unlabelled
vertex; if vertex i is labelled, then prev[i] < 0, and next[i]
is the connected component number */
/* initially all vertices are unlabelled */
f = 1;
for (i = 1; i <= nv; i++)
prev[i] = i - 1, next[i] = i + 1;
next[nv] = 0;
/* main loop (until all vertices have been labelled) */
nc = 0;
while (f != 0)
{ /* take an unlabelled vertex */
i = f;
/* and remove it from the list of unlabelled vertices */
f = next[i];
if (f != 0) prev[f] = 0;
/* label the vertex; it begins a new component */
prev[i] = -1, next[i] = ++nc;
/* breadth first search */
list[1] = i, pos1 = pos2 = 1;
while (pos1 <= pos2)
{ /* dequeue vertex i */
i = list[pos1++];
/* consider all arcs incoming to vertex i */
for (a = G->v[i]->in; a != NULL; a = a->h_next)
{ /* vertex j is adjacent to vertex i */
j = a->tail->i;
if (prev[j] >= 0)
{ /* vertex j is unlabelled */
/* remove it from the list of unlabelled vertices */
if (prev[j] == 0)
f = next[j];
else
next[prev[j]] = next[j];
if (next[j] == 0)
;
else
prev[next[j]] = prev[j];
/* label the vertex */
prev[j] = -1, next[j] = nc;
/* and enqueue it for further consideration */
list[++pos2] = j;
}
}
/* consider all arcs outgoing from vertex i */
for (a = G->v[i]->out; a != NULL; a = a->t_next)
{ /* vertex j is adjacent to vertex i */
j = a->head->i;
if (prev[j] >= 0)
{ /* vertex j is unlabelled */
/* remove it from the list of unlabelled vertices */
if (prev[j] == 0)
f = next[j];
else
next[prev[j]] = next[j];
if (next[j] == 0)
;
else
prev[next[j]] = prev[j];
/* label the vertex */
prev[j] = -1, next[j] = nc;
/* and enqueue it for further consideration */
list[++pos2] = j;
}
}
}
}
/* store component numbers */
if (v_num >= 0)
{ for (i = 1; i <= nv; i++)
{ v = G->v[i];
memcpy((char *)v->data + v_num, &next[i], sizeof(int));
}
}
/* free working arrays */
xfree(prev);
xfree(next);
xfree(list);
done: return nc;
}
/***********************************************************************
* NAME
*
* glp_strong_comp - find all strongly connected components of graph
*
* SYNOPSIS
*
* int glp_strong_comp(glp_graph *G, int v_num);
*
* DESCRIPTION
*
* The routine glp_strong_comp finds all strongly connected components
* of the specified graph.
*
* The parameter v_num specifies an offset of the field of type int
* in the vertex data block, to which the routine stores the number of
* a strongly connected component containing that vertex. If v_num < 0,
* no component numbers are stored.
*
* The components are numbered in arbitrary order from 1 to nc, where
* nc is the total number of components found, 0 <= nc <= |V|. However,
* the component numbering has the property that for every arc (i->j)
* in the graph the condition num(i) >= num(j) holds.
*
* RETURNS
*
* The routine returns nc, the total number of components found. */
int glp_strong_comp(glp_graph *G, int v_num)
{ glp_vertex *v;
glp_arc *a;
int i, k, last, n, na, nc, *icn, *ip, *lenr, *ior, *ib, *lowl,
*numb, *prev;
if (v_num >= 0 && v_num > G->v_size - (int)sizeof(int))
xerror("glp_strong_comp: v_num = %d; invalid offset\n",
v_num);
n = G->nv;
if (n == 0)
{ nc = 0;
goto done;
}
na = G->na;
icn = xcalloc(1+na, sizeof(int));
ip = xcalloc(1+n, sizeof(int));
lenr = xcalloc(1+n, sizeof(int));
ior = xcalloc(1+n, sizeof(int));
ib = xcalloc(1+n, sizeof(int));
lowl = xcalloc(1+n, sizeof(int));
numb = xcalloc(1+n, sizeof(int));
prev = xcalloc(1+n, sizeof(int));
k = 1;
for (i = 1; i <= n; i++)
{ v = G->v[i];
ip[i] = k;
for (a = v->out; a != NULL; a = a->t_next)
icn[k++] = a->head->i;
lenr[i] = k - ip[i];
}
xassert(na == k-1);
nc = mc13d(n, icn, ip, lenr, ior, ib, lowl, numb, prev);
if (v_num >= 0)
{ xassert(ib[1] == 1);
for (k = 1; k <= nc; k++)
{ last = (k < nc ? ib[k+1] : n+1);
xassert(ib[k] < last);
for (i = ib[k]; i < last; i++)
{ v = G->v[ior[i]];
memcpy((char *)v->data + v_num, &k, sizeof(int));
}
}
}
xfree(icn);
xfree(ip);
xfree(lenr);
xfree(ior);
xfree(ib);
xfree(lowl);
xfree(numb);
xfree(prev);
done: return nc;
}
/***********************************************************************
* NAME
*
* glp_top_sort - topological sorting of acyclic digraph
*
* SYNOPSIS
*
* int glp_top_sort(glp_graph *G, int v_num);
*
* DESCRIPTION
*
* The routine glp_top_sort performs topological sorting of vertices of
* the specified acyclic digraph.
*
* The parameter v_num specifies an offset of the field of type int in
* the vertex data block, to which the routine stores the vertex number
* assigned. If v_num < 0, vertex numbers are not stored.
*
* The vertices are numbered from 1 to n, where n is the total number
* of vertices in the graph. The vertex numbering has the property that
* for every arc (i->j) in the graph the condition num(i) < num(j)
* holds. Special case num(i) = 0 means that vertex i is not assigned a
* number, because the graph is *not* acyclic.
*
* RETURNS
*
* If the graph is acyclic and therefore all the vertices have been
* assigned numbers, the routine glp_top_sort returns zero. Otherwise,
* if the graph is not acyclic, the routine returns the number of
* vertices which have not been numbered, i.e. for which num(i) = 0. */
static int top_sort(glp_graph *G, int num[])
{ glp_arc *a;
int i, j, cnt, top, *stack, *indeg;
/* allocate working arrays */
indeg = xcalloc(1+G->nv, sizeof(int));
stack = xcalloc(1+G->nv, sizeof(int));
/* determine initial indegree of each vertex; push into the stack
the vertices having zero indegree */
top = 0;
for (i = 1; i <= G->nv; i++)
{ num[i] = indeg[i] = 0;
for (a = G->v[i]->in; a != NULL; a = a->h_next)
indeg[i]++;
if (indeg[i] == 0)
stack[++top] = i;
}
/* assign numbers to vertices in the sorted order */
cnt = 0;
while (top > 0)
{ /* pull vertex i from the stack */
i = stack[top--];
/* it has zero indegree in the current graph */
xassert(indeg[i] == 0);
/* so assign it a next number */
xassert(num[i] == 0);
num[i] = ++cnt;
/* remove vertex i from the current graph, update indegree of
its adjacent vertices, and push into the stack new vertices
whose indegree becomes zero */
for (a = G->v[i]->out; a != NULL; a = a->t_next)
{ j = a->head->i;
/* there exists arc (i->j) in the graph */
xassert(indeg[j] > 0);
indeg[j]--;
if (indeg[j] == 0)
stack[++top] = j;
}
}
/* free working arrays */
xfree(indeg);
xfree(stack);
return G->nv - cnt;
}
int glp_top_sort(glp_graph *G, int v_num)
{ glp_vertex *v;
int i, cnt, *num;
if (v_num >= 0 && v_num > G->v_size - (int)sizeof(int))
xerror("glp_top_sort: v_num = %d; invalid offset\n", v_num);
if (G->nv == 0)
{ cnt = 0;
goto done;
}
num = xcalloc(1+G->nv, sizeof(int));
cnt = top_sort(G, num);
if (v_num >= 0)
{ for (i = 1; i <= G->nv; i++)
{ v = G->v[i];
memcpy((char *)v->data + v_num, &num[i], sizeof(int));
}
}
xfree(num);
done: return cnt;
}
/* eof */