Graphviz 12.0.1~dev.20240716.0800
Loading...
Searching...
No Matches
sgraph.c
Go to the documentation of this file.
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 <cgraph/alloc.h>
14#include <limits.h>
15#include <ortho/sgraph.h>
16#include <ortho/fPQ.h>
17
18void
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
28void
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
40void
41initSEdges (sgraph* g, int maxdeg)
42{
43 int i;
44 int* adj = gv_calloc(6 * g->nnodes + 2 * maxdeg, sizeof(int));
45 g->edges = gv_calloc(3 * g->nnodes + maxdeg, sizeof(sedge));
46 for (i = 0; i < g->nnodes; i++) {
47 g->nodes[i].adj_edge_list = adj;
48 adj += 6;
49 }
50 for (; i < g->nnodes+2; i++) {
51 g->nodes[i].adj_edge_list = adj;
52 adj += maxdeg;
53 }
54}
55
56sgraph*
57createSGraph (int nnodes)
58{
59 sgraph* g = gv_alloc(sizeof(sgraph));
60
61 /* create the nodes vector in the search graph */
62 g->nnodes = 0;
63 g->nodes = gv_calloc(nnodes, sizeof(snode));
64 return g;
65}
66
67snode*
69{
70 snode* np = g->nodes+g->nnodes;
71 np->index = g->nnodes;
72 g->nnodes++;
73 return np;
74}
75
76static void
77addEdgeToNode (snode* np, int idx)
78{
79 np->adj_edge_list[np->n_adj] = idx;
80 np->n_adj++;
81}
82
83sedge*
84createSEdge (sgraph* g, snode* v1, snode* v2, double wt)
85{
86 sedge* e;
87 int idx = g->nedges++;
88
89 e = g->edges + idx;
90 e->v1 = v1->index;
91 e->v2 = v2->index;
92 e->weight = wt;
93 e->cnt = 0;
94
95 addEdgeToNode (v1, idx);
96 addEdgeToNode (v2, idx);
97
98 return e;
99}
100
101void
103{
104 free (g->nodes[0].adj_edge_list);
105 free (g->nodes);
106 free (g->edges);
107 free (g);
108}
109
110#include <ortho/fPQ.h>
111
112/* shortest path:
113 * Constructs the path of least weight between from and to.
114 *
115 * Assumes graph, node and edge type, and that nodes
116 * have associated values N_VAL, N_IDX, and N_DAD, the first two
117 * being ints, the last being a node*. Edges have a E_WT function
118 * to specify the edge length or weight.
119 *
120 * Assumes there are functions:
121 * agnnodes: graph -> int number of nodes in the graph
122 * agfstnode, agnxtnode : iterators over the nodes in the graph
123 * agfstedge, agnxtedge : iterators over the edges attached to a node
124 * adjacentNode : given an edge e and an endpoint n of e, returns the
125 * other endpoint.
126 *
127 * The path is given by
128 * to, N_DAD(to), N_DAD(N_DAD(to)), ..., from
129 */
130
131#define UNSEEN INT_MIN
132
133static snode*
135{
136 if (e->v1==n->index)
137 return &g->nodes[e->v2];
138 else
139 return &g->nodes[e->v1];
140}
141
142int
143shortPath (sgraph* g, snode* from, snode* to)
144{
145 snode* n;
146 sedge* e;
147 snode* adjn;
148 int d;
149 int x, y;
150
151 for (x = 0; x<g->nnodes; x++) {
152 snode* temp = &g->nodes[x];
153 N_VAL(temp) = UNSEEN;
154 }
155
156 PQinit();
157 if (PQ_insert (from)) return 1;
158 N_DAD(from) = NULL;
159 N_VAL(from) = 0;
160
161 while ((n = PQremove())) {
162#ifdef DEBUG
163 fprintf (stderr, "process %d\n", n->index);
164#endif
165 N_VAL(n) *= -1;
166 if (n == to) break;
167 for (y=0; y<n->n_adj; y++) {
168 e = &g->edges[n->adj_edge_list[y]];
169 adjn = adjacentNode(g, e, n);
170 if (N_VAL(adjn) < 0) {
171 d = -(N_VAL(n) + E_WT(e));
172 if (N_VAL(adjn) == UNSEEN) {
173#ifdef DEBUG
174 fprintf (stderr, "new %d (%d)\n", adjn->index, -d);
175#endif
176 N_VAL(adjn) = d;
177 if (PQ_insert(adjn)) return 1;
178 N_DAD(adjn) = n;
179 N_EDGE(adjn) = e;
180 }
181 else {
182 if (N_VAL(adjn) < d) {
183#ifdef DEBUG
184 fprintf (stderr, "adjust %d (%d)\n", adjn->index, -d);
185#endif
186 PQupdate(adjn, d);
187 N_DAD(adjn) = n;
188 N_EDGE(adjn) = e;
189 }
190 }
191 }
192 }
193 }
194
195 return 0;
196}
197
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
static void * gv_alloc(size_t size)
Definition alloc.h:47
void PQinit(void)
Definition fPQ.c:44
snode * PQremove(void)
Definition fPQ.c:121
int PQ_insert(snode *np)
Definition fPQ.c:80
void PQupdate(snode *n, int d)
Definition fPQ.c:137
#define G
Definition gdefs.h:7
void free(void *)
node NULL
Definition grammar.y:149
#define N_DAD(n)
#define E_WT(e)
#define N_VAL(pq, n)
snode priority queue for shortPath in sgraph
#define N_EDGE(n)
Definition fPQ.h:24
void freeSGraph(sgraph *g)
Definition sgraph.c:102
static void addEdgeToNode(snode *np, int idx)
Definition sgraph.c:77
void initSEdges(sgraph *g, int maxdeg)
Definition sgraph.c:41
sedge * createSEdge(sgraph *g, snode *v1, snode *v2, double wt)
Definition sgraph.c:84
int shortPath(sgraph *g, snode *from, snode *to)
Definition sgraph.c:143
void gsave(sgraph *G)
Definition sgraph.c:19
snode * createSNode(sgraph *g)
Definition sgraph.c:68
void reset(sgraph *G)
Definition sgraph.c:29
static snode * adjacentNode(sgraph *g, sedge *e, snode *n)
Definition sgraph.c:134
#define UNSEEN
Definition sgraph.c:131
sgraph * createSGraph(int nnodes)
Definition sgraph.c:57
Definition sgraph.h:42
double weight
Definition sgraph.h:43
int cnt
Definition sgraph.h:44
int v2
Definition sgraph.h:48
int v1
Definition sgraph.h:48
int nedges
Definition sgraph.h:52
sedge * edges
Definition sgraph.h:55
int nnodes
Definition sgraph.h:52
snode * nodes
Definition sgraph.h:54
a node of search graph sgraph, is created as a border segment between two adjusted cells of type cell...
Definition sgraph.h:26
short n_adj
Definition sgraph.h:30
int index
Definition sgraph.h:38
int * adj_edge_list
edges incident on this node – stored as indices of the edges array in the graph
Definition sgraph.h:37