-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA5.cpp
581 lines (500 loc) · 15.4 KB
/
A5.cpp
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/**
* @brief: A5.cpp.
*
* @paragraph: Code Description - This code is used to demonstrate DFS and BFS topological sort with cycle detection.
*
* @paragraph: Problem Statement - For the two given graphs (A5_images1.png and A5_images2.png): a.) Write a program to do DFS topological sort. Your program must be able to catch the loop. Run the program on the attached graphs. b.) Write a program to do BFS topological sort. Your program must be able to catch the loop. Run the program on the attached graphs. Submit screen shots of execution results. Submit the code.
*
* @author: Shreyans Patel (SSP210009)
*/
#include<iostream>
#include<queue>
//Necessary Defines
#define GRAPH_1 1
#define GRAPH_2 2
#define NUM_VERTICES_GRAPH_1 8
#define NUM_VERTICES_GRAPH_2 14
//Structure Defines
struct graphStruct;
struct adjacentVertex;
//Global Variables
graphStruct *Graph1[NUM_VERTICES_GRAPH_1];
graphStruct *Graph2[NUM_VERTICES_GRAPH_2];
bool visitedGraph1[NUM_VERTICES_GRAPH_1] = {false};
bool visitedGraph2[NUM_VERTICES_GRAPH_2] = {false};
bool doneGraph1[NUM_VERTICES_GRAPH_1] = {false};
bool doneGraph2[NUM_VERTICES_GRAPH_2] = {false};
int vertexMappingG1[NUM_VERTICES_GRAPH_1];
char vertexMappingG2[NUM_VERTICES_GRAPH_2];
int inDegreeListGraph1[NUM_VERTICES_GRAPH_1] = {0};
int inDegreeListGraph2[NUM_VERTICES_GRAPH_2] = {0};
std::queue<int> topoQGraph1;
std::queue<int> topoQGraph2;
//Structure of a graph for adjacency list representation
struct graphStruct
{
int vertex;
adjacentVertex *adjacent; //Vertex adjacent to current vertex
graphStruct()
{
vertex = -1;
adjacent = NULL;
}
};
//Structure of an adjacent vertex for adjacency list
struct adjacentVertex
{
graphStruct *vertexAddr; //Vertex represented by the adjacent node in a graph
adjacentVertex *next;
adjacentVertex()
{
vertexAddr = NULL;
next = NULL;
}
};
/**
* @brief: insertEdgeInGraph1.
* @details: Used to add an edge in Graph1 (Number Format (1 is index 0)).
* @param: iVertex - Source Vertex.
* @param: iAdjacentVertex - Destination Vertex.
* @return none.
*/
void insertEdgeInGraph1(int iVertex, int iAdjacentVertex)
{
int tInsertAtVertex = iVertex - 1;
int tVertexToInsert = iAdjacentVertex - 1;
adjacentVertex *tpAdjacent = Graph1[tInsertAtVertex]->adjacent;
if(NULL == tpAdjacent)
{
tpAdjacent = new adjacentVertex;
if(NULL == tpAdjacent)
{
return;
}
tpAdjacent->vertexAddr = Graph1[tVertexToInsert];
tpAdjacent->next = NULL;
Graph1[tInsertAtVertex]->adjacent = tpAdjacent;
return;
}
while(tpAdjacent->next != NULL)
{
tpAdjacent = tpAdjacent->next;
}
tpAdjacent->next = new adjacentVertex;
if(NULL == tpAdjacent->next)
{
return;
}
tpAdjacent->next->vertexAddr = Graph1[tVertexToInsert];
tpAdjacent->next->next = NULL;
}
/**
* @brief: insertEdgeInGraph2.
* @details: Used to add an edge in Graph2 (Alphabet Format (m is index 0)).
* @param: iVertex - Source Vertex.
* @param: iAdjacentVertex - Destination Vertex.
* @return none.
*/
void insertEdgeInGraph2(char iVertex, char iAdjacentVertex)
{
int tInsertAtVertex = (int)iVertex - 109;
int tVertexToInsert = (int)iAdjacentVertex - 109;
adjacentVertex *tpAdjacent = Graph2[tInsertAtVertex]->adjacent;
if(NULL == tpAdjacent)
{
tpAdjacent = new adjacentVertex;
if(NULL == tpAdjacent)
{
return;
}
tpAdjacent->vertexAddr = Graph2[tVertexToInsert];
tpAdjacent->next = NULL;
Graph2[tInsertAtVertex]->adjacent = tpAdjacent;
return;
}
while(tpAdjacent->next != NULL)
{
tpAdjacent = tpAdjacent->next;
}
tpAdjacent->next = new adjacentVertex;
if(NULL == tpAdjacent->next)
{
return;
}
tpAdjacent->next->vertexAddr = Graph2[tVertexToInsert];
tpAdjacent->next->next = NULL;
}
/**
* @brief: createGraph1.
* @details: Used to create Graph1 in adjacency list representation and its in-degree list.
* @return none.
*/
void createGraph1(void)
{
int tIterator = 0;
for(tIterator = 0; tIterator < NUM_VERTICES_GRAPH_1; tIterator++)
{
graphStruct *tpGraph1 = new graphStruct;
if(NULL == tpGraph1)
{
return;
}
tpGraph1->vertex = tIterator;
Graph1[tIterator] = tpGraph1;
tpGraph1 = NULL;
vertexMappingG1[tIterator] = tIterator + 1;
}
insertEdgeInGraph1(1,2);
insertEdgeInGraph1(1,5);
insertEdgeInGraph1(1,6);
insertEdgeInGraph1(2,3);
insertEdgeInGraph1(2,5);
insertEdgeInGraph1(2,7);
insertEdgeInGraph1(3,4);
insertEdgeInGraph1(4,5);
insertEdgeInGraph1(5,7);
insertEdgeInGraph1(5,8);
insertEdgeInGraph1(6,5);
insertEdgeInGraph1(6,8);
insertEdgeInGraph1(7,4);
insertEdgeInGraph1(7,8);
for(tIterator = 0; tIterator < NUM_VERTICES_GRAPH_1; tIterator++)
{
adjacentVertex *tpAdjacent = Graph1[tIterator]->adjacent;
while(tpAdjacent != NULL)
{
inDegreeListGraph1[tpAdjacent->vertexAddr->vertex] += 1;
tpAdjacent = tpAdjacent->next;
}
}
}
/**
* @brief: createGraph2.
* @details: Used to create Graph2 in adjacency list representation and its in-degree list.
* @return none.
*/
void createGraph2(void)
{
int tIterator = 0;
for(tIterator = 0; tIterator < NUM_VERTICES_GRAPH_2; tIterator++)
{
graphStruct *tpGraph2 = new graphStruct;
if(NULL == tpGraph2)
{
return;
}
tpGraph2->vertex = tIterator;
Graph2[tIterator] = tpGraph2;
tpGraph2 = NULL;
vertexMappingG2[tIterator] = tIterator + 109;
}
insertEdgeInGraph2('m','q');
insertEdgeInGraph2('m','r');
insertEdgeInGraph2('m','x');
insertEdgeInGraph2('n','o');
insertEdgeInGraph2('n','q');
insertEdgeInGraph2('n','u');
insertEdgeInGraph2('o','r');
insertEdgeInGraph2('o','s');
insertEdgeInGraph2('o','v');
insertEdgeInGraph2('p','o');
insertEdgeInGraph2('p','s');
insertEdgeInGraph2('p','z');
insertEdgeInGraph2('q','t');
insertEdgeInGraph2('r','u');
insertEdgeInGraph2('r','y');
insertEdgeInGraph2('s','r');
insertEdgeInGraph2('u','t');
insertEdgeInGraph2('v','w');
insertEdgeInGraph2('v','x');
insertEdgeInGraph2('w','z');
insertEdgeInGraph2('y','v');
for(tIterator = 0; tIterator < NUM_VERTICES_GRAPH_2; tIterator++)
{
adjacentVertex *tAdjacent = Graph2[tIterator]->adjacent;
while(tAdjacent != NULL)
{
inDegreeListGraph2[tAdjacent->vertexAddr->vertex] += 1;
tAdjacent = tAdjacent->next;
}
}
}
/**
* @brief: createGraph.
* @details: Used to create both the given graphs.
* @return none.
*/
void createGraph(void)
{
createGraph1();
createGraph2();
}
/**
* @brief: printGraph.
* @details: Used to the graph in adjacency list representation.
* @param: iGraph - Graph to be printed.
* @param: iSize - Number of vertices in the graph.
* @return none.
*/
void printGraph(graphStruct *iGraph[], int iSize)
{
graphStruct **tppGraph = iGraph;
int tIterator = 0;
for(tIterator = 0; tIterator < iSize; tIterator++)
{
switch(iSize)
{
case NUM_VERTICES_GRAPH_1:
{
std::cout<<vertexMappingG1[tppGraph[tIterator]->vertex];
break;
}
case NUM_VERTICES_GRAPH_2:
{
std::cout<<vertexMappingG2[tppGraph[tIterator]->vertex];
break;
}
default:
{
return;
}
}
adjacentVertex *tpAdjacent = tppGraph[tIterator]->adjacent;
while(tpAdjacent != NULL)
{
switch(iSize)
{
case NUM_VERTICES_GRAPH_1:
{
std::cout<<" -> "<<vertexMappingG1[tpAdjacent->vertexAddr->vertex];
break;
}
case NUM_VERTICES_GRAPH_2:
{
std::cout<<" -> "<<vertexMappingG2[tpAdjacent->vertexAddr->vertex];
break;
}
default:
{
return;
}
}
tpAdjacent = tpAdjacent->next;
}
std::cout<<" -> null";
std::cout<<std::endl;
}
}
/**
* @brief: topoSort.
* @details: Used to topologically sort the Graph using DFS.
* @param: iVertex - Vertex to start from.
* @param: iVisitedArr - List of visited vertexes of the graph.
* @param: iDoneArr - List of done vertexes of the graph.
* @param: iGraph - Graph under topological sort.
* @return bool - True(No Cycle) or False(Cycle Detected).
*/
bool topoSort(int iVertex, bool iVisitedArr[], bool iDoneArr[], graphStruct *iGraph[], int iSize)
{
if(true == iDoneArr[iVertex])
{
return true;
}
iVisitedArr[iVertex] = true;
adjacentVertex *tpAdjacent = iGraph[iVertex]->adjacent;
while(tpAdjacent != NULL)
{
if(false == iDoneArr[tpAdjacent->vertexAddr->vertex])
{
if(true == iVisitedArr[tpAdjacent->vertexAddr->vertex])
{
return false;
}
if(false == topoSort(tpAdjacent->vertexAddr->vertex, iVisitedArr, iDoneArr, iGraph, iSize))
{
return false;
}
}
tpAdjacent = tpAdjacent->next;
}
iDoneArr[iVertex] = true;
//We can also push this vertex to queue instead. We are printing it here so we can see where we encountered a loop.
switch(iSize)
{
case NUM_VERTICES_GRAPH_1:
{
std::cout<<vertexMappingG1[iVertex]<<" ";
break;
}
case NUM_VERTICES_GRAPH_2:
{
std::cout<<vertexMappingG2[iVertex]<<" ";
break;
}
default:
{
break;
}
}
return true;
}
/**
* @brief: dfsGraph.
* @details: Used to perform DFS on the given graph.
* @param: iSize - Number of vertices in the graph.
* @param: iVisitedArr - List of visited vertexes of the graph.
* @param: iDoneArr - List of done vertexes of the graph.
* @param: iGraph - Graph tp perform DFS on.
* @return none.
*/
void dfsGraph(int iSize, bool iVisitedArr[], bool iDoneArr[], graphStruct *iGraph[])
{
int tIterator = 0;
for(tIterator = 0; tIterator < iSize; tIterator++)
{
if(false == topoSort(tIterator, iVisitedArr, iDoneArr, iGraph, iSize))
{
std::cout<<"Cycle Detected. Exiting....."<<std::endl;
return;
}
}
}
/**
* @brief: bfsGraph.
* @details: Used to perform BFS on the given graph.
* @param: iGraph - Graph tp perform BFS on.
* @param: iNumVertex - Number of vertices in the graph.
* @param: iInDegreeList - List of in-degrees of the graph vertices.
* @param: iOutputQueue - Queue for topological output after BFS.
* @return none.
*/
bool bfsGraph(graphStruct *iGraph[], int iNumVertex, int *iInDegreeList, std::queue<int> &iOutputQueue)
{
std::queue<int> tVertexQueue;
int tVisitedCount = 0;
for(int i = 0; i < iNumVertex; i++)
{
if(0 == iInDegreeList[i])
{
tVertexQueue.push(i);
}
}
while(false == tVertexQueue.empty())
{
int tVertex = tVertexQueue.front();
tVertexQueue.pop();
iOutputQueue.push(tVertex);
adjacentVertex *tAdjacent = iGraph[tVertex]->adjacent;
while(tAdjacent != NULL)
{
iInDegreeList[tAdjacent->vertexAddr->vertex] -= 1;
if(0 == iInDegreeList[tAdjacent->vertexAddr->vertex])
{
tVertexQueue.push(tAdjacent->vertexAddr->vertex);
}
tAdjacent = tAdjacent->next;
}
tVisitedCount += 1;
}
if(tVisitedCount != iNumVertex)
{
return false;
}
return true;
}
/**
* @brief: printQueue.
* @details: Used to print the vertexes in a given queue.
* @param: iQueue - Queue to print.
* @param: iGraphNum - Graph to which the queue belongs.
* @return none.
*/
void printQueue(std::queue<int> iQueue, int iGraphNum)
{
switch(iGraphNum)
{
case GRAPH_1:
{
while(false == iQueue.empty())
{
std::cout<<vertexMappingG1[iQueue.front()]<<" ";
iQueue.pop();
}
break;
}
case GRAPH_2:
{
while(false == iQueue.empty())
{
std::cout<<vertexMappingG2[iQueue.front()]<<" ";
iQueue.pop();
}
break;
}
default:
{
return;
}
}
}
/**
* @brief: main.
* @details: Main Function.
* @return int.
*/
int main(void)
{
//Create graphs and their in-degree lists.
createGraph();
//Print adjacency lists of both the graphs.
std::cout<<"Adjacency List of Graph 1:"<<std::endl;
printGraph(Graph1,NUM_VERTICES_GRAPH_1);
std::cout<<std::endl;
std::cout<<"Adjacency List of Graph 2:"<<std::endl;
printGraph(Graph2,NUM_VERTICES_GRAPH_2);
std::cout<<std::endl;
//Perform DFS topological sort and show output for both of the graphs.
std::cout<<"DFS Topological Sorting of Graph 1: ";
dfsGraph(NUM_VERTICES_GRAPH_1, visitedGraph1, doneGraph1, Graph1);
std::cout<<std::endl;
std::cout<<"DFS Topological Sorting of Graph 2: ";
dfsGraph(NUM_VERTICES_GRAPH_2, visitedGraph2, doneGraph2, Graph2);
std::cout<<std::endl<<std::endl;
//Print in-degree lists of both the graphs.
std::cout<<"In-Degree List of Graph 1:"<<std::endl;
for(int i = 0; i < NUM_VERTICES_GRAPH_1; i++)
{
std::cout<<vertexMappingG1[i]<<" -> "<<inDegreeListGraph1[i]<<std::endl;
}
std::cout<<std::endl;
std::cout<<"In-Degree List of Graph 2:"<<std::endl;
for(int i = 0; i < NUM_VERTICES_GRAPH_2; i++)
{
std::cout<<vertexMappingG2[i]<<" -> "<<inDegreeListGraph2[i]<<std::endl;
}
std::cout<<std::endl;
//Perform BFS topological sort and show output for both of the graphs.
std::cout<<"BFS Topological Sorting of Graph 1: ";
if(false == bfsGraph(Graph1, NUM_VERTICES_GRAPH_1, inDegreeListGraph1, topoQGraph1))
{
printQueue(topoQGraph1,GRAPH_1);
std::cout<<"Cycle Detected. Exiting.....";
}
else
{
printQueue(topoQGraph1,GRAPH_1);
}
std::cout<<std::endl<<std::endl;
std::cout<<"BFS Topological Sorting of Graph 2: ";
if(false == bfsGraph(Graph2, NUM_VERTICES_GRAPH_2, inDegreeListGraph2, topoQGraph2))
{
printQueue(topoQGraph2,GRAPH_2);
std::cout<<"Cycle Detected. Exiting.....";
}
else
{
printQueue(topoQGraph2,GRAPH_2);
}
std::cout<<std::endl;
return 0;
}