Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#define MAX _ COURSES 1 0 0 #define MAX _ NAME _ LEN 5 0 int graph _ courses [ MAX _ COURSES ] [

#define MAX_COURSES 100
#define MAX_NAME_LEN 50
int graph_courses[MAX_COURSES][MAX_COURSES];
char courses[MAX_COURSES][MAX_NAME_LEN];
int num_courses =0;
int get_course_index(char* name){
for (int i =0; i < num_courses; i++){
if (strcmp(courses[i], name)==0){
return i;
}
}
return -1;
}
void add_edge_courses(char* src, char* dest){
int src_index = get_course_index(src);
int dest_index = get_course_index(dest);
if (src_index ==-1|| dest_index ==-1){
printf("Error: Invalid edge between %s and %s
", src, dest);
return;
}
graph_courses[src_index][dest_index]=1;
}
void read_courses_from_file(){
char* filename = "courses.txt";
FILE* file = fopen(filename,"r");
if (file == NULL){
printf("Error: Could not open file %s
", filename);
return;
}
// Initialize graph_courses to all zeros
memset(graph_courses, 0, sizeof(graph_courses));
char line[256];
while (fgets(line, sizeof(line), file)){
char src[MAX_NAME_LEN], dest1[MAX_NAME_LEN], dest2[MAX_NAME_LEN]="";
int count = sscanf(line,"%[^#]#%[^#]#%s", src, dest1, dest2);
if (count <2){
printf("Error: Invalid line in file: %s
", line);
continue;
}
// Add the course to the courses array if not already present
if (get_course_index(src)==-1){
strcpy(courses[num_courses++], src);
}
// Add the first prerequisite to the courses array if not already present
if (get_course_index(dest1)==-1){
strcpy(courses[num_courses++], dest1);
}
// If there is a second prerequisite, add it to the courses array if not already present
if (count ==3 && get_course_index(dest2)==-1){
strcpy(courses[num_courses++], dest2);
}
// Add edges only if the prerequisites are valid courses
if (get_course_index(src)!=-1 && get_course_index(dest1)!=-1){
add_edge_courses(dest1, src);
}
if (count ==3 && get_course_index(dest2)!=-1){
add_edge_courses(dest2, src);
}
}
fclose(file);
}
void topological_sort(){
int in_degree[MAX_COURSES]={0};
int queue[MAX_COURSES], front =0, rear =-1;
int top_order[MAX_COURSES];
int counter =0;
// Calculate in-degree (number of incoming edges) for each of the vertex present in the graph
for(int i =0; i < num_courses; i++)
for(int j =0; j < num_courses; j++)
in_degree[i]+= graph_courses[j][i];
// Enqueue all vertices with in-degree of 0
for(int i =0; i < num_courses; i++){
if(in_degree[i]==0){
queue[++rear]= i;
}
}
while (front <= rear){
int u = queue[front++];
top_order[counter++]= u;
// For each neighbor v of u, if in-degree[v] becomes 0, then enqueue it
for(int v =0; v < num_courses; v++){
if(graph_courses[u][v]){
in_degree[v]--;
if(in_degree[v]==0){
queue[++rear]= v;
}
}
}
}
// If count of visited nodes is not equal to the number of nodes in the graph then the topological sort is not possible for the given graph
if(counter != num_courses){
printf("There exists a cycle in the graph
");
return;
}
}
//print the sorted courses
void print_sorted_courses(){
for(int i =0; i < num_courses; i++){
printf("%s ", courses[i]);
}
printf("
");
}
fix the code the output should be :
COMP133, COMP233, COMP2310, COMP2421, ENCS2340, ENCS3130,
ENCS3340, ENCS4320, COMP333, ENCS2380, COMP433, ENCS3310
and the input file is :
COMP2310#COMP133
COMP2421#COMP133
COMP333#COMP2421
COMP433#COMP333
ENCS2340#COMP133
ENCS2380#ENCS2340
ENCS3130#COMP133
ENCS3310#ENCS2380
ENCS3340#COMP233 and COMP133
ENCS4320#COMP233 and COMP133

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Shouhong Wang, Hai Wang

1st Edition

1612330150, 978-1612330150

More Books

Students also viewed these Databases questions

Question

6. Does your speech have a clear and logical structure?

Answered: 1 week ago