-
Notifications
You must be signed in to change notification settings - Fork 0
/
11. Graph Traversal.c
331 lines (275 loc) · 6.15 KB
/
11. Graph Traversal.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*********************************************************************************
TITLE: Program to implement Graph Traversal.
*********************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#define S 50
//Stack is created
typedef struct
{
int b[S];
int tos;
}Stack;
//Queue is created
typedef struct
{
int a[S];
int front, rear;
}Queue;
//Graph is created
typedef struct
{
int adj[S][S];
int e,v;
}Graph;
//isEmpty function
int isEmpty(int front)
{
if(front==-1)
return 1;
else
return 0;
}
//Dequeue function
int dequeue(Queue *q)
{
int x=q->a[q->front];
if(q->front==q->rear)
{
q->front=q->rear=-1;
}
else
{
q->front++;
}
return x;
}
//Enqueue function
void enqueue(Queue *q, int e)
{
q->rear++;
q->a[q->rear]=e;
if(q->front==-1)
{
q->front=0;
}
}
//Push function
void push(Stack *s, int e)
{
s->tos++;
s->b[s->tos]=e;
}
//Pop function
int pop(Stack *s)
{
int x=s->b[s->tos];
s->tos--;
return x;
}
//initializing graph
void intializeGraph(Graph *g)
{
for(int i=0;i<g->v;i++)
{
for(int j=0;j<g->v;j++)
{
g->adj[i][j]=0;
}
}
}
//Add elements into the graph using adj matrix
void add(Graph *g, int src, int dest)
{
g->adj[src][dest]=1;
//g->adj[dest][src]=0; //for undirected graph
}
//DFS traversal using recursion method
void dfsTraveral(int root, Graph g)
{
int visitedArray[g.v];
for(int i=0;i<g.v;i++)
visitedArray[i]=0;
dfsRecursion(root,visitedArray,g); //Recursion method
}
//Recursion method for DFS traversal
void dfsRecursion(int root, int visitedArray[], Graph g)
{
visitedArray[root]=1;
printf("%d ",root);
for(int j=0;j<g.v;j++)
{
if(g.adj[root][j]==1 && visitedArray[j]!=1)
{
dfsRecursion(j,visitedArray,g);
}
}
}
//DFS traversal using iterative method
void dfsIterative(Stack *s, int root, Graph g)
{
int visitedArray[g.v];
for(int i=0;i<g.v;i++)
visitedArray[i]=0;
push(s,root);
while(!isEmpty(s->tos))
{
int x=pop(s);
if(visitedArray[x]!=1)
{
printf("%d ",x);
visitedArray[x]=1;
for(int j=0;j<g.v;j++)
{
if(g.adj[x][j]==1 && visitedArray[j]!=1)
{
push(s,j);
}
}
}
}
}
//BFS traversal using iterative method
void bfsTraversal(Queue *q, int root, Graph g)
{
int visitedArray[g.v];
for(int i=0;i<g.v;i++)
visitedArray[i]=0;
enqueue(q,root);
visitedArray[root]=1;
while(!isEmpty(q->front))
{
int x=dequeue(q);
printf("%d ",x);
for(int j=0;j<g.v;j++)
{
if(g.adj[x][j]==1 && visitedArray[j]!=1)
{
enqueue(q,j);
visitedArray[j]=1;
}
}
}
}
void printadjmatrix(Graph g)
{
for(int i=0;i<g.v;i++)
{
for(int j=0;j<g.v;j++)
{
printf("%d ",g.adj[i][j]);
}
printf("\n");
}
}
int main()
{
//Initializing different data structures
Graph g;
Queue q;
Stack s;
s.tos=-1;
int c;
q.front=q.rear=-1;
int root,src,dest;
//Inputing the no. of vertices
printf("Enter the no. of vertices for directed graph : ");
scanf("%d",&g.v);
//Inputing the no. of edges
printf("\nEnter the no. of edges for directed graph : ");
scanf("%d",&g.e);
//Entering the source code & destination code
for(int i=1;i<=g.e;i++)
{
printf("\nEnter the source node value : ");
scanf("%d",&src);
printf("\nEnter the destination node value : ");
scanf("%d",&dest);
add(&g,src,dest); //Adding elements into the graph
}
printf("\n***** Adjacency Matrix ******\n");
printadjmatrix(g);
do
{
//Choosing the option
printf("\nChoose your operation: 1.DFS Recursive 2.DFS Iterative 3.BFS Traversal 4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\nEnter the root : ");
scanf("%d",&root);
printf("\n***** DFS Recursive *****\n");
dfsTraveral(root,g);
break;
case 2:
printf("\nEnter the root : ");
scanf("%d",&root);
printf("\n***** DFS Iterative *****\n");
dfsIterative(&s,root,g);
break;
case 3:
printf("\nEnter the root : ");
scanf("%d",&root);
printf("\n***** BFS Traversal *****\n");
bfsTraversal(&q,root,g);
break;
case 4:
exit(0);
}
}while(1);
return 0;
}
/*********************************************************************************
OUTPUT:
Enter the no. of vertices for directed graph : 8
Enter the no. of edges for directed graph : 10
Enter the source node value : 0
Enter the destination node value : 1
Enter the source node value : 0
Enter the destination node value : 2
Enter the source node value : 0
Enter the destination node value : 3
Enter the source node value : 0
Enter the destination node value : 4
Enter the source node value : 1
Enter the destination node value : 5
Enter the source node value : 2
Enter the destination node value : 5
Enter the source node value : 3
Enter the destination node value : 6
Enter the source node value : 4
Enter the destination node value : 6
Enter the source node value : 5
Enter the destination node value : 7
Enter the source node value : 6
Enter the destination node value : 7
***** Adjacency Matrix ******
0 1 1 1 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
Choose your operation: 1.DFS Recursive 2.DFS Iterative 3.BFS Traversal 4.Exit
Enter your choice : 2
Enter the root : 0
***** DFS Iterative *****
0 4 6 7 3 2 5 1
Choose your operation: 1.DFS Recursive 2.DFS Iterative 3.BFS Traversal 4.Exit
Enter your choice : 1
Enter the root : 0
***** DFS Recursive *****
0 1 5 7 2 3 6 4
Choose your operation: 1.DFS Recursive 2.DFS Iterative 3.BFS Traversal 4.Exit
Enter your choice : 3
Enter the root : 0
***** BFS Traversal *****
0 1 2 3 4 5 6 7
Choose your operation: 1.DFS Recursive 2.DFS Iterative 3.BFS Traversal 4.Exit
Enter your choice : 4
Process returned 0
*********************************************************************************/