-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphAdjM.cpp
More file actions
464 lines (457 loc) · 11.7 KB
/
Copy pathGraphAdjM.cpp
File metadata and controls
464 lines (457 loc) · 11.7 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include <iostream>
#include <vector>
using namespace std;
const int Size = 20;
struct Stack
{
int arr[Size];
int top = -1;
};
struct Queue
{
int capacity = Size;
int front = -1;
int rear = -1;
int *data = new int[capacity];
};
// Push Function
void push(Stack &stack, int value)
{
if (stack.top == Size - 1)
{
cout << "Stack Overflow! You cannot push more Elements " << value << endl;
return;
}
stack.arr[++stack.top] = value;
}
// Pop Functon
int pop(Stack &stack)
{
if (stack.top == -1)
{
cout << "Stack Underflow! You cannot pop more elements." << endl;
return -1;
}
return stack.arr[stack.top--];
}
// Display Stack
void displayStack(Stack &stack)
{
if (stack.top == -1)
{
cout << "Stack is empty." << endl;
return;
}
for (int i = 0; i <= stack.top; i++)
{
cout << stack.arr[i] << "->";
}
cout << endl;
}
// Queue
void EnQueue(Queue &q, int value)
{
if (q.rear == q.capacity - 1)
{
cout << "The Queue is Full" << endl;
return;
}
q.rear++;
q.data[q.rear] = value;
if (q.front == -1 || q.front > q.rear) // Queue empty
{
q.front = 0;
}
// cout<<q.data[q.rear]<<endl;
}
int DeQueue(Queue &q)
{
if (q.front == -1 || q.front > q.rear) // Queue empty
{
return -1;
}
int temp = q.data[q.front];
q.front++;
if (q.front == -1 || q.front > q.rear)
{
q.front = -1;
q.rear = -1;
}
return temp;
}
void displayQueue(Queue &q)
{
if (q.front == -1 || q.front > q.rear)
{
cout << "Queue is empty" << endl;
return;
}
cout << "Queue: ";
for (int i = q.front; i <= q.rear; i++)
{
cout << q.data[i] << " ";
}
cout << endl;
}
void dirAddEdges(vector<vector<int>> &arr)
{
int connection, edges;
int Size = arr.size();
// directed
for (int i = 0; i < Size; i++)
{
cout << "Enter no of edges connected to " << i << " :";
cin >> edges;
for (size_t j = 0; j < edges; j++)
{
cout << "Enter Edge: ";
cin >> connection;
arr[i][connection] = 1;
}
}
}
void dirDegree(vector<vector<int>> &arr)
{
int Size = arr.size();
// directed
int indegree;
int outdegree;
for (size_t i = 0; i < Size; i++)
{
indegree = 0;
outdegree = 0;
for (size_t j = 0; j < Size; j++)
{
if (arr[i][j] == 1)
{
++indegree;
}
else if (arr[j][i] == 1)
{
++outdegree;
}
}
cout << "In-Deg(" << i << ")" << " = " << indegree << endl;
cout << "Out-Deg(" << i << ")" << " = " << outdegree << endl;
}
}
void unDirAddEdges(vector<vector<int>> &arr)
{
int connection, edges;
int Size = arr.size();
// directed
for (int i = 0; i < Size; i++)
{
cout << "Enter no of edges connected to " << i << " :";
cin >> edges;
for (size_t j = 0; j < edges; j++)
{
cout << "Enter Edge: ";
cin >> connection;
arr[i][connection] = 1;
arr[connection][i] = 1;
}
}
}
void unDirDegree(vector<vector<int>> &arr)
{
int Size = arr.size();
int degree;
for (size_t i = 0; i < Size; i++)
{
degree = 0;
for (size_t j = 0; j < Size; j++)
{
if (arr[i][j] == 1)
{
++degree;
}
}
cout << "Deg(" << i << ")" << " = " << degree << endl;
}
}
void display(vector<vector<int>> &arr)
{
int Size = arr.size();
// display
for (size_t i = 0; i < Size; i++)
{
for (size_t j = 0; j < Size; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
}
void addVertex(vector<vector<int>> &arr)
{
int size = arr.size();
// appending a row
arr.push_back(vector<int>(size, 0));
// appending a col in each row
for (size_t i = 0; i < size + 1; i++)
{
arr[i].push_back(0);
}
}
void removeVertex(vector<vector<int>> &arr, int vertex)
{
int size = arr.size();
// removing the (vetex)th row
arr.erase(arr.begin() + vertex);
// removing the (vertex)th col
for (size_t i = 0; i < size - 1; i++)
{
arr[i].erase(arr[i].begin() + vertex);
}
}
// void addEdge(vector<vector<int>>& arr,int vertex,int edge){
// arr[vertex][edge] = 1;
// }
void path(vector<vector<int>> &arr, Stack &stack, int from, int to)
{
int size = arr.size();
vector<int> visited(size, 0); // boolean array to check which node has been visited
int curr = from;
push(stack, from); // push the initial node in stack
visited[from] = 1; // mark it as visited
bool flag = false;
for (int i = 0; i < size; i++)
{
// check for the 1st connecting edge
if (arr[curr][i] == 1)
{
// check whether it has been alrady visited
if (visited[i] == 0)
{
push(stack, i);
visited[i] = 1;
// moving to the ith row to find the next '1' in the arr
curr = i;
// if we reached the destination edge then break the loop
if (curr == to)
{
flag = true;
break;
}
else // reset to 0 and start looking for '1' in that row
{
i = -1;
}
}
}
// if we reach the last vertex in that row and '1' is not found then backtrack to prev vertex in stack
else if (i == size - 1 && flag == false && stack.top != -1)
{
curr = stack.arr[stack.top - 1]; // move to prev node
pop(stack);
// if stack gets empty, means path not found
if (stack.top == -1)
{
break;
}
else
{
i = curr + 1; // after backtracking move to the next index
}
}
}
if (flag == true)
{
displayStack(stack);
}
else
{
cout << "Path does not exists" << endl;
}
}
bool isCyclic(vector<vector<int>> arr, Stack &stack, int vertex)
{
int size = arr.size();
int curr = vertex; // starting from the vertex in adj matrix
push(stack, curr); // push the initial vertex in stack
bool flag = false;
for (int i = 0; i < size; i++)
{
// check for the 1st connecting edge
if (arr[curr][i] == 1)
{
// if the stack contains only the source vertex, directly push the connecting edge
if (stack.top == 0)
{
push(stack, i);
curr = i;
i = -1;
}
// check whether it has been alrady visited
else if (stack.arr[stack.top - 1] != i)
{
push(stack, i);
// moving to the ith row to find the next '1' in the arr
curr = i;
// if we reached the destination edge then break the loop
if (curr == vertex)
{
flag = true;
break;
}
else // reset to 0 and start looking for '1' in that row
{
i = -1;
}
}
}
// if we reach the last vertex in that row and '1' is not found then backtrack to prev vertex in stack
else if (i == size - 1 && flag == false && stack.top != -1)
{
// save the curr position so that when we backtrack we start looking forward from that position
int index = curr;
curr = stack.arr[stack.top - 1]; // move to prev vertex
pop(stack);
// if stack gets empty, means path not found
if (stack.top == -1)
{
break;
}
else
{
i = index + 1; // after backtracking move to the next index
}
}
}
//displayStack(stack);
return flag;
}
void BFS(vector<vector<int>> arr, Queue &q, int start)
{
int size = arr.size();
vector<int> visited(size, 0); // boolean array to check which node has been visited
EnQueue(q, start); // push starting vertex in the queue
visited[start] = 1; // mark it as visited
while (q.front <= q.rear)
{
// deque the vertex and start adding it's connection vertices in the queue
int curr = DeQueue(q);
// if Queue gets empty
if (curr == -1)
{
break;
}
cout << curr << "->"; // output vertex
for (int i = 0; i < size; i++)
{
if (arr[curr][i] == 1)
{
if (visited[i] == 0)
{
EnQueue(q, i); // store the curr vertex's connections
visited[i] = 1; // mark it as visited
}
}
}
}
}
void DFS(vector<vector<int>> arr, Stack &stack, int start)
{
int size = arr.size();
vector<int> visited(size, 0); // boolean array to check which node has been visited
push(stack, start); // push starting vertex in the stack
visited[start] = 1; // mark it as visited
while (stack.top != -1)
{
// pop the vertex and start adding it's connection vertex in stack
int curr = stack.arr[stack.top];
cout << curr << "->"; // output vertex
bool moved = false;
for (int i = 0; i < size; i++)
{
if (arr[curr][i] == 1)
{
if (visited[i] == 0)
{
push(stack, i); // store the curr vertex's connection
visited[i] = 1; // mark it as visited
moved = true;
break;
}
}
}
if (moved == false)
{
pop(stack);
}
}
// displayStack(stack);
}
int main()
{
int option;
cout << "1->Directed" << endl;
cout << "2->Un-Directed" << endl;
cin >> option;
int n;
cout << "Enter number of Vertices: ";
cin >> n;
// Initialize 2d-matrix vector of vectors with 0
vector<vector<int>> arr(n, vector<int>(n, 0));
int vertex;
Stack stack;
bool flag = false; // if cycle exists or not
Queue q;
// Directed
if (option == 1)
{
dirAddEdges(arr);
dirDegree(arr);
display(arr);
//addVertex(arr);
//display(arr);
path(arr, stack, 0, 6);
for (size_t i = 0; i < arr.size(); i++)
{
flag = isCyclic(arr, stack, i);
if (flag == true)
{
cout << "Graph is Cyclic";
displayStack(stack);
cout<<endl;
break;
}
}
if (!flag)
{
cout << "Graph is A-Cyclic" << endl;
}
//BFS(arr, q, 0);
//cout<<endl;
DFS(arr, stack, 0);
}
// Un-directed
else
{
unDirAddEdges(arr);
unDirDegree(arr);
display(arr);
//addVertex(arr);
//display(arr);
path(arr, stack, 3, 6);
for (size_t i = 0; i < arr.size(); i++)
{
flag = isCyclic(arr, stack, i);
if (flag == true)
{
cout << "Graph is Cyclic: ";
displayStack(stack);
cout<<endl;
break;
}
}
if (!flag)
{
cout << "Graph is A-Cyclic" << endl;
}
//BFS(arr, q, 0);
DFS(arr, stack, 0);
}
return 0;
}