forked from sv00111/DFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphTest.c
461 lines (394 loc) · 10.2 KB
/
GraphTest.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
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
//Shrey Valia
//svalia
//PA5 : GraphTest.c - Test module I wrote that isolates and tests Graph.c
#include<stdio.h>
#include<stdlib.h>
#include"List.h"
#include"Graph.h"
int main(int argc, char* argv[]){
int i, n=30;
List S = newList();
Graph G = newGraph(n);
Graph T=NULL, C=NULL;
for(i=1; i<=n; i++) append(S, i);
for (i = 1; i < 7; i++)
{
if(i%2)
{
addArc(G, i,i+1);
}
}
for (i = 1; i < 7; i++)
{
if(i%3 && i > 2)
{
addArc(G, i,i-2);
}
else
{
addArc(G, i,i);
}
}
for(i=7; i<n; i++){
if (i <= 20)
{
addArc(G, 6, i);
}
if( i%7)
addArc(G, i, i+1);
if( i<=23 )
addArc(G, i, i+7);
}
addArc(G, 9, 30);
addArc(G, 17, 13);
addEdge(G, 30, 28);
DFS(G, S);
printf("The order of the graph is %i\n", getOrder(G));
printf("The size of the graph is %i\n", getSize(G));
fprintf(stdout, "\n");
fprintf(stdout, "x: d f p\n");
for(i=1; i<=n; i++){
fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(G, i), getFinish(G, i), getParent(G, i));
}
printGraph(stdout, G);
for(i=1; i<=n; i++){
if(getParent(G, i) == NIL)
{
printf("the parent for the vertex %i is: NIL \n", i);
}
else
printf("the parent for the vertex %i is: %i \n", i, getParent(G, i));
}
C = copyGraph(G);
T = transpose(G);
//***** copy graph
DFS(C, S);
printf("\n");
printf("\n");
printf("copy graph:\n");
printf("The order of the copied graph is %i\n", getOrder(C));
printf("The size of the copied graph is %i\n", getSize(C));
fprintf(stdout, "\n");
fprintf(stdout, "x: d f p\n");
for(i=1; i<=n; i++){
fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(C, i), getFinish(C, i), getParent(C, i));
}
printGraph(stdout, C);
for(i=1; i<=n; i++){
if(getParent(C, i) == NIL)
{
printf("in the copy graph the parent for the vertex %i is: NIL \n", i);
}
else
printf("in the copy graph the parent for the vertex %i is: %i \n", i, getParent(C, i));
}
printf("\n");
printf("\n");
printf("Transpose graph:\n");
//***** transpose graph
DFS(T, S);
printf("The order of the transpose graph is %i\n", getOrder(T));
printf("The size of the transpose graph is %i\n", getSize(T));
fprintf(stdout, "\n");
fprintf(stdout, "x: d f p\n");
for(i=1; i<=n; i++){
fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(T, i), getFinish(T, i), getParent(T, i));
}
printGraph(stdout, T);
for(i=1; i<=n; i++){
if(getParent(T, i) == NIL)
{
printf("in the transpose graph the parent for the vertex %i is: NIL \n", i);
}
else
printf("in the transpose graph the parent for the vertex %i is: %i \n", i, getParent(T, i));
}
freeList(&S);
freeGraph(&G);
freeGraph(&T);
freeGraph(&C);
return 0;
}
/*
OUTPUT of the program:The order of the graph is 30
The size of the graph is 62
x: d f p
1: 1 4 0
2: 2 3 1
3: 5 8 0
4: 6 7 3
5: 9 60 0
6: 10 59 5
7: 11 20 6
8: 21 58 6
9: 22 51 8
10: 23 46 9
11: 24 41 10
12: 25 36 11
13: 26 31 12
14: 12 19 7
15: 52 57 8
16: 47 50 9
17: 42 45 10
18: 37 40 11
19: 32 35 12
20: 27 30 13
21: 13 18 14
22: 53 56 15
23: 48 49 16
24: 43 44 17
25: 38 39 18
26: 33 34 19
27: 28 29 20
28: 14 17 21
29: 54 55 22
30: 15 16 28
1: 1 2
2: 2
3: 3 4
4: 2
5: 3 6
6: 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
7: 14
8: 9 15
9: 10 16 30
10: 11 17
11: 12 18
12: 13 19
13: 14 20
14: 21
15: 16 22
16: 17 23
17: 13 18 24
18: 19 25
19: 20 26
20: 21 27
21: 28
22: 23 29
23: 24 30
24: 25
25: 26
26: 27
27: 28
28: 30
29: 30
30: 28
the parent for the vertex 1 is: NIL
the parent for the vertex 2 is: 1
the parent for the vertex 3 is: NIL
the parent for the vertex 4 is: 3
the parent for the vertex 5 is: NIL
the parent for the vertex 6 is: 5
the parent for the vertex 7 is: 6
the parent for the vertex 8 is: 6
the parent for the vertex 9 is: 8
the parent for the vertex 10 is: 9
the parent for the vertex 11 is: 10
the parent for the vertex 12 is: 11
the parent for the vertex 13 is: 12
the parent for the vertex 14 is: 7
the parent for the vertex 15 is: 8
the parent for the vertex 16 is: 9
the parent for the vertex 17 is: 10
the parent for the vertex 18 is: 11
the parent for the vertex 19 is: 12
the parent for the vertex 20 is: 13
the parent for the vertex 21 is: 14
the parent for the vertex 22 is: 15
the parent for the vertex 23 is: 16
the parent for the vertex 24 is: 17
the parent for the vertex 25 is: 18
the parent for the vertex 26 is: 19
the parent for the vertex 27 is: 20
the parent for the vertex 28 is: 21
the parent for the vertex 29 is: 22
the parent for the vertex 30 is: 28
copy graph:
The order of the copied graph is 30
The size of the copied graph is 62
x: d f p
1: 59 60 0
2: 4 5 4
3: 2 7 5
4: 3 6 3
5: 1 58 0
6: 8 57 5
7: 9 18 6
8: 19 56 6
9: 20 49 8
10: 21 44 9
11: 22 39 10
12: 23 34 11
13: 24 29 12
14: 10 17 7
15: 50 55 8
16: 45 48 9
17: 40 43 10
18: 35 38 11
19: 30 33 12
20: 25 28 13
21: 11 16 14
22: 51 54 15
23: 46 47 16
24: 41 42 17
25: 36 37 18
26: 31 32 19
27: 26 27 20
28: 12 15 21
29: 52 53 22
30: 13 14 28
1: 1 2
2: 2
3: 3 4
4: 2
5: 3 6
6: 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
7: 14
8: 9 15
9: 10 16 30
10: 11 17
11: 12 18
12: 13 19
13: 14 20
14: 21
15: 16 22
16: 17 23
17: 13 18 24
18: 19 25
19: 20 26
20: 21 27
21: 28
22: 23 29
23: 24 30
24: 25
25: 26
26: 27
27: 28
28: 30
29: 30
30: 28
in the copy graph the parent for the vertex 1 is: NIL
in the copy graph the parent for the vertex 2 is: 4
in the copy graph the parent for the vertex 3 is: 5
in the copy graph the parent for the vertex 4 is: 3
in the copy graph the parent for the vertex 5 is: NIL
in the copy graph the parent for the vertex 6 is: 5
in the copy graph the parent for the vertex 7 is: 6
in the copy graph the parent for the vertex 8 is: 6
in the copy graph the parent for the vertex 9 is: 8
in the copy graph the parent for the vertex 10 is: 9
in the copy graph the parent for the vertex 11 is: 10
in the copy graph the parent for the vertex 12 is: 11
in the copy graph the parent for the vertex 13 is: 12
in the copy graph the parent for the vertex 14 is: 7
in the copy graph the parent for the vertex 15 is: 8
in the copy graph the parent for the vertex 16 is: 9
in the copy graph the parent for the vertex 17 is: 10
in the copy graph the parent for the vertex 18 is: 11
in the copy graph the parent for the vertex 19 is: 12
in the copy graph the parent for the vertex 20 is: 13
in the copy graph the parent for the vertex 21 is: 14
in the copy graph the parent for the vertex 22 is: 15
in the copy graph the parent for the vertex 23 is: 16
in the copy graph the parent for the vertex 24 is: 17
in the copy graph the parent for the vertex 25 is: 18
in the copy graph the parent for the vertex 26 is: 19
in the copy graph the parent for the vertex 27 is: 20
in the copy graph the parent for the vertex 28 is: 21
in the copy graph the parent for the vertex 29 is: 22
in the copy graph the parent for the vertex 30 is: 28
Transpose graph:
The order of the transpose graph is 30
The size of the transpose graph is 62
x: d f p
1: 1 2 0
2: 59 60 0
3: 55 56 0
4: 57 58 0
5: 3 4 0
6: 5 6 0
7: 45 46 0
8: 7 8 0
9: 15 16 0
10: 21 22 0
11: 27 28 0
12: 33 34 0
13: 39 40 0
14: 47 48 0
15: 9 10 0
16: 17 18 0
17: 23 24 0
18: 29 30 0
19: 35 36 0
20: 41 42 0
21: 49 50 0
22: 11 12 0
23: 19 20 0
24: 25 26 0
25: 31 32 0
26: 37 38 0
27: 43 44 0
28: 51 54 0
29: 13 14 0
30: 52 53 28
1: 1
2: 1 2 4
3: 3 5
4: 3
5:
6: 5 6
7: 6
8: 6
9: 6 8
10: 6 9
11: 6 10
12: 6 11
13: 6 12 17
14: 6 7 13
15: 6 8
16: 6 9 15
17: 6 10 16
18: 6 11 17
19: 6 12 18
20: 6 13 19
21: 14 20
22: 15
23: 16 22
24: 17 23
25: 18 24
26: 19 25
27: 20 26
28: 21 27 30
29: 22
30: 9 23 28 29
in the transpose graph the parent for the vertex 1 is: NIL
in the transpose graph the parent for the vertex 2 is: NIL
in the transpose graph the parent for the vertex 3 is: NIL
in the transpose graph the parent for the vertex 4 is: NIL
in the transpose graph the parent for the vertex 5 is: NIL
in the transpose graph the parent for the vertex 6 is: NIL
in the transpose graph the parent for the vertex 7 is: NIL
in the transpose graph the parent for the vertex 8 is: NIL
in the transpose graph the parent for the vertex 9 is: NIL
in the transpose graph the parent for the vertex 10 is: NIL
in the transpose graph the parent for the vertex 11 is: NIL
in the transpose graph the parent for the vertex 12 is: NIL
in the transpose graph the parent for the vertex 13 is: NIL
in the transpose graph the parent for the vertex 14 is: NIL
in the transpose graph the parent for the vertex 15 is: NIL
in the transpose graph the parent for the vertex 16 is: NIL
in the transpose graph the parent for the vertex 17 is: NIL
in the transpose graph the parent for the vertex 18 is: NIL
in the transpose graph the parent for the vertex 19 is: NIL
in the transpose graph the parent for the vertex 20 is: NIL
in the transpose graph the parent for the vertex 21 is: NIL
in the transpose graph the parent for the vertex 22 is: NIL
in the transpose graph the parent for the vertex 23 is: NIL
in the transpose graph the parent for the vertex 24 is: NIL
in the transpose graph the parent for the vertex 25 is: NIL
in the transpose graph the parent for the vertex 26 is: NIL
in the transpose graph the parent for the vertex 27 is: NIL
in the transpose graph the parent for the vertex 28 is: NIL
in the transpose graph the parent for the vertex 29 is: NIL
in the transpose graph the parent for the vertex 30 is: 28
*/