Debian Codegraph @

lib/ortho/sgraph.c

raw
1 /*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11
12 #include "config.h"
13 #include <limits.h>
14 #include <ortho/sgraph.h>
15 #include <ortho/fPQ.h>
16 #include <util/alloc.h>
17
18 void
19 gsave (sgraph* G)
20 {
21 int i;
22 G->save_nnodes = G->nnodes;
23 G->save_nedges = G->nedges;
24 for (i = 0; i < G->nnodes; i++)
25 G->nodes[i].save_n_adj = G->nodes[i].n_adj;
26 }
27
28 void
29 reset(sgraph* G)
30 {
31 int i;
32 G->nnodes = G->save_nnodes;
33 G->nedges = G->save_nedges;
34 for (i = 0; i < G->nnodes; i++)
35 G->nodes[i].n_adj = G->nodes[i].save_n_adj;
36 for (; i < G->nnodes+2; i++)
37 G->nodes[i].n_adj = 0;
38 }
39
40 void initSEdges(sgraph *g, size_t maxdeg) {
41 int i;
42 int* adj = gv_calloc(6 * g->nnodes + 2 * maxdeg, sizeof(int));
43 g->edges = gv_calloc(3 * g->nnodes + maxdeg, sizeof(sedge));
44 for (i = 0; i < g->nnodes; i++) {
45 g->nodes[i].adj_edge_list = adj;
46 adj += 6;
47 }
48 for (; i < g->nnodes+2; i++) {
49 g->nodes[i].adj_edge_list = adj;
50 adj += maxdeg;
51 }
52 }
53
54 sgraph *createSGraph(size_t nnodes) {
55 sgraph* g = gv_alloc(sizeof(sgraph));
56
57 /* create the nodes vector in the search graph */
58 g->nnodes = 0;
59 g->nodes = gv_calloc(nnodes, sizeof(snode));
60 return g;
61 }
62
63 snode*
64 createSNode (sgraph* g)
65 {
66 snode* np = g->nodes+g->nnodes;
67 np->index = g->nnodes;
68 g->nnodes++;
69 return np;
70 }
71
72 static void
73 addEdgeToNode (snode* np, int idx)
74 {
75 np->adj_edge_list[np->n_adj] = idx;
76 np->n_adj++;
77 }
78
79 sedge*
80 createSEdge (sgraph* g, snode* v1, snode* v2, double wt)
81 {
82 sedge* e;
83 int idx = g->nedges++;
84
85 e = g->edges + idx;
86 e->v1 = v1->index;
87 e->v2 = v2->index;
88 e->weight = wt;
89 e->cnt = 0;
90
91 addEdgeToNode (v1, idx);
92 addEdgeToNode (v2, idx);
93
94 return e;
95 }
96
97 void
98 freeSGraph (sgraph* g)
99 {
100 free (g->nodes[0].adj_edge_list);
101 free (g->nodes);
102 free (g->edges);
103 free (g);
104 }
105
106 #include <ortho/fPQ.h>
107
108 /* shortest path:
109 * Constructs the path of least weight between from and to.
110 *
111 * Assumes graph, node and edge type, and that nodes
112 * have associated values N_VAL, N_IDX, and N_DAD, the first two
113 * being ints, the last being a node*. Edges have a E_WT function
114 * to specify the edge length or weight.
115 *
116 * Assumes there are functions:
117 * agnnodes: graph -> int number of nodes in the graph
118 * agfstnode, agnxtnode : iterators over the nodes in the graph
119 * agfstedge, agnxtedge : iterators over the edges attached to a node
120 * adjacentNode : given an edge e and an endpoint n of e, returns the
121 * other endpoint.
122 *
123 * The path is given by
124 * to, N_DAD(to), N_DAD(N_DAD(to)), ..., from
125 */
126
127 #define UNSEEN INT_MIN
128
129 static snode*
130 adjacentNode(sgraph* g, sedge* e, snode* n)
131 {
132 if (e->v1==n->index)
133 return &g->nodes[e->v2];
134 else
135 return &g->nodes[e->v1];
136 }
137
138 int
139 shortPath (sgraph* g, snode* from, snode* to)
140 {
141 snode* n;
142 sedge* e;
143 snode* adjn;
144 int d;
145 int x, y;
146
147 for (x = 0; x<g->nnodes; x++) {
148 snode* temp = &g->nodes[x];
149 N_VAL(temp) = UNSEEN;
150 }
151
152 PQinit();
153 if (PQ_insert (from)) return 1;
154 N_DAD(from) = NULL;
155 N_VAL(from) = 0;
156
157 while ((n = PQremove())) {
158 #ifdef DEBUG
159 fprintf (stderr, "process %d\n", n->index);
160 #endif
161 N_VAL(n) *= -1;
162 if (n == to) break;
163 for (y=0; y<n->n_adj; y++) {
164 e = &g->edges[n->adj_edge_list[y]];
165 adjn = adjacentNode(g, e, n);
166 if (N_VAL(adjn) < 0) {
167 d = -(N_VAL(n) + E_WT(e));
168 if (N_VAL(adjn) == UNSEEN) {
169 #ifdef DEBUG
170 fprintf (stderr, "new %d (%d)\n", adjn->index, -d);
171 #endif
172 N_VAL(adjn) = d;
173 if (PQ_insert(adjn)) return 1;
174 N_DAD(adjn) = n;
175 N_EDGE(adjn) = e;
176 }
177 else {
178 if (N_VAL(adjn) < d) {
179 #ifdef DEBUG
180 fprintf (stderr, "adjust %d (%d)\n", adjn->index, -d);
181 #endif
182 PQupdate(adjn, d);
183 N_DAD(adjn) = n;
184 N_EDGE(adjn) = e;
185 }
186 }
187 }
188 }
189 }
190
191 return 0;
192 }
193