-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
427 lines (405 loc) · 8.42 KB
/
Graph.cpp
File metadata and controls
427 lines (405 loc) · 8.42 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
Name: Chirantan Joshi.
Div: G
Batch: G1
Roll No.: PG13
GRAPH TRAVERSALS WITH AND WITHOUT RECURSION
*/
#include<iostream>
using namespace std;
class gnode
{
int vertex;
gnode *next;
friend class graph;
friend class stack;
};
class queue //queue
{
int front,rear;
gnode *data[30];
public:
queue()
{
front=-1;
rear=-1;
}
void enq(gnode *temp) //enqueue
{
front++;
data[front]=temp;
}
gnode *deq() //dequeue
{
gnode *temp;
rear++;
temp=data[rear];
return temp;
}
int isempq()
{
if(front==rear)
return 1;
else
return 0;
}
friend class tree;
};
class stack //stack
{
int top;
gnode *data[30];
public:
stack()
{top=-1;}
void push(gnode *temp) //push
{
top++;
data[top]=temp;
}
gnode *pop() //pop
{
gnode *temp;
temp=data[top];
top--;
return temp;
}
int isemp()
{
if(top==-1)
return 1;
else
return 0;
}
friend class graph;
};
class graph //graph
{
gnode *head[20];
int n,i,visited[20][2];
public:
void create() //create
{
char choice,choice1;
int v;
gnode *curr;
n=0,i=0;
do //allocate memory for heads of all nodes
{
head[i]=new gnode;
cout<<"Enter the node value: ";
cin>>head[i]->vertex;
visited[i][0]=head[i]->vertex; //create visited array for checking if node is visited or not
cout<<"Do you want to add more node(y/n): ";
cin>>choice1;
n+=1;
i+=1;
}while(choice1=='y');
for(i=0;i<n;i++) //create linked list for each node
{
gnode *temp=head[i];
cout<<endl<<"Do you want to add node adjacent to "<<head[i]->vertex<<" (y/n): ";
cin>>choice;
while(choice=='y') //linked list will contain all adjacent nodes
{
cout<<"Enter the node adjacent to "<<head[i]->vertex<<": ";
cin>>v;
if(v==head[i]->vertex) //self loop is not allowed
cout<<"Self loop not allowed.";
else
{
curr=new gnode;
curr->vertex=v;
temp->next=curr;
temp=temp->next;
}
cout<<"Do you want to continue(y/n): ";
cin>>choice;
}
}
}
void DFS_r() //Depth first traversal of graph with recursion
{
for(i=0;i<n;i++) //mark all nodes as not visited
{
visited[i][1]=0;
}
int v;
cout<<endl<<"Enter the starting vertex: ";
cin>>v;
cout<<endl<<"Depth first traversal with recursion:"<<endl;
DFS_r(v);
}
void DFS_r(int v)
{
gnode *curr;
cout<<v<<endl; //print the node
for(i=0;i<n;i++)
{
if(visited[i][0]==v)
{
visited[i][1]=1; //mark node as visited
curr=head[i]; //assign a pointer to head of that node
break;
}
}
while(curr->next!=NULL) //for all adjacent vertices of node,
{ //repeat above 3 steps
curr=curr->next;
for(i=0;i<n;i++)
{
if(visited[i][0]==curr->vertex)
{
if(!visited[i][1])
{
DFS_r(curr->vertex);
break;
}
}
}
}
}
void DFS_nr()
{
for(i=0;i<n;i++) //mark all nodes as not visited
{
visited[i][1]=0;
}
int v;
gnode *curr;
stack s;
cout<<endl<<"Enter the starting vertex: ";
cin>>v;
cout<<endl<<"Depth first traversal without recursion:"<<endl;
for(i=0;i<n;i++)
{
if(visited[i][0]==v)
{
visited[i][1]=1;
curr=head[i];
break;
}
}
cout<<curr->vertex<<endl; //print the vertex and push it to stack
s.push(curr);
do
{
curr=s.pop(); //point the pointer to popped node
if(curr->next!=NULL)
s.push(curr->next); //push next of node to stack
curr=curr->next;
while(curr!=NULL) //continue for all adjacent nodes
{
for(i=0;i<n;i++)
{
if(visited[i][0]==curr->vertex)
{
if(!visited[i][1])
{
curr=head[i]; //attach pointer to head of vertex's node and print node
cout<<curr->vertex<<endl;
if(curr->next!=NULL) //push next of node to stack if not NULL
s.push(curr->next);
visited[i][1]=1; //mark current node as visited
break;
}
else
break;
}
}
curr=curr->next;
}
}while(!s.isemp());
}
void BFS() //breadth first traversal
{
for(i=0;i<n;i++) //mark all nodes as not visited
{
visited[i][1]=0;
}
int v;
gnode *curr;
queue q;
cout<<endl<<"Enter the starting vertex: ";
cin>>v;
cout<<endl<<"Breadth first traversal:"<<endl;
for(i=0;i<n;i++) //enqueue starting vertex and mark it as visited
{
if(visited[i][0]==v)
{
visited[i][1]=1;
curr=head[i]; //assign pointer to head of node
break;
}
}
q.enq(curr);
while(!q.isempq()) //continue till queue is empty
{
curr=q.deq(); //dequeue element and print node
cout<<curr->vertex<<endl;
for(i=0;i<n;i++)
{
if(visited[i][0]==curr->vertex)
{
curr=head[i]; //assign pointer to head of node
break;
}
}
while(curr!=NULL) //enqueue all adjacent nodes
{
for(i=0;i<n;i++)
{
if(visited[i][0]==curr->vertex)
{
if(!visited[i][1])
{
q.enq(curr);
visited[i][1]=1;
break;
}
else
break;
}
}
curr=curr->next;
}
}
}
};
int main()
{
graph p;
int choice;
while(1)
{
cout<<"\n********-----------MENU-----------********\n1.Create.\n2.Depth first traversal with recursion.\n3.Depth first traversal without recursion.\n4.Breadth first traversal.\n5.Exit.\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1: p.create();
break;
case 2: p.DFS_r();
break;
case 3: p.DFS_nr();
break;
case 4: p.BFS();
break;
case 5: exit(1);
break;
default:cout<<"Wrong choice!!";
}
}
return 0;
}
/*
OUTPUT:
********-----------MENU-----------********
1.Create.
2.Depth first traversal with recursion.
3.Depth first traversal without recursion.
4.Breadth first traversal.
5.Exit.
Enter your choice: 1
Enter the node value: 12
Do you want to add more node(y/n): y
Enter the node value: 6
Do you want to add more node(y/n): y
Enter the node value: 2
Do you want to add more node(y/n): y
Enter the node value: 8
Do you want to add more node(y/n): y
Enter the node value: 15
Do you want to add more node(y/n): y
Enter the node value: 4
Do you want to add more node(y/n): y
Enter the node value: 1
Do you want to add more node(y/n): y
Enter the node value: 20
Do you want to add more node(y/n): n
Do you want to add node adjacent to 12 (y/n): y
Enter the node adjacent to 12: 6
Do you want to continue(y/n): y
Enter the node adjacent to 12: 8
Do you want to continue(y/n): y
Enter the node adjacent to 12: 2
Do you want to continue(y/n): n
Do you want to add node adjacent to 6 (y/n): n
Do you want to add node adjacent to 2 (y/n): y
Enter the node adjacent to 2: 6
Do you want to continue(y/n): n
Do you want to add node adjacent to 8 (y/n): y
Enter the node adjacent to 8: 15
Do you want to continue(y/n): n
Do you want to add node adjacent to 15 (y/n): y
Enter the node adjacent to 15: 12
Do you want to continue(y/n): y
Enter the node adjacent to 15: 4
Do you want to continue(y/n): n
Do you want to add node adjacent to 4 (y/n): y
Enter the node adjacent to 4: 1
Do you want to continue(y/n): y
Enter the node adjacent to 4: 20
Do you want to continue(y/n): n
Do you want to add node adjacent to 1 (y/n): y
Enter the node adjacent to 1: 20
Do you want to continue(y/n): n
Do you want to add node adjacent to 20 (y/n): n
********-----------MENU-----------********
1.Create.
2.Depth first traversal with recursion.
3.Depth first traversal without recursion.
4.Breadth first traversal.
5.Exit.
Enter your choice: 2
Enter the starting vertex: 12
Depth first traversal with recursion:
12
6
8
15
4
1
20
2
********-----------MENU-----------********
1.Create.
2.Depth first traversal with recursion.
3.Depth first traversal without recursion.
4.Breadth first traversal.
5.Exit.
Enter your choice: 3
Enter the starting vertex: 12
Depth first traversal without recursion:
12
6
8
15
4
1
20
2
********-----------MENU-----------********
1.Create.
2.Depth first traversal with recursion.
3.Depth first traversal without recursion.
4.Breadth first traversal.
5.Exit.
Enter your choice: 4
Enter the starting vertex: 12
Breadth first traversal:
12
6
8
2
15
4
1
20
********-----------MENU-----------********
1.Create.
2.Depth first traversal with recursion.
3.Depth first traversal without recursion.
4.Breadth first traversal.
5.Exit.
Enter your choice: 5
*/