-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest3.c
More file actions
87 lines (68 loc) · 1.8 KB
/
test3.c
File metadata and controls
87 lines (68 loc) · 1.8 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "bitmap.h"
typedef struct {
Bitmap_t **edges;
size_t n_edges;
} Graph_t;
static void Graph_init(Graph_t *g, size_t n_edges)
{
g->n_edges = n_edges;
g->edges = malloc(n_edges * sizeof(Bitmap_t **));
for (size_t i = 0; i < n_edges; i++) {
g->edges[i] = malloc(sizeof(Bitmap_t));
Bitmap_init(g->edges[i], n_edges);
}
}
static void Graph_free(Graph_t *g)
{
for (size_t i = 0; i < g->n_edges; i++) {
Bitmap_free(g->edges[i]);
free(g->edges[i]);
}
free(g->edges);
}
static inline void Graph_insert_arc(Graph_t *g, int a, int b)
{
Bitmap_set(g->edges[a], b, 1);
}
static inline void Graph_remove_arc(Graph_t *g, int a, int b)
{
Bitmap_set(g->edges[a], b, 0);
}
static inline int Graph_is_arc(Graph_t *g, int a, int b)
{
return Bitmap_at(g->edges[a], b) != 0;
}
static int Graph_is_threestep(Graph_t *g, int a, int b)
{
for (size_t i = 0; i < g->n_edges; i++) {
if (Graph_is_arc(g, a, i) && Graph_is_arc(g, i, b))
return 1;
}
return 0;
}
int main(int argc, char *argv[])
{
Graph_t g;
enum {A, B, C, N_NODES};
/* init graph 'g' with N_NODES nodes */
Graph_init(&g, N_NODES);
/* connect node 'A' to node 'B' */
Graph_insert_arc(&g, A, B);
/* connect node 'B' to node 'C' */
Graph_insert_arc(&g, B, C);
/* arc from 'A' to 'B' */
if (Graph_is_arc(&g, A, B))
puts("There is an arc from A to B in G");
/* arc from 'B' to 'C' */
if (Graph_is_arc(&g, B, C))
puts("There is an arc from B to C in G");
/* threestep from 'A' to 'C' */
if (Graph_is_threestep(&g, A, C))
puts("There is a threestep from A to C in G");
/* free the graph 'g' */
Graph_free(&g);
return 0;
}