Skip to content

Instantly share code, notes, and snippets.

@anshumanatri
Created March 12, 2009 10:19
Show Gist options
  • Save anshumanatri/78010 to your computer and use it in GitHub Desktop.
Save anshumanatri/78010 to your computer and use it in GitHub Desktop.
#include<iostream.h>]
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int count,comp, e[10][10],n,v[10],order[10];
void dfs(int i)
{
v[i]=comp;
for(int j=0;j<n;j++)
if(e[i][j] && i!=j)
if(!v[j])
dfs(j);
order[i]=count--;
for(int j=0;j<n;j++)
if(e[i][j] && v[i]==v[j] && v[i]!=0 && v[j]!=0 && order[i]>order[j])
{
cout<<"\n The given graph is cyclic.";
getch();
exit(0);
}
}
void main()
{
int i,j;
clrscr();
cout<<"\nEnter the value of n (i.e.),no of vertices:\t";
cin>>n;count=n;
cout<<"\n\nEnter the adjacency matrix of digraph.\n";
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&e[i][j]);
for(i=0;i<n;i++)
if(v[i]==0)
{
comp+=1;;
dfs(i);
}
cout<<"\ntopological order:\t";
for(i=0;i<n;i++)
cout<<"\n vertex [ "<<(i+1)<<" ]\torder:\t"<<order[i];
getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment