Blogger news

Followers

Monday, July 21, 2014
/*Obtain the Topological ordering of vertices in a given digraph*/


#include<stdio.h>
int ad[10][10];
void main()
{
            int i,j,n,f=1,in=1,k,v[50000],count=0,flag;
            printf("\n\nEnter the number of nodes\n\n");
            scanf("%d",&n);
            printf("\n\nEnter the graph in the form of adjacency matrix\n\n");
            for(i=1;i<=n;i++)
                        for(j=1;j<=n;j++)
                                    scanf("%d",&ad[i][j]);
            printf("\nThe adjacency matrix is:\n\n");
            for(i=1;i<=n;i++)
            {
                        for(j=1;j<=n;j++)
                                    printf("%d\t",ad[i][j]);
                        printf("\n");
            }
            while(f)
            {
                        count++;
                        for(j=1;j<=n;j++)
                        {
                                    flag=0;
                                    for(i=1;i<=n;i++)
                                    {
                                                if(ad[i][j]==1||v[i]==j)
                                                {
                                                            flag=1;
                                                            break;
                                                }
                                    }
                                    if(flag==0)
                                    {
                                                v[in++]=j;
                                                for(k=1;k<=n;k++)
                                                            ad[j][k]=0;
                                    }
                        }
                        if(count==n)
                                    f=0;
            }
            if(in<n)
                        printf("\nThere is no topological order\n\n");
            else
            {
                        printf("\nThere is a topological order and it is::\t");
                        for(i=1;i<=n;i++)
                                    printf("%d\t",v[i]);
            }
            printf("\n\n");
}
                                   
           


0 comments: