Question: #include #define n 1 0 0 / / Assuming maximum number of vertices int visited [ n ] ; / / Array to store visited

#include
#define n 100// Assuming maximum number of vertices
int visited[n]; // Array to store visited vertices
int finished[n]; // Array to store finished vertices
int out[n]; // Array to store outgoing edges count
int stage[n]; // Array to store stage number
void go(int, int[][n], int);
void topological_sort(int GEIJ[][n], int GEJJ[][n], int h){
int i, j;
// Initializing variables
for (i =0; i < n; i++){
visited[i]=0;
finished[i]=0;
out[i]=0;
stage[i]=0;
}
// Compute out[] for each vertex
for (i =0; i < n; i++){
for (j =0; j < n; j++){
if (GEIJ[i][j]==1){
out[i]++;
}
}
}
// Main loop for topological sorting
for (i =0; i < n; i++){
if (out[i]==0 && visited[i]==0){
go(i, GEJJ, h);
}
}
// Recursive function to visit vertices
void go(int vertex, int GEJJ[][n], int h){
int j;
visited[vertex]=1;
for (j =0; j < h; j++){
if (GEJJ[vertex][j]==1){
out[vertex]++;
}
}
for (int k =0; k < n; k++){
if (GEIJ[vertex][k]==1){
int i = k;
if (visited[i]==1){
if (finished[i]==0){
printf("Cycle detected!
");
return;
}
} else {
stage[i]=(stage[i]> stage[vertex])? stage[i] : stage[vertex]+1;
finished[i]++;
go(i, GEJJ, h);
}
}
}
}
}
int main(){
// Define GEIJ and GEJJ matrices and h
int GEIJ[n][n]; // Adjacency matrix for directed edges from i to j
int GEJJ[n][n]; // Adjacency matrix for directed edges from j to i
int h; // Number of vertices
// Initialize GEIJ and GEJJ and h
// Call topological_sort with these matrices and h
topological_sort(GEIJ, GEJJ, h);
return 0;
}
why is it not working

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!