This repository was archived by the owner on Aug 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 740
/
Copy pathgraph_implementation_in_C_bfs_dfs.c
192 lines (161 loc) · 4.04 KB
/
graph_implementation_in_C_bfs_dfs.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
/**
* This is a Implementation of Undirected Graph in C using Both BFS (Breadth First Search) & DFS(Depth First Search).
*
*/
#include<stdio.h>
#include<stdlib.h>
#define SIZE 40
struct node{
int vertex;
struct node* next;
};
//Creating a Node
struct node* createNode(int v){
struct node* newNode = malloc( sizeof( struct node ) );
newNode->vertex = v;
newNode->next = NULL;
return newNode;
};
struct Graph{
int numVertices;
struct node** adjLists;
int* visited;
};
//Creating a Graph
struct Graph* createGraph(int vertices){
struct Graph* graph = malloc( sizeof( struct Graph ) );
graph->numVertices = vertices;
graph->adjLists = malloc( vertices * sizeof( struct node* ));
graph->visited = malloc( vertices * sizeof(int) );
int i ;
for( i = 0; i < vertices; i++){
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
// Adding a Edge in a Graph
void addEdge(struct Graph* graph, int src, int dest){
//Adding edge from source to destination
struct node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
//Adding edge from destination to source
newNode = createNode( src );
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
struct queue{
int items[SIZE];
int front;
int rear;
};
// Creating a Queue
struct queue* createQueue(){
struct queue* q = malloc( sizeof( struct queue) );
q->front = -1;
q->rear = -1;
return q;
};
//Adding Elements into a Queue
void enqueue( struct queue* q, int value ){
if( q->rear == SIZE - 1 )
printf("Queue is Full !!!");
else{
if( q->front == -1)
q->front = 0;
q->rear++;
q->items[ q->rear ] = value;
}
}
// Check weather a queue is empty
int isEmpty( struct queue* q ){
if( q->rear == -1 )
return 1;
else
return 0;
}
//Removing Elements From Queue
int dequeue( struct queue* q ){
int item;
if( isEmpty( q ) ){
printf("Queue is Empty");
item = -1;
} else {
item = q->items[q->front];
q->front++;
if( q->front > q->rear ){
// printf("Resetting Queue");
q->front = q->rear = -1;
}
}
return item;
}
// Print the queue
void printQueue( struct queue* q ){
int i = q->front;
if( isEmpty(q) ){
printf("Queue is Empty");
}else{
printf("\nQueue Front: ");
for( i = q->front; i < q->rear ; i++){
printf("%d", q->items[i]);
}
}
}
// Breadth First Search Algorithm
void bfs( struct Graph* graph, int startVertex ){
struct queue* q = createQueue();
graph->visited[ startVertex ] = 1;
enqueue( q, startVertex );
while ( !isEmpty(q) ){
// printQueue( q );
int currentVertex = dequeue( q );
printf("Visited : %d \n", currentVertex);
struct node* temp = graph->adjLists[currentVertex];
while( temp ){
int adjVertex = temp->vertex;
if( graph->visited[adjVertex] == 0 ){
graph->visited[adjVertex] = 1;
enqueue( q, adjVertex );
}
temp = temp->next;
}
}
}
//Depth First Search Algorithm
void DFS(struct Graph* graph, int vertex){
struct node* adjList = graph->adjLists[vertex];
struct node* temp = adjList;
graph->visited[vertex] = 1;
printf("Visited %d \n", vertex);
while( temp != NULL){
int connectedVertex = temp->vertex;
if( graph->visited[connectedVertex] == 0 ){
DFS(graph , connectedVertex);
}
temp = temp->next;
}
}
// It is the main calling function.
int main(){
int vertices=0, edges = 0, src = 0 , dest = 0;
printf("Enter the Number of Vertices:");
scanf("%d", &vertices );
struct Graph* graph = createGraph(vertices);
printf("Enter the Number of Edges in a Graph: ");
scanf("%d", &edges );
for( int i = 0 ; i < edges ; i++ ){
printf("Enter the Source and Destination of Edge %d :", i+1);
scanf("%d%d", &src, &dest);
addEdge( graph , src , dest);
}
printf("\n<!-------Using Breadth First Search-------!>\n");
bfs( graph, 0);
for( int i = 0; i < vertices; i++ ){
graph->visited[i] = 0;
}
printf("\n<!-------Using Deapth First Search-------!>\n");
DFS(graph, 2);
return 0;
}