-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment-4.cpp
More file actions
352 lines (325 loc) · 4.67 KB
/
Copy pathassignment-4.cpp
File metadata and controls
352 lines (325 loc) · 4.67 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
#include<iostream>
#define MAX 20
using namespace std;
class node
{
int data;
node *next;
friend class graph;
friend class queue;
friend class stack;
};
class queue
{
node *start=NULL;
public:
void insert(int num)
{
node *q=start;
node *temp=new node;
temp->data=num;
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
while(q->next!=NULL)
{
q=q->next;
}
q->next=temp;
}
}
bool isempty()
{
if(start==NULL)
return true;
else
return false;
}
void del()
{
if(start==NULL)
cout<<"Queue underflow!\n";
else
{
node *temp=start;
start=temp->next;
delete temp;
}
}
int front()
{
return start->data;
}
};
class stack
{
node *start=NULL;
public:
void push(int num)
{
node *temp=new node;
temp->data=num;
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
temp->next=start;
start=temp;
}
}
bool isempty()
{
if(start==NULL)
return true;
else
return false;
}
void pop()
{
if(start==NULL)
cout<<"Queue underflow!\n";
else
{
node *temp=start;
start=temp->next;
delete temp;
}
}
int top()
{
return start->data;
}
};
class graph
{
int matrix[MAX][MAX];
int edge,vert,matcnt,adjcnt;
node *adjlist[MAX];
bool visit[MAX];
public:
graph()
{
for(int i=0;i<MAX;i++)
{
adjlist[i]=NULL;
for(int j=0;j<MAX;j++)
{
matrix[i][j]=0;
}
}
matcnt=0;
adjcnt=0;
}
node* create_node(int pos,int x)
{
node *temp=new node;
temp->data=x;
temp->next=NULL;
if(adjlist[pos]==NULL)
{
adjlist[pos]=temp;
}
else
{
node *q=adjlist[pos];
while(q->next!=NULL)
{
q=q->next;
}
q->next=temp;
}
}
void getdata()
{
int i,j,var1,var2;
cout<<"Enter number of vertices : ";
cin>>vert;
cout<<"Let, the vertices be : ";
for(i=0;i<vert;i++)
{
cout<<i<<" ";
}
cout<<endl<<"Enter number of edges : ";
cin>>edge;
cout<<"Enter edges in the form of (u,v) :\n";
for(i=0;i<edge;i++)
{
cout<<"Enter edge "<<i+1<<" : ";
cin>>var1>>var2;
matrix[var1][var2]=1;
matrix[var2][var1]=1;
create_node(var1,var2);
create_node(var2,var1);
}
}
int disp_matrix()
{
cout<<"Matrix representation of this graph :\n";
for(int i=0;i<vert;i++)
{
for(int j=0;j<vert;j++)
{
cout<<matrix[i][j]<<" ";
matcnt++;
}
cout<<endl;
}
return matcnt;
}
int disp_adjlist()
{
cout<<"Adjacency List representation of this matrix :\n";
for(int i=0;i<vert;i++)
{
node *q=adjlist[i];
cout<<i<<"-->";
while(q!=NULL)
{
cout<<q->data;
if(q->next!=NULL)
cout<<"-->";
q=q->next;
adjcnt++;
}
cout<<endl;
}
return adjcnt;
}
void bfs_nonrec(int source)
{
queue q1;
int i,get;
node *temp;
for(i=0;i<MAX;i++)
{
visit[i]=false;
}
q1.insert(source);
visit[source]=true;
while(!q1.isempty())
{
get=q1.front();
q1.del();
temp=adjlist[get];
cout<<get<<endl;
while(temp!=NULL)
{
if(visit[temp->data]==false)
{
q1.insert(temp->data);
visit[temp->data]=true;
}
temp=temp->next;
}
}
}
void dfs_nonrec(int source)
{
stack s1;
node *temp;
int i,get;
for(i=0;i<MAX;i++)
{
visit[i]=false;
}
s1.push(source);
visit[source]=true;
while(!s1.isempty())
{
get=s1.top();
s1.pop();
cout<<get<<endl;
temp=adjlist[get];
while(temp!=NULL)
{
if(visit[temp->data]==false)
{
s1.push(temp->data);
visit[temp->data]=true;
}
temp=temp->next;
}
}
}
void dfs_util(int source)
{
node *temp=adjlist[source];
cout<<source<<endl;
visit[source]=true;
while(temp!=NULL)
{
if(visit[temp->data]==false)
dfs_util(temp->data);
temp=temp->next;
}
}
void dfs_rec(int source)
{
for(int i=0;i<MAX;i++)
{
visit[i]=false;
}
dfs_util(source);
}
void bfs_util(queue q2)
{
if(q2.isempty())
return;
int get=q2.front();
cout<<get<<endl;
node *temp=adjlist[get];
q2.del();
while(temp!=NULL)
{
if(visit[temp->data]==false)
{
visit[temp->data]=true;
q2.insert(temp->data);
}
temp=temp->next;
}
bfs_util(q2);
}
void bfs_rec()
{
queue q2;
for(int i=0;i<MAX;i++)
{
visit[i]=false;
}
for(int i=0;i<vert;i++)
{
if(visit[i]==false)
{
visit[i]=true;
q2.insert(i);
bfs_util(q2);
}
}
}
};
int main()
{
int count1,count2;
graph g1;
g1.getdata();
count1=g1.disp_matrix();
count2=g1.disp_adjlist();
cout<<count1<<endl<<count2<<endl;
cout<<"BFS non rec:\n";
g1.bfs_nonrec(0);
cout<<"DFS non rec:\n";
g1.dfs_nonrec(0);
cout<<"DFS rec :\n";
g1.dfs_rec(0);
cout<<"BFS rec :\n";
g1.bfs_rec();
return 0;
}