Graphviz 13.1.3~dev.20250829.0113
Loading...
Searching...
No Matches
rawgraph.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#include <ortho/rawgraph.h>
12#include <stddef.h>
13#include <util/alloc.h>
14#include <util/list.h>
15
16#define UNSCANNED 0
17#define SCANNING 1
18#define SCANNED 2
19
20rawgraph *make_graph(size_t n) {
21 rawgraph* g = gv_alloc(sizeof(rawgraph));
22 g->nvs = n;
23 g->vertices = gv_calloc(n, sizeof(vertex));
24 for(size_t i = 0; i < n; ++i) {
25 g->vertices[i].color = UNSCANNED;
26 }
27 return g;
28}
29
30void
32{
33 for(size_t i = 0; i < g->nvs; ++i)
35 free (g->vertices);
36 free (g);
37}
38
39void insert_edge(rawgraph *g, size_t v1, size_t v2) {
40 if (!edge_exists(g, v1, v2)) {
41 LIST_APPEND(&g->vertices[v1].adj_list, v2);
42 }
43}
44
45void remove_redge(rawgraph *g, size_t v1, size_t v2) {
46 LIST_REMOVE(&g->vertices[v1].adj_list, v2);
47 LIST_REMOVE(&g->vertices[v2].adj_list, v1);
48}
49
50bool edge_exists(rawgraph *g, size_t v1, size_t v2) {
51 return LIST_CONTAINS(&g->vertices[v1].adj_list, v2);
52}
53
54typedef LIST(size_t) int_stack_t;
55
56static int DFS_visit(rawgraph *g, size_t v, int time, int_stack_t *sp) {
57 vertex* vp;
58
59 vp = g->vertices + v;
60 vp->color = SCANNING;
61 const adj_list_t adj = vp->adj_list;
62 time = time + 1;
63
64 for (size_t i = 0; i < LIST_SIZE(&adj); ++i) {
65 const size_t id = LIST_GET(&adj, i);
66 if(g->vertices[id].color == UNSCANNED)
67 time = DFS_visit(g, id, time, sp);
68 }
69 vp->color = SCANNED;
70 LIST_PUSH_BACK(sp, v);
71 return time + 1;
72}
73
74void
76{
77 int time = 0;
78 int count = 0;
79
80 if (g->nvs == 0) return;
81 if (g->nvs == 1) {
82 g->vertices[0].topsort_order = count;
83 return;
84 }
85
86 int_stack_t sp = {0};
87 LIST_RESERVE(&sp, g->nvs);
88 for(size_t i = 0; i < g->nvs; ++i) {
89 if(g->vertices[i].color == UNSCANNED)
90 time = DFS_visit(g, i, time, &sp);
91 }
92 while (!LIST_IS_EMPTY(&sp)) {
93 const size_t v = LIST_POP_BACK(&sp);
94 g->vertices[v].topsort_order = count;
95 count++;
96 }
97 LIST_FREE(&sp);
98}
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 free(void *)
type-generic dynamically expanding list
#define LIST(type)
Definition list.h:55
#define LIST_SIZE(list)
Definition list.h:80
#define LIST_APPEND(list, item)
Definition list.h:132
#define LIST_FREE(list)
Definition list.h:379
#define LIST_RESERVE(list, capacity)
Definition list.h:266
#define LIST_POP_BACK(list)
Definition list.h:416
#define LIST_CONTAINS(list, needle)
Definition list.h:282
#define LIST_IS_EMPTY(list)
Definition list.h:90
#define LIST_PUSH_BACK(list, item)
Definition list.h:393
#define LIST_REMOVE(list, item)
Definition list.h:227
#define LIST_GET(list, index)
Definition list.h:165
rawgraph * make_graph(size_t n)
makes a graph with n vertices, 0 edges
Definition rawgraph.c:20
#define SCANNING
Definition rawgraph.c:17
void insert_edge(rawgraph *g, size_t v1, size_t v2)
inserts edge FROM v1 to v2
Definition rawgraph.c:39
#define SCANNED
Definition rawgraph.c:18
void remove_redge(rawgraph *g, size_t v1, size_t v2)
removes any edge between v1 to v2 – irrespective of direction
Definition rawgraph.c:45
void top_sort(rawgraph *g)
Definition rawgraph.c:75
bool edge_exists(rawgraph *g, size_t v1, size_t v2)
tests if there is an edge FROM v1 TO v2
Definition rawgraph.c:50
void free_graph(rawgraph *g)
Definition rawgraph.c:31
#define UNSCANNED
Definition rawgraph.c:16
size_t nvs
Definition rawgraph.h:26
vertex * vertices
Definition rawgraph.h:27
Definition legal.c:31
int topsort_order
Definition rawgraph.h:21
adj_list_t adj_list
adjacency list
Definition rawgraph.h:22
int color
Definition rawgraph.h:20