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