-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.c
281 lines (246 loc) · 6.04 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "set.h"
#include "graph.h"
/*
* @param: adjacent
* after return, adjacent will be pointed directly to vertex's adjlist::adjacent
*/
static int get_adjacent(Graph *gp, const void *vertex, Set **adjacent);
static int get_adjlist(Graph *gp, const void *vertex, AdjList **adjlist);
static int adjlist_match(const void *data1, const void *data2);
void graph_init(Graph *gp, void (*destroy)(void *data), int (*match)(const void *data1, const void *data2)){
gp->ecount = 0;
gp->vcount = 0;
gp->destroy = destroy;
gp->match = match;
list_init(&(gp->adjtable), NULL, adjlist_match);
}
void graph_destroy(Graph *gp){
AdjList *adjlist = NULL;
while(list_len(&(gp->adjtable)) > 0){
if(list_pop(&(gp->adjtable), (void **)&adjlist) == 0){
/* Destroy Set's item, actually just memset Set, and
Set is a auto variable
*/
set_destroy(&(adjlist->adjacent));
if(gp->destroy != NULL){
gp->destroy(adjlist->vertex);
}
/* The responsibility of Manage adjlist by Library Author */
free(adjlist);
}
}
/* actually just memset ajatable(is a auto variable) */
list_destroy(&(gp->adjtable));
memset(gp, 0, sizeof(*gp));
}
int graph_get_adjacent(Graph *gp, const void *vertex, Set *adjacent){
int retval;
Set *temp = NULL;
if((retval = get_adjacent(gp, vertex, &temp)) != 0){
return retval;
}
if((retval = set_copy_to(temp, adjacent)) != 0){
return retval;
}
return 0;
}
int graph_is_adjacent(Graph *gp, const void *vertex1, const void *vertex2){
/* if vertex1 or vertex2 is not existed */
Set *adjacent1, *adjacent2;
get_adjacent(gp, vertex1, &adjacent1);
if(adjacent1 == NULL){
return 0;
}
get_adjacent(gp, vertex2, &adjacent2);
if(adjacent2 == NULL){
return 0;
}
int v1has2 = set_is_member(adjacent1, vertex2);
int v2has1 = set_is_member(adjacent2, vertex1);
if(v1has2 && v2has1){
return 2;
}
else if(v1has2){
return 1;
}
else if(v2has1){
return -1;
}
else{
return 0;
}
}
int graph_add_vertex(Graph *gp, const void *vertex){
AdjList *adjlist = NULL;
get_adjlist(gp, vertex, &adjlist);
if(adjlist != NULL){
/* vertex existed, don't allow insertion duplicate vertices */
return 1;
}
if((adjlist = (AdjList *)malloc(sizeof(AdjList))) == NULL){
return -1;
}
adjlist->vertex = (void *)vertex;
set_init(&(adjlist->adjacent), NULL, gp->match);
if(list_push(&(gp->adjtable), adjlist) != 0){
free(adjlist);
return -1;
}
gp->vcount++;
return 0;
}
int graph_add_edge(Graph *gp, const void *from, const void *to){
Set *adjacent = NULL;
/* if vertices not in graph, dont allow insertion */
get_adjacent(gp, to, &adjacent);
if(adjacent == NULL){
return -1;
}
adjacent = NULL;
get_adjacent(gp, from, &adjacent);
/* NOW: adjacent is owned by [from] */
if(adjacent == NULL){
return -1;
}
int retval;
if((retval = set_insert(adjacent, to)) != 0){
/* existed, will return 1 */
return retval;
}
gp->ecount++;
return 0;
}
int graph_drop_vertex(Graph *gp, const void *vertex){
AdjList *adjlist = NULL;
get_adjlist(gp, vertex, &adjlist);
if(adjlist == NULL){
/* vertext is not existed */
return 1;
}
/* remove all adjacent */
AdjList *temp = NULL;
Node *pNode = list_head(&(gp->adjtable));
while(pNode != NULL){
temp = (AdjList *)list_node_data(pNode);
pNode = list_node_next(pNode);
if(temp == adjlist){
continue;
}
set_remove(&(temp->adjacent), vertex);
}
/* remove adjlist */
if(gp->destroy != NULL){
gp->destroy(adjlist->vertex);
}
set_destroy(&(adjlist->adjacent));
list_remove_by_data(&(gp->adjtable), adjlist);
free(adjlist);
gp->vcount--;
return 0;
}
int graph_remove_vertex(Graph *gp, const void *vertex){
AdjList *adjlist = NULL;
get_adjlist(gp, vertex, &adjlist);
if(adjlist == NULL){
/* vertext is not existed */
return 1;
}
AdjList *temp = NULL;
Node *pNode = list_head(&(gp->adjtable));
while(pNode != NULL){
temp = (AdjList *)list_node_data(pNode);
pNode = list_node_next(pNode);
if(temp == adjlist){
continue;
}
if(set_is_member(&(temp->adjacent), vertex)){
/* other verteics adjacent the vertex, so cannot remove it */
return -1;
}
}
/* remove adjlist */
if(gp->destroy != NULL){
gp->destroy(adjlist->vertex);
}
set_destroy(&(adjlist->adjacent));
list_remove_by_data(&(gp->adjtable), adjlist);
free(adjlist);
gp->vcount--;
return 0;
}
int graph_remove_edge(Graph *gp, const void *from, const void *to){
Set *adjacent = NULL;
/* if vertices not in graph, dont allow insertion */
get_adjacent(gp, to, &adjacent);
if(adjacent == NULL){
return -1;
}
/* NOTICE: from test must be after to test,
so adjlist store from list pointer
*/
get_adjacent(gp, from, &adjacent); /* adjacent is from's adjacent */
if(adjacent == NULL){
return -1;
}
int retval;
if((retval = set_remove(adjacent, to)) != 0){
/* if not in, return 1 */
return retval;
}
gp->ecount--;
return 0;
}
void graph_dump(Graph *gp, void (*print)(const void *data)){
AdjList *adjlist = NULL;
Set *adjacent = NULL;
Node *pNode = list_head(&(gp->adjtable));
while(pNode != NULL){
adjlist = (AdjList *)list_node_data(pNode);
print(adjlist->vertex);
printf(": ");
get_adjacent(gp, adjlist->vertex, &adjacent);
set_dump(adjacent, print);
printf("\n");
pNode = list_node_next(pNode);
}
}
/*
* Internel Functions
*/
int get_adjlist(Graph *gp, const void *vertex, AdjList **adjlist){
if(graph_is_empty(gp)){
*adjlist = NULL;
return -1;
}
AdjList *temp = NULL;
Node *pNode = list_head(&(gp->adjtable));
while(pNode != NULL){
temp = (AdjList *)list_node_data(pNode);
if(gp->match(temp->vertex, vertex)){
*adjlist = temp;
return 0;
}
pNode = list_node_next(pNode);
}
*adjlist = NULL;
return -1;
}
int get_adjacent(Graph *gp, const void *vertex, Set **adjacent){
AdjList *adjlist = NULL;
int retval;
if((retval = get_adjlist(gp, vertex, &adjlist)) == 0){
*adjacent = &(adjlist->adjacent);
}
else{
return retval;
}
}
int adjlist_match(const void *data1, const void *data2){
if(((AdjList *)data1)->vertex == ((AdjList *)data2)->vertex){
return 1;
}
return 0;
}