Repository navigation
Expand file tree
/
Copy pathgraphlist.h
More file actions
252 lines (233 loc) · 8.19 KB
/
Copy pathgraphlist.h
File metadata and controls
252 lines (233 loc) · 8.19 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
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
#ifndef GRAPHLIST_JL
#define GRAPHLIST_JL
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#include "../../../LinearDataStructures/DynamicDataStructure/LinkedQueue/linkedqueue.h"
#include "../../../LinearDataStructures/DynamicDataStructure/LinkedStack/linkedstack.h"
#define GRAPH_LIST_MAX_VERTICES 20 // The maximum number of vertices that our graph can have.
// Define how an edge of the adjacency list is structured.
typedef struct EdgeNode{
int vertex; // The vertex where the edge arrives.
int weight; // The weight of the edge.
struct EdgeNode* next; // The pointer to the next edge that leaves the same vertex.
} EdgeNode;
// Define how our (non linear and dynamic) graph with adjacency lists is structured.
// Each vertex has a linked list with its neighbours, sorted from the smallest to the greatest. The vertices are 0..vertices-1.
typedef struct{
EdgeNode* adjacency[GRAPH_LIST_MAX_VERTICES]; // The first edge of each vertex (null if it has no edges).
int vertices; // The number of vertices of our graph.
int directed; // 1 if the edges have a direction or 0 if they go both ways.
} GraphList;
/********************/
/* FUNCTIONS */
/********************/
// Set Up the Graph with a Number of Vertices and no Edges. Returns 1 if successful or 0 if the number of vertices is invalid.
int initGraphList(GraphList *graph, int vertices, int directed){
if(vertices < 1 || vertices > GRAPH_LIST_MAX_VERTICES){
return 0;
}
else{
graph->vertices = vertices;
graph->directed = directed;
for(int i = 0; i < GRAPH_LIST_MAX_VERTICES; i++){
graph->adjacency[i] = NULL;
}
return 1;
}
}
// Check if a Vertex Exists. Returns 1 if it exists or 0 if it doesn't.
int isValidVertexList(GraphList *graph, int vertex){
if(vertex >= 0 && vertex < graph->vertices){
return 1;
}
else{
return 0;
}
}
// Add the Edge from -> to in the List of from, keeping it sorted. Returns 1 if successful or 0 if there's no memory.
int insertEdgeNode(GraphList *graph, int from, int to, int weight){
EdgeNode* temp = graph->adjacency[from];
EdgeNode* prev = NULL;
while(temp != NULL && temp->vertex < to){
prev = temp;
temp = temp->next;
}
if(temp != NULL && temp->vertex == to){
temp->weight = weight;
return 1;
}
EdgeNode* newNode = (EdgeNode*)malloc(sizeof(EdgeNode));
if(!newNode){
return 0;
}
newNode->vertex = to;
newNode->weight = weight;
newNode->next = temp;
if(prev == NULL){
graph->adjacency[from] = newNode;
}
else{
prev->next = newNode;
}
return 1;
}
// Remove the Edge from -> to from the List of from. Returns 1 if successful or 0 if the edge doesn't exist.
int deleteEdgeNode(GraphList *graph, int from, int to){
EdgeNode* temp = graph->adjacency[from];
EdgeNode* prev = NULL;
while(temp != NULL && temp->vertex != to){
prev = temp;
temp = temp->next;
}
if(temp == NULL){
return 0;
}
if(prev == NULL){
graph->adjacency[from] = temp->next;
}
else{
prev->next = temp->next;
}
free(temp);
return 1;
}
// Add an Edge with a Weight (use 1 for unweighted graphs). If it already exists, its weight is updated.
// Returns 1 if successful or 0 if a vertex is invalid, the weight is not positive or there's no memory.
int addEdgeList(GraphList *graph, int from, int to, int weight){
if(!isValidVertexList(graph, from) || !isValidVertexList(graph, to) || weight <= 0){
return 0;
}
if(!insertEdgeNode(graph, from, to, weight)){
return 0;
}
if(!graph->directed && from != to && !insertEdgeNode(graph, to, from, weight)){
deleteEdgeNode(graph, from, to);
return 0;
}
return 1;
}
// Remove an Edge. Returns 1 if successful or 0 if the edge doesn't exist.
int removeEdgeList(GraphList *graph, int from, int to){
if(!isValidVertexList(graph, from) || !isValidVertexList(graph, to) || !deleteEdgeNode(graph, from, to)){
return 0;
}
if(!graph->directed && from != to){
deleteEdgeNode(graph, to, from);
}
return 1;
}
// Check if there's an Edge from one Vertex to Another. Returns 1 if there is or 0 if there isn't.
int hasEdgeList(GraphList *graph, int from, int to){
if(!isValidVertexList(graph, from)){
return 0;
}
for(EdgeNode* temp = graph->adjacency[from]; temp != NULL; temp = temp->next){
if(temp->vertex == to){
return 1;
}
}
return 0;
}
// Count the Edges that Leave a Vertex (its degree, or its out-degree if the graph is directed). Returns -1 if the vertex is invalid.
int degreeList(GraphList *graph, int vertex){
if(!isValidVertexList(graph, vertex)){
return -1;
}
int degree = 0;
for(EdgeNode* temp = graph->adjacency[vertex]; temp != NULL; temp = temp->next){
degree++;
}
return degree;
}
// Breadth-First Search: visit the start vertex, then its neighbours, then their neighbours... using a Queue.
// Writes the visited vertices in order. Returns 1 if successful or 0 if the start vertex is invalid or there's no memory.
int bfsGraphList(GraphList *graph, int start, int *order, int *count){
*count = 0;
if(!isValidVertexList(graph, start)){
return 0;
}
int visited[GRAPH_LIST_MAX_VERTICES] = {0};
LinkedQueue queue;
initLinkedQueue(&queue);
visited[start] = 1;
if(!enQueueLinked(&queue, start)){
return 0;
}
int vertex;
while(deQueueLinked(&queue, &vertex)){
order[(*count)++] = vertex;
for(EdgeNode* temp = graph->adjacency[vertex]; temp != NULL; temp = temp->next){
if(!visited[temp->vertex]){
visited[temp->vertex] = 1;
if(!enQueueLinked(&queue, temp->vertex)){
freeLinkedQueue(&queue);
return 0;
}
}
}
}
return 1;
}
// Depth-First Search: go as deep as possible before going back, using a Stack. The smallest neighbour is visited first.
// Writes the visited vertices in order. Returns 1 if successful or 0 if the start vertex is invalid or there's no memory.
int dfsGraphList(GraphList *graph, int start, int *order, int *count){
*count = 0;
if(!isValidVertexList(graph, start)){
return 0;
}
int visited[GRAPH_LIST_MAX_VERTICES] = {0};
LinkedStack stack;
initLinkedStack(&stack);
if(!pushLinkedStack(&stack, start)){
return 0;
}
int vertex;
while(popLinkedStack(&stack, &vertex)){
if(visited[vertex]){
continue;
}
visited[vertex] = 1;
order[(*count)++] = vertex;
// The list is sorted from the smallest to the greatest, so we push it backwards and the smallest is popped first.
int neighbours[GRAPH_LIST_MAX_VERTICES];
int total = 0;
for(EdgeNode* temp = graph->adjacency[vertex]; temp != NULL; temp = temp->next){
neighbours[total++] = temp->vertex;
}
for(int i = total-1; i >= 0; i--){
if(!visited[neighbours[i]] && !pushLinkedStack(&stack, neighbours[i])){
freeLinkedStack(&stack);
return 0;
}
}
}
return 1;
}
// Print the Adjacency Lists. Displays one line per vertex with its neighbours and weights: "0: [1 (4), 2 (1)]"
void printGraphList(GraphList *graph){
for(int i = 0; i < graph->vertices; i++){
printf(" %2d: [", i);
for(EdgeNode* temp = graph->adjacency[i]; temp != NULL; temp = temp->next){
printf("%d (%d)", temp->vertex, temp->weight);
if(temp->next != NULL){
printf(", ");
}
}
printf("]\n");
}
}
// Free all the Edges and Leave the Graph without Edges.
void freeGraphList(GraphList *graph){
for(int i = 0; i < GRAPH_LIST_MAX_VERTICES; i++){
EdgeNode* temp = graph->adjacency[i];
while(temp != NULL){
EdgeNode* nextNode = temp->next;
free(temp);
temp = nextNode;
}
graph->adjacency[i] = NULL;
}
}
#endif //GRAPHLIST_JL