Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#include #define n 1 0 0 / / Assuming maximum number of vertices int visited [ n ] ; / / Array to store visited
#include
#define n Assuming maximum number of vertices
int visitedn; Array to store visited vertices
int finishedn; Array to store finished vertices
int outn; Array to store outgoing edges count
int stagen; Array to store stage number
void goint intn int;
void topologicalsortint GEIJn int GEJJn int h
int i j;
Initializing variables
for i ; i n; i
visitedi;
finishedi;
outi;
stagei;
Compute out for each vertex
for i ; i n; i
for j ; j n; j
if GEIJij
outi;
Main loop for topological sorting
for i ; i n; i
if outi && visitedi
goi GEJJ, h;
Recursive function to visit vertices
void goint vertex, int GEJJn int h
int j;
visitedvertex;
for j ; j h; j
if GEJJvertexj
outvertex;
for int k ; k n; k
if GEIJvertexk
int i k;
if visitedi
if finishedi
printfCycle detected!
;
return;
else
stageistagei stagevertex stagei : stagevertex;
finishedi;
goi GEJJ, h;
int main
Define GEIJ and GEJJ matrices and h
int GEIJnn; Adjacency matrix for directed edges from i to j
int GEJJnn; Adjacency matrix for directed edges from j to i
int h; Number of vertices
Initialize GEIJ and GEJJ and h
Call topologicalsort with these matrices and h
topologicalsortGEIJ GEJJ, h;
return ;
why is it not working
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