-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourses_graph.c
More file actions
136 lines (121 loc) · 3.38 KB
/
courses_graph.c
File metadata and controls
136 lines (121 loc) · 3.38 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define WHITE 0
#define GRAY 1
#define BLACK 2
void DFS_visit(int u, int N, int graph[][1001], int *curN, int res[], int color[], int deg[]) {
int v;
color[u] = BLACK;
for (v = 0; v < N ; v++) {
if (graph[u][v] == 1 && color[v] == WHITE) {
DFS_visit(v, N, graph, curN, res, color, deg);
}
}
res[(*curN)] = u;
(*curN)++;
}
int main(){
char fileName[31];
char line[1001];
char c;
char courses[101][1001];
char tmpcourses[1001];
int deg[101];
int N =0;
int i, j, k;
printf("This program will read, from a file, a list of courses and their prerequesites and will print the list in which to take courses.\n");
printf("Enter file name: ");
scanf("%s%c", fileName, &c);
//open the file
FILE *fp;
fp = fopen(fileName, "r");
while(fgets(courses[N], sizeof(courses[N]), fp) != NULL) N++;
fclose(fp);
//store only the courses as a string array
printf("Number of vertices in built graph: N = %d\n", N);
char className[101][31];
int classNameCnt = 0;
char * token;
int graph[101][1001];
for(i = 0; i < N ; i ++)
for(j = 0; j < N; j++)
graph[i][j] = 0;
for(i = 0; i < N; i++) deg[i] = 0;
puts("Vertex - coursename correspondence");
for(i=0; i<N; i++){
strcpy(tmpcourses, courses[i]);
token = strtok(tmpcourses, " \n");
if(strlen(token) == 0) continue;
for(j = 0; j < classNameCnt; j++) if(strcmp(token, className[j]) == 0) break;
if(j == classNameCnt) {
strcpy(className[classNameCnt], token);
classNameCnt++;
printf("%d - %s\n", classNameCnt, className[classNameCnt - 1]);
}
token = strtok(NULL, " \n");
}
for(i=0; i<N; i++){
strcpy(tmpcourses, courses[i]);
token = strtok(tmpcourses, " \n");
if(strlen(token) == 0) continue;
for(j = 0; j < classNameCnt; j++) if(strcmp(token, className[j]) == 0) break;
token = strtok(NULL, " \n");
while( token != NULL ) {
if(strlen(token) > 0){
for(k = 0; k < classNameCnt; k++) if(strcmp(token, className[k]) == 0) break;
graph[k][j] = 1;
deg[j]++;
}
token = strtok(NULL, " \n");
}
}
printf("Adjacency Matrix:\n");
printf(" |");
for(i = 0; i < N; i++) {
printf(" %d", i);
}
printf("\n");
for(i = 0; i < N + 1; i++) printf("----");
puts("");
for (i = 0; i < N; i++) {
printf(" %d|", i);
for (j = 0; j < N; j++) {
printf(" %d", graph[i][j]);
}
printf("\n");
}
int color[101];
for (i = 0; i < N; i++) {
color[i] = WHITE;
}
int curN = 0;
int res[101];
for (i = 0; i < N; i++) {
if (color[i] == WHITE) {
DFS_visit(i, N, graph, &curN, res, color, deg);
}
}
int flag = 0;
int pos[101];
for(i = N - 1; i >=0; i--) {
pos[res[i]] = N - 1 - i;
}
for(i = 0; i < N; i++) {
for(j = 0; j < N; j++) {
if(graph[i][j] == 1 && pos[i] > pos[j]) {
flag = 1;
}
}
}
if(flag) {
printf("There was at least one cycle. There is no possible ordering of the courses.\n");
}
else{
puts("Order in which to take courses:");
for(i = N - 1; i >= 0; i --) {
printf("%d. - %s (corresponds to graph vertex %d)\n", N - i, className[res[i]], res[i] );
}
}
return 0;
}