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