Skip to content

Instantly share code, notes, and snippets.

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