Graphviz 13.1.2~dev.20250725.0048
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 <limits.h>
14#include <ortho/sgraph.h>
15#include <ortho/fPQ.h>
16#include <util/alloc.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 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
54sgraph *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
63snode*
65{
66 snode* np = g->nodes+g->nnodes;
67 np->index = g->nnodes;
68 g->nnodes++;
69 return np;
70}
71
72static void
73addEdgeToNode (snode* np, int idx)
74{
75 np->adj_edge_list[np->n_adj] = idx;
76 np->n_adj++;
77}
78
79sedge*
80createSEdge (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
97void
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
129static snode*
131{
132 if (e->v1==n->index)
133 return &g->nodes[e->v2];
134 else
135 return &g->nodes[e->v1];
136}
137
138int
139shortPath (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
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:180
#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:98
static void addEdgeToNode(snode *np, int idx)
Definition sgraph.c:73
sedge * createSEdge(sgraph *g, snode *v1, snode *v2, double wt)
Definition sgraph.c:80
int shortPath(sgraph *g, snode *from, snode *to)
Definition sgraph.c:139
void gsave(sgraph *G)
Definition sgraph.c:19
snode * createSNode(sgraph *g)
Definition sgraph.c:64
void reset(sgraph *G)
Definition sgraph.c:29
static snode * adjacentNode(sgraph *g, sedge *e, snode *n)
Definition sgraph.c:130
#define UNSEEN
Definition sgraph.c:127
sgraph * createSGraph(size_t nnodes)
Definition sgraph.c:54
void initSEdges(sgraph *g, size_t maxdeg)
Definition sgraph.c:40
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