Answered step by step
Verified Expert Solution
Link Copied!

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 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

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

Spatial Databases A Tour

Authors: Shashi Shekhar, Sanjay Chawla

1st Edition

0130174807, 978-0130174802

More Books

Students also viewed these Databases questions

Question

=+h. Vacation pay expense for December, $4,800.

Answered: 1 week ago

Question

Persuasive Speaking Organizing Patterns in Persuasive Speaking?

Answered: 1 week ago