Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#define MAX _ COURSES 1 0 0 #define MAX _ NAME _ LEN 5 0 int graph _ courses [ MAX _ COURSES ] [
#define MAXCOURSES
#define MAXNAMELEN
int graphcoursesMAXCOURSESMAXCOURSES;
char coursesMAXCOURSESMAXNAMELEN;
int numcourses ;
int getcourseindexchar name
for int i ; i numcourses; i
if strcmpcoursesi name
return i;
return ;
void addedgecourseschar src char dest
int srcindex getcourseindexsrc;
int destindex getcourseindexdest;
if srcindex destindex
printfError: Invalid edge between s and s
src dest;
return;
graphcoursessrcindexdestindex;
void readcoursesfromfile
char filename "courses.txt;
FILE file fopenfilenamer;
if file NULL
printfError: Could not open file s
filename;
return;
Initialize graphcourses to all zeros
memsetgraphcourses, sizeofgraphcourses;
char line;
while fgetsline sizeofline file
char srcMAXNAMELEN destMAXNAMELEN destMAXNAMELEN;
int count sscanfline####s src dest dest;
if count
printfError: Invalid line in file: s
line;
continue;
Add the course to the courses array if not already present
if getcourseindexsrc
strcpycoursesnumcourses src;
Add the first prerequisite to the courses array if not already present
if getcourseindexdest
strcpycoursesnumcourses dest;
If there is a second prerequisite, add it to the courses array if not already present
if count && getcourseindexdest
strcpycoursesnumcourses dest;
Add edges only if the prerequisites are valid courses
if getcourseindexsrc && getcourseindexdest
addedgecoursesdest src;
if count && getcourseindexdest
addedgecoursesdest src;
fclosefile;
void topologicalsort
int indegreeMAXCOURSES;
int queueMAXCOURSES front rear ;
int toporderMAXCOURSES;
int counter ;
Calculate indegree number of incoming edges for each of the vertex present in the graph
forint i ; i numcourses; i
forint j ; j numcourses; j
indegreei graphcoursesji;
Enqueue all vertices with indegree of
forint i ; i numcourses; i
ifindegreei
queuerear i;
while front rear
int u queuefront;
topordercounter u;
For each neighbor v of u if indegreev becomes then enqueue it
forint v ; v numcourses; v
ifgraphcoursesuv
indegreev;
ifindegreev
queuerear 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
ifcounter numcourses
printfThere exists a cycle in the graph
;
return;
print the sorted courses
void printsortedcourses
forint i ; i numcourses; i
printfs coursesi;
printf
;
fix the code the output should be :
COMP COMP COMP COMP ENCS ENCS
ENCS ENCS COMP ENCS COMP ENCS
and the input file is :
COMP#COMP
COMP#COMP
COMP#COMP
COMP#COMP
ENCS#COMP
ENCS#ENCS
ENCS#COMP
ENCS#ENCS
ENCS#COMP and COMP
ENCS#COMP and COMP
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started