-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinalTransitiveClosureTest.c
98 lines (84 loc) · 2.97 KB
/
FinalTransitiveClosureTest.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "Graph.h"
#include "GraphTransitiveClosure.h"
void testGraphFromFile(const char* filename) {
printf("Starting test for: %s\n", filename);
FILE* file = fopen(filename, "r");
if (!file) {
printf("Error: Could not open file %s\n", filename);
return;
}
int type, weighted, numVertices, numEdges;
fscanf(file, "%d %d %d %d", &type, &weighted, &numVertices, &numEdges);
Graph* graph = GraphCreate(numVertices, type, weighted);
if (!graph) {
printf("Error: Failed to create graph for %s\n", filename);
fclose(file);
return;
}
for (int i = 0; i < numEdges; i++) {
int u, v;
fscanf(file, "%d %d", &u, &v);
GraphAddEdge(graph, u, v);
}
fclose(file);
printf("Graph loaded from: %s\n", filename);
GraphDisplayDOT(graph);
printf("\n");
printf("Checking graph invariants...\n");
GraphCheckInvariants(graph);
printf("Computing Transitive Closure for: %s\n", filename);
Graph* transitiveClosure = GraphComputeTransitiveClosure(graph);
if (transitiveClosure) {
GraphDisplayDOT(transitiveClosure);
GraphDestroy(&transitiveClosure);
} else {
printf("Failed to compute Transitive Closure for: %s\n", filename);
}
GraphDestroy(&graph);
printf("Test completed for: %s\n\n", filename);
}
int main(void) {
const char* filenames[] = {
"graph_tests/transitive_graph1.txt",
"graph_tests/transitive_graph2.txt",
"graph_tests/transitive_graph3.txt",
"graph_tests/transitive_graph4.txt",
"graph_tests/transitive_graph5.txt",
"graph_tests/transitive_graph6.txt",
"graph_tests/transitive_graph7.txt",
"graph_tests/transitive_graph8.txt",
"graph_tests/transitive_graph9.txt",
"graph_tests/transitive_graph10.txt",
"graph_tests/transitive_graph11.txt",
"graph_tests/transitive_graph12.txt",
"graph_tests/transitive_graph13.txt",
"graph_tests/transitive_graph14.txt",
"graph_tests/transitive_graph15.txt",
"graph_tests/transitive_graph16.txt",
"graph_tests/transitive_graph17.txt",
"graph_tests/transitive_graph18.txt",
"graph_tests/transitive_graph19.txt",
"graph_tests/transitive_graph20.txt"
};
int numFiles = sizeof(filenames) / sizeof(filenames[0]);
while (1) {
printf("\nSelect a graph to test (1-%d) or type 'exit' to quit: ", numFiles);
char input[10];
scanf("%s", input);
if (strcmp(input, "exit") == 0) {
printf("Exiting the program.\n");
break;
}
int choice = atoi(input);
if (choice < 1 || choice > numFiles) {
printf("Invalid choice. Please select a valid number between 1 and %d.\n", numFiles);
} else {
testGraphFromFile(filenames[choice - 1]);
}
}
return 0;
}