-
Notifications
You must be signed in to change notification settings - Fork 0
/
digraph.c
42 lines (33 loc) · 871 Bytes
/
digraph.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "digraph.h"
void digraph_init(digraph_t *digraph, size_t num_nodes, size_t out_degree)
{
digraph->num_nodes = num_nodes;
digraph->out_degree = out_degree;
digraph->graph = malloc(num_nodes * out_degree * sizeof(void*));
}
void digraph_free(digraph_t *digraph)
{
free(digraph->graph);
}
bool digraph_has_edge(digraph_t *digraph, size_t node, size_t target)
{
size_t i;
for (i = 0; i < digraph->out_degree; i++) {
if (DG_NODE(digraph, node, i) == target)
return true;
}
return false;
}
void digraph_fprint(FILE *stream, digraph_t *digraph)
{
fprintf(stream, "digraph g {\n"
"\tnode [shape=\"point\"];\n");
size_t i;
size_t j;
for (i = 0; i < digraph->num_nodes; i++) {
for (j = 0; j < digraph->out_degree; j++) {
fprintf(stream, "\t%d -> %d;\n", i, DG_NODE(digraph, i, j));
}
}
fprintf(stream, "}\n");
}