This repository has been archived by the owner on Mar 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.c
153 lines (127 loc) · 3.07 KB
/
graph.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdlib.h>
#include "graph.h"
/*
* Creates an empty graph
*/
graph_p create_graph() {
graph_p graph = (graph_p) malloc(sizeof(graph_t));
graph->num_vertices = 0;
graph->vertices = NULL;
}
/*
* Adds an vertex with data to a graph
*/
vertex_p add_vertex(graph_p graph, void *data) {
vertex_p v = (vertex_p) malloc(sizeof(vertex_t));
v->data = data;
v->neighbors = NULL;
v->next = graph->vertices;
v->flag = 0;
v->depth = 0;
v->parent = NULL;
graph->vertices = v;
return v;
}
/*
* Removes an vertex from a graph and returns its data
*/
void *remove_vertex(graph_p graph, vertex_p v) {
if (graph->vertices == v) {
graph->vertices = v->next;
}
vertex_p w = graph->vertices;
// remove all incoming edges
while (w) {
edge_p e = v->neighbors;
while (e) {
if (e->to == v) {
// there's a vertex w -> v, remove it
remove_edge(w, v);
}
e = e->next;
}
// if v is the following vertex in the list of all vertices, remove it from the list and skip it
if (w->next == v) {
w->next = v->next;
}
w = w->next;
}
// remove all outgoing edges
while (v->neighbors) {
remove_edge(v, v->neighbors->to);
}
void *data = v->data;
free(v);
return data;
}
/*
* Adds an edge from v1 to v2 into a graph and returns the pointer to the edge
*/
edge_p add_edge(vertex_p v1, vertex_p v2) {
edge_p e = v1->neighbors;
while (e) {
if (e->to == v2) {
// edge already exists
return;
}
e = e->next;
}
e = (edge_p) malloc(sizeof(edge_t));
e->to = v2;
e->next = v1->neighbors;
v1->neighbors = e;
return e;
}
/*
* Removes an edge from v1 to v2 from a graph
*/
void remove_edge(vertex_p v1, vertex_p v2) {
edge_p e = v1->neighbors;
if (e && e->to == v2) {
v1->neighbors = e->next;
free(e);
} else {
while (e) {
if (e->next && e->next->to == v2) {
edge_p f = e->next;
e->next = f->next;
free(f);
return;
}
e = e->next;
}
}
}
/*
* Resets all flags set in every vertex of a graph after BFS
*/
void reset(graph_p graph) {
vertex_p v = graph->vertices;
while (v) {
v->flag = 0;
v->depth = 0;
v->parent = NULL;
v = v->next;
}
}
/*
* Frees the memory allocated for the graph and the data represented by each vertex
*/
void free_graph(graph_p graph) {
vertex_p v = graph->vertices;
while (v) {
// remove all edges of this vertex
edge_p e = v->neighbors;
while (e) {
edge_p f = e->next;
free(e);
e = f;
}
// remove vertex and its data
vertex_p w = v->next;
free(v->data);
free(v);
v = w;
}
free(graph);
}