Debian Codegraph @

packages/seacas/libraries/chaco/connect/find_comps.c

raw
1 /*
2 * Copyright(C) 1999-2020, 2023 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #include "smalloc.h" // for smalloc
10 #include "structs.h" // for edgeslist, vtx_data
11 #include <stdio.h> // for NULL
12
13 static int bfsearch(struct vtx_data **graph, /* graph data structure */
14 int root, /* start vertex for DFS */
15 int *count, /* number of vertices in component */
16 int *mark, /* has vtx been seen? */
17 int *vtxlist, /* space for storing vtxs to search */
18 int comp_num /* current component number */
19 );
20
21 /* Breadth first search algorithm to find & mark connected components. */
22 int find_comps(struct vtx_data **graph, /* graph data structure */
23 int nvtxs, /* number of vertices in graph */
24 int *mark, /* space for nvtxs+1 ints */
25 int *vtxlist /* space for nvtxs ints */
26 )
27 {
28 int root; /* vertex to start the dfs */
29 int count; /* number of vertices seen so far */
30 int ncomps; /* number of components found */
31 int i; /* loop counter */
32 double drandom(void);
33
34 for (i = 1; i <= nvtxs; i++) {
35 mark[i] = -1;
36 }
37 count = 0;
38 ncomps = 0;
39 root = nvtxs * drandom() + 1;
40
41 bfsearch(graph, root, &count, mark, vtxlist, ncomps);
42
43 while (count != nvtxs) { /* Are there any remaining vertices? */
44 /* Find starting vtx for next BFS. */
45 root = nvtxs * drandom() + 1;
46 while (mark[root] >= 0) {
47 root++;
48 if (root > nvtxs) {
49 root = 1;
50 }
51 }
52 /* Add new edge to list needed for connectivity. */
53 ncomps++;
54 bfsearch(graph, root, &count, mark, vtxlist, ncomps);
55 }
56 return (ncomps + 1);
57 }
58
59 /* Breadth first search algorithm to find & mark connected components. */
60 /* Returns list of edges to connect them together. */
61 int find_edges(struct vtx_data **graph, /* graph data structure */
62 int nvtxs, /* number of vertices in graph */
63 int *mark, /* space for nvtxs+1 ints */
64 int *vtxlist, /* space for nvtxs ints */
65 struct edgeslist **edges /* list of edges connecting graph */
66 )
67 {
68 struct edgeslist *newedge; /* space to add new edge */
69 int root; /* vertex to start the dfs */
70 int last; /* last vertex seen in BFS */
71 int count; /* number of vertices seen so far */
72 int nadded; /* number of edges needed to be added */
73 int i; /* loop counter */
74 double drandom(void);
75
76 for (i = 1; i <= nvtxs; i++) {
77 mark[i] = -1;
78 }
79 count = 0;
80 nadded = 0;
81 *edges = NULL;
82 root = nvtxs * drandom() + 1;
83
84 last = bfsearch(graph, root, &count, mark, vtxlist, nadded);
85
86 while (count != nvtxs) { /* Are there any remaining vertices? */
87 /* Find starting vtx for next BFS. */
88 root = nvtxs * drandom() + 1;
89 while (mark[root] >= 0) {
90 root++;
91 if (root > nvtxs) {
92 root = 1;
93 }
94 }
95 /* Add new edge to list needed for connectivity. */
96 newedge = smalloc(sizeof(struct edgeslist));
97 newedge->next = *edges;
98 newedge->vtx1 = last;
99 newedge->vtx2 = root;
100 *edges = newedge;
101 nadded++;
102 last = bfsearch(graph, root, &count, mark, vtxlist, nadded);
103 }
104 return (nadded);
105 }
106
107 /* BFS to find connected component */
108 static int bfsearch(struct vtx_data **graph, /* graph data structure */
109 int root, /* start vertex for DFS */
110 int *count, /* number of vertices in component */
111 int *mark, /* has vtx been seen? */
112 int *vtxlist, /* space for storing vtxs to search */
113 int comp_num /* current component number */
114 )
115 {
116 int *iptr; /* loops through neighbor list */
117 int vtxbeg, vtxend; /* beginning and end of vertices in vtxlist */
118 int vtx; /* vertex being processed */
119 int neighbor; /* neighbor of vertex */
120 int i; /* loop counter */
121
122 vtxbeg = vtxend = 1;
123 mark[root] = comp_num;
124 vtxlist[0] = root;
125
126 /* Copy root's neighbors to vtxlist, incrementing count */
127 iptr = graph[root]->edges;
128 for (i = graph[root]->nedges - 1; i; i--) {
129 neighbor = *(++iptr);
130 vtxlist[vtxend++] = neighbor;
131 mark[neighbor] = comp_num;
132 }
133
134 while (vtxbeg < vtxend) {
135 vtx = vtxlist[vtxbeg++];
136 /* Loop through neighbors, copying to vtxlist if unmarked. */
137 iptr = graph[vtx]->edges;
138 for (i = graph[vtx]->nedges - 1; i; i--) {
139 neighbor = *(++iptr);
140 if (mark[neighbor] != comp_num) {
141 mark[neighbor] = comp_num;
142 vtxlist[vtxend++] = neighbor;
143 }
144 }
145 }
146 *count += vtxend;
147 return (vtxlist[vtxend - 1]);
148 }