Debian Codegraph @

external/glpk/glpapi16.c

raw symbols SCIP
1 /* glpapi16.c (graph and network analysis routines) */cxx . . $ `<file>/external/glpk/glpapi16.c`/
2
3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
5 *
6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
7 * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
9 * E-mail: <[email protected]>.
10 *
11 * GLPK is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 * License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
23 ***********************************************************************/
24
25 #include "glpapi.h"cxx . . $ `<file>/external/glpk/glpapi.h`/
26 #include "glpnet.h"cxx . . $ `<file>/external/glpk/glpnet.h`/
27
28 /***********************************************************************
29 * NAME
30 *
31 * glp_weak_comp - find all weakly connected components of graph
32 *
33 * SYNOPSIS
34 *
35 * int glp_weak_comp(glp_graph *G, int v_num);
36 *
37 * DESCRIPTION
38 *
39 * The routine glp_weak_comp finds all weakly connected components of
40 * the specified graph.
41 *
42 * The parameter v_num specifies an offset of the field of type int
43 * in the vertex data block, to which the routine stores the number of
44 * a (weakly) connected component containing that vertex. If v_num < 0,
45 * no component numbers are stored.
46 *
47 * The components are numbered in arbitrary order from 1 to nc, where
48 * nc is the total number of components found, 0 <= nc <= |V|.
49 *
50 * RETURNS
51 *
52 * The routine returns nc, the total number of components found. */
53
54 int glp_weak_compcxx . . $ glp_weak_comp(9d5fec13eee488ca).(glp_graphcxx . . $ glp_graph# *G, int v_num)
55 { glp_vertexcxx . . $ glp_vertex# *v;
56 glp_arccxx . . $ glp_arc# *a;
57 int f, i, j, nc, nv, pos1, pos2, *prev, *next, *list;
58 if (v_num >= 0 && v_num > G->v_sizecxx . . $ _glp_graph#v_size. - (int)sizeof(int))
59 xerrorcxx . . $ `external/glpk/glpenv.h:158:9`!("glp_weak_comp: v_num = %d; invalid offset\n", v_num);
60 nv = G->nvcxx . . $ _glp_graph#nv.;
61 if (nv == 0)
62 { nc = 0;
63 goto done;
64 }
65 /* allocate working arrays */
66 prev = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+nv, sizeof(int));
67 next = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+nv, sizeof(int));
68 list = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+nv, sizeof(int));
69 /* if vertex i is unlabelled, prev[i] is the index of previous
70 unlabelled vertex, and next[i] is the index of next unlabelled
71 vertex; if vertex i is labelled, then prev[i] < 0, and next[i]
72 is the connected component number */
73 /* initially all vertices are unlabelled */
74 f = 1;
75 for (i = 1; i <= nv; i++)
76 prev[i] = i - 1, next[i] = i + 1;
77 next[nv] = 0;
78 /* main loop (until all vertices have been labelled) */
79 nc = 0;
80 while (f != 0)
81 { /* take an unlabelled vertex */
82 i = f;
83 /* and remove it from the list of unlabelled vertices */
84 f = next[i];
85 if (f != 0) prev[f] = 0;
86 /* label the vertex; it begins a new component */
87 prev[i] = -1, next[i] = ++nc;
88 /* breadth first search */
89 list[1] = i, pos1 = pos2 = 1;
90 while (pos1 <= pos2)
91 { /* dequeue vertex i */
92 i = list[pos1++];
93 /* consider all arcs incoming to vertex i */
94 for (a = G->vcxx . . $ _glp_graph#v.[i]->incxx . . $ _glp_vertex#in.; a != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; a = a->h_nextcxx . . $ _glp_arc#h_next.)
95 { /* vertex j is adjacent to vertex i */
96 j = a->tailcxx . . $ _glp_arc#tail.->icxx . . $ _glp_vertex#i.;
97 if (prev[j] >= 0)
98 { /* vertex j is unlabelled */
99 /* remove it from the list of unlabelled vertices */
100 if (prev[j] == 0)
101 f = next[j];
102 else
103 next[prev[j]] = next[j];
104 if (next[j] == 0)
105 ;
106 else
107 prev[next[j]] = prev[j];
108 /* label the vertex */
109 prev[j] = -1, next[j] = nc;
110 /* and enqueue it for further consideration */
111 list[++pos2] = j;
112 }
113 }
114 /* consider all arcs outgoing from vertex i */
115 for (a = G->vcxx . . $ _glp_graph#v.[i]->outcxx . . $ _glp_vertex#out.; a != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; a = a->t_nextcxx . . $ _glp_arc#t_next.)
116 { /* vertex j is adjacent to vertex i */
117 j = a->headcxx . . $ _glp_arc#head.->icxx . . $ _glp_vertex#i.;
118 if (prev[j] >= 0)
119 { /* vertex j is unlabelled */
120 /* remove it from the list of unlabelled vertices */
121 if (prev[j] == 0)
122 f = next[j];
123 else
124 next[prev[j]] = next[j];
125 if (next[j] == 0)
126 ;
127 else
128 prev[next[j]] = prev[j];
129 /* label the vertex */
130 prev[j] = -1, next[j] = nc;
131 /* and enqueue it for further consideration */
132 list[++pos2] = j;
133 }
134 }
135 }
136 }
137 /* store component numbers */
138 if (v_num >= 0)
139 { for (i = 1; i <= nv; i++)
140 { v = G->vcxx . . $ _glp_graph#v.[i];
141 memcpycxx . . $ memcpy(cc817cb175f282d).((char *)v->datacxx . . $ _glp_vertex#data. + v_num, &next[i], sizeof(int));
142 }
143 }
144 /* free working arrays */
145 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(prev);
146 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(next);
147 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(list);
148 done: return nc;
149 }
150
151 /***********************************************************************
152 * NAME
153 *
154 * glp_strong_comp - find all strongly connected components of graph
155 *
156 * SYNOPSIS
157 *
158 * int glp_strong_comp(glp_graph *G, int v_num);
159 *
160 * DESCRIPTION
161 *
162 * The routine glp_strong_comp finds all strongly connected components
163 * of the specified graph.
164 *
165 * The parameter v_num specifies an offset of the field of type int
166 * in the vertex data block, to which the routine stores the number of
167 * a strongly connected component containing that vertex. If v_num < 0,
168 * no component numbers are stored.
169 *
170 * The components are numbered in arbitrary order from 1 to nc, where
171 * nc is the total number of components found, 0 <= nc <= |V|. However,
172 * the component numbering has the property that for every arc (i->j)
173 * in the graph the condition num(i) >= num(j) holds.
174 *
175 * RETURNS
176 *
177 * The routine returns nc, the total number of components found. */
178
179 int glp_strong_compcxx . . $ glp_strong_comp(9d5fec13eee488ca).(glp_graphcxx . . $ glp_graph# *G, int v_num)
180 { glp_vertexcxx . . $ glp_vertex# *v;
181 glp_arccxx . . $ glp_arc# *a;
182 int i, k, last, n, na, nc, *icn, *ip, *lenr, *ior, *ib, *lowl,
183 *numb, *prev;
184 if (v_num >= 0 && v_num > G->v_sizecxx . . $ _glp_graph#v_size. - (int)sizeof(int))
185 xerrorcxx . . $ `external/glpk/glpenv.h:158:9`!("glp_strong_comp: v_num = %d; invalid offset\n",
186 v_num);
187 n = G->nvcxx . . $ _glp_graph#nv.;
188 if (n == 0)
189 { nc = 0;
190 goto done;
191 }
192 na = G->nacxx . . $ _glp_graph#na.;
193 icn = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+na, sizeof(int));
194 ip = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+n, sizeof(int));
195 lenr = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+n, sizeof(int));
196 ior = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+n, sizeof(int));
197 ib = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+n, sizeof(int));
198 lowl = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+n, sizeof(int));
199 numb = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+n, sizeof(int));
200 prev = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+n, sizeof(int));
201 k = 1;
202 for (i = 1; i <= n; i++)
203 { v = G->vcxx . . $ _glp_graph#v.[i];
204 ip[i] = k;
205 for (a = v->outcxx . . $ _glp_vertex#out.; a != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; a = a->t_nextcxx . . $ _glp_arc#t_next.)
206 icn[k++] = a->headcxx . . $ _glp_arc#head.->icxx . . $ _glp_vertex#i.;
207 lenr[i] = k - ip[i];
208 }
209 xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(na == k-1);
210 nc = mc13dcxx . . $ `external/glpk/glpnet.h:33:9`!(n, icn, ip, lenr, ior, ib, lowl, numb, prev);
211 if (v_num >= 0)
212 { xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(ib[1] == 1);
213 for (k = 1; k <= nc; k++)
214 { last = (k < nc ? ib[k+1] : n+1);
215 xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(ib[k] < last);
216 for (i = ib[k]; i < last; i++)
217 { v = G->vcxx . . $ _glp_graph#v.[ior[i]];
218 memcpycxx . . $ memcpy(cc817cb175f282d).((char *)v->datacxx . . $ _glp_vertex#data. + v_num, &k, sizeof(int));
219 }
220 }
221 }
222 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(icn);
223 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(ip);
224 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(lenr);
225 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(ior);
226 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(ib);
227 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(lowl);
228 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(numb);
229 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(prev);
230 done: return nc;
231 }
232
233 /***********************************************************************
234 * NAME
235 *
236 * glp_top_sort - topological sorting of acyclic digraph
237 *
238 * SYNOPSIS
239 *
240 * int glp_top_sort(glp_graph *G, int v_num);
241 *
242 * DESCRIPTION
243 *
244 * The routine glp_top_sort performs topological sorting of vertices of
245 * the specified acyclic digraph.
246 *
247 * The parameter v_num specifies an offset of the field of type int in
248 * the vertex data block, to which the routine stores the vertex number
249 * assigned. If v_num < 0, vertex numbers are not stored.
250 *
251 * The vertices are numbered from 1 to n, where n is the total number
252 * of vertices in the graph. The vertex numbering has the property that
253 * for every arc (i->j) in the graph the condition num(i) < num(j)
254 * holds. Special case num(i) = 0 means that vertex i is not assigned a
255 * number, because the graph is *not* acyclic.
256 *
257 * RETURNS
258 *
259 * If the graph is acyclic and therefore all the vertices have been
260 * assigned numbers, the routine glp_top_sort returns zero. Otherwise,
261 * if the graph is not acyclic, the routine returns the number of
262 * vertices which have not been numbered, i.e. for which num(i) = 0. */
263
264 static int top_sortcxx . . $ top_sort(1401ea12b79e7a8b).(glp_graphcxx . . $ glp_graph# *G, int num[])
265 { glp_arccxx . . $ glp_arc# *a;
266 int i, j, cnt, top, *stack, *indeg;
267 /* allocate working arrays */
268 indeg = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+G->nvcxx . . $ _glp_graph#nv., sizeof(int));
269 stack = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+G->nvcxx . . $ _glp_graph#nv., sizeof(int));
270 /* determine initial indegree of each vertex; push into the stack
271 the vertices having zero indegree */
272 top = 0;
273 for (i = 1; i <= G->nvcxx . . $ _glp_graph#nv.; i++)
274 { num[i] = indeg[i] = 0;
275 for (a = G->vcxx . . $ _glp_graph#v.[i]->incxx . . $ _glp_vertex#in.; a != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; a = a->h_nextcxx . . $ _glp_arc#h_next.)
276 indeg[i]++;
277 if (indeg[i] == 0)
278 stack[++top] = i;
279 }
280 /* assign numbers to vertices in the sorted order */
281 cnt = 0;
282 while (top > 0)
283 { /* pull vertex i from the stack */
284 i = stack[top--];
285 /* it has zero indegree in the current graph */
286 xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(indeg[i] == 0);
287 /* so assign it a next number */
288 xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(num[i] == 0);
289 num[i] = ++cnt;
290 /* remove vertex i from the current graph, update indegree of
291 its adjacent vertices, and push into the stack new vertices
292 whose indegree becomes zero */
293 for (a = G->vcxx . . $ _glp_graph#v.[i]->outcxx . . $ _glp_vertex#out.; a != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; a = a->t_nextcxx . . $ _glp_arc#t_next.)
294 { j = a->headcxx . . $ _glp_arc#head.->icxx . . $ _glp_vertex#i.;
295 /* there exists arc (i->j) in the graph */
296 xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(indeg[j] > 0);
297 indeg[j]--;
298 if (indeg[j] == 0)
299 stack[++top] = j;
300 }
301 }
302 /* free working arrays */
303 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(indeg);
304 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(stack);
305 return G->nvcxx . . $ _glp_graph#nv. - cnt;
306 }
307
308 int glp_top_sortcxx . . $ glp_top_sort(9d5fec13eee488ca).(glp_graphcxx . . $ glp_graph# *G, int v_num)
309 { glp_vertexcxx . . $ glp_vertex# *v;
310 int i, cnt, *num;
311 if (v_num >= 0 && v_num > G->v_sizecxx . . $ _glp_graph#v_size. - (int)sizeof(int))
312 xerrorcxx . . $ `external/glpk/glpenv.h:158:9`!("glp_top_sort: v_num = %d; invalid offset\n", v_num);
313 if (G->nvcxx . . $ _glp_graph#nv. == 0)
314 { cnt = 0;
315 goto done;
316 }
317 num = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+G->nvcxx . . $ _glp_graph#nv., sizeof(int));
318 cnt = top_sortcxx . . $ top_sort(1401ea12b79e7a8b).(G, num);
319 if (v_num >= 0)
320 { for (i = 1; i <= G->nvcxx . . $ _glp_graph#nv.; i++)
321 { v = G->vcxx . . $ _glp_graph#v.[i];
322 memcpycxx . . $ memcpy(cc817cb175f282d).((char *)v->datacxx . . $ _glp_vertex#data. + v_num, &num[i], sizeof(int));
323 }
324 }
325 xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(num);
326 done: return cnt;
327 }
328
329 /* eof */