/
graph.c
186 lines (166 loc) · 3.87 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
#include "graph.h"
#include "queue.h"
#include "stack.h"
void
dfsTraversal(struct graph *graph) {
printf("DFS Traversal\n");
int n_visited = 0;
int visited_vertex[MAX_VERTICES];
struct stack *stack = createStack();
push(stack, &graph->vertices[0]);
struct vertex *vertex = NULL;
while(stack->top != NULL && (n_visited < graph->n_vertices)) {
vertex = (struct vertex *) pop(stack);
int i;
bool isVisited = false;
for (i = 0; i < n_visited; i++) {
if (visited_vertex[i] == vertex->data) {
isVisited = true;
break;
}
}
if (!isVisited) {
printf("%d ", vertex->data);
visited_vertex[n_visited] = vertex->data;
n_visited++;
struct linkedList *list = vertex->adjList;
if(list != NULL && list->head != NULL) {
struct llNode *node = list->head;
do {
push(stack, node->data);
node = node->next;
} while (node != NULL);
}
}
}
printf("\n");
}
void
bfsTraversal(struct graph *graph) {
printf("BFS Traversal\n");
int n_visited = 0;
int visited_vertex[MAX_VERTICES];
struct queue *queue = createQueue();
enqueue(queue, &graph->vertices[0]);
struct vertex *vertex = NULL;
while((queue->begin != NULL && queue->end != NULL) && (n_visited < graph->n_vertices)) {
vertex = (struct vertex *) dequeue(queue);
int i;
bool isVisited = false;
for (i = 0; i < n_visited; i++) {
if (visited_vertex[i] == vertex->data) {
isVisited = true;
break;
}
}
if (!isVisited) {
printf("%d ", vertex->data);
visited_vertex[n_visited] = vertex->data;
n_visited++;
struct linkedList *list = vertex->adjList;
if(list != NULL && list->head != NULL) {
struct llNode *node = list->head;
do {
enqueue(queue, node->data);
node = node->next;
} while (node != NULL);
}
}
}
printf("\n");
}
void
createVertex(struct vertex *vertex, int data) {
vertex->data = data;
vertex->adjList = createLinkedList();
}
void
addEdge(struct graph *graph, int src, int dst) {
int i;
bool srcFound = false;
bool dstFound = false;
struct vertex *srcVertex = NULL;
struct vertex *dstVertex = NULL;
for (i = 0; i < graph->n_vertices; i++) {
if (graph->vertices[i].data == src) {
srcFound = true;
srcVertex = &graph->vertices[i];
if (dstFound) {
break;
}
}
if (graph->vertices[i].data == dst) {
dstFound = true;
dstVertex = &graph->vertices[i];
if (srcFound){
break;
}
}
}
if (srcFound && dstFound) {
addToLLEnd(srcVertex->adjList, dstVertex);
} else {
printf("Invalid vertices. src:%d dst:%d\n", srcFound, dstFound);
}
}
void
addVertex(struct graph *graph, int data) {
if (graph->n_vertices < MAX_VERTICES) {
createVertex(&graph->vertices[graph->n_vertices], data);
graph->n_vertices++;
} else {
printf("Vertex limit reached!\n");
}
}
printAdjList(struct linkedList *list) {
if(list->head != NULL) {
struct llNode *node = list->head;
do {
struct vertex *vertex = (struct vertex *) node->data;
printf(" -> %d", vertex->data);
node = node->next;
} while (node != NULL);
}
}
void
printGraph(struct graph *graph) {
int i;
for (i = 0; i < graph->n_vertices; i++) {
printf("%d)", graph->vertices[i].data);
printAdjList(graph->vertices[i].adjList);
printf("\n");
}
}
struct graph *
createGraph() {
struct graph *graph = malloc(sizeof(struct graph));
if (graph == NULL) {
printf("Error allocating memory.\n");
return NULL;
}
graph->n_vertices = 0;
return graph;
}
void main() {
struct graph *graph = createGraph();
addVertex(graph, 1);
addVertex(graph, 2);
addVertex(graph, 3);
addVertex(graph, 4);
addVertex(graph, 5);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 1);
addEdge(graph, 2, 3);
addEdge(graph, 3, 2);
addEdge(graph, 3, 1);
addEdge(graph, 4, 1);
addEdge(graph, 5, 2);
addEdge(graph, 5, 3);
addEdge(graph, 3, 5);
addEdge(graph, 2, 5);
printGraph(graph);
bfsTraversal(graph);
dfsTraversal(graph);
}