JustPaste.it

LAB 5 (TOPOLOGICAL ORDER)

#include<stdio.h>
void main()
{
  int i,j,a[10][10],n,indegree[10],u,v,t[10],s[10],top=-1,k=0,sum=0;
  printf("Enter the no of Vertices: ");
  scanf("%d",&n);
  printf("Enter the adjacency matrix:");
  for(i=0;i<n;i++)
  {
    for(j=0;j<n;j++)
    {
     scanf("%d",&a[i][j]);
    }
  }
   for(u=0;u<n;u++)
  {
    sum=0;
    for(v=0;v<n;v++)
    {
      sum = sum+a[v][u];
    }
    indegree[u]=sum;
  }
  for(u=0;u<n;u++)
  {
    if(indegree[u]==0)
    {
      top=top+1;
      s[top]=u;
    }
  }
  if(top!=-1)
  {
     while(top!=-1)
     {
       u=s[top];
       top=top-1;
       t[k]=u;
       k=k+1;
       for(v=0;v<n;v++)
       {
        if(a[u][v]==1)
        {
            indegree[v]--;
            if(indegree[v]==0)
            {
            top=top+1;
            s[top]=v;
            }
        }
       }
     }
     printf("The topological Sorting is\n");
     for(u=0;u<n;u++)
     {
       printf("%d\t",t[u]+1);
     }
  }
  else
  {
    printf("We cannot find Topological Sorting for the given graph\n");
  }
}