0% found this document useful (0 votes)
11 views2 pages

Program 5

The document provides a C/C++ program that implements topological ordering for a directed acyclic graph (DAG) using an adjacency matrix. It reads the number of vertices and the adjacency matrix from user input, calculates the indegrees of each vertex, and uses a stack to determine the topological order. The output displays the vertices in topologically sorted order.

Uploaded by

abhinehalove71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Program 5

The document provides a C/C++ program that implements topological ordering for a directed acyclic graph (DAG) using an adjacency matrix. It reads the number of vertices and the adjacency matrix from user input, calculates the indegrees of each vertex, and uses a stack to determine the topological order. The output displays the vertices in topologically sorted order.

Uploaded by

abhinehalove71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Program 4

Design and implement C/C++ Program to obtain the Topological ordering of vertices in a given
digraph.

#include<stdio.h>

void main()
{
int a[10][10],t[10],indeg[10],n,SUM=0;
int u,k=0,v;
int i,j,stack[10],top=-1;
printf("\n\n\t topological ordering \n\n");
printf("enter the directed acyclic graph\n\n");
printf("enter the no of vertex\t");
scanf("%d",&n);
printf("enter the adjacency matrix\n");

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}

for(i=0;i<n;i++)
indeg[i]=0;

for(j=0;j<n;j++)
{
SUM=0;
for(i=0;i<n;i++)
{
SUM+=a[i][j];
}
indeg[j]=SUM;
}

for(i=0;i<n;i++)
{
if(indeg[i]==0)
{
stack[++top]=i;
}
}

while(top!=-1)
{
u=stack[top--];
t[k++]=u;
for(v=0;v<n;v++)
{
if(a[u][v]==1)
{
indeg[v]--;
if(indeg[v]==0)
{
stack[++top]=v;
}
}
}
}

printf("the topological sorting list\n");


for(i=0;i<n;i++)
{
printf("%d\t",t[i]+1);
}
}

Sample Input and Output:


topological ordering

enter the directed acyclic graph

enter the no of vertex 5


enter the adjacency matrix
01010
00011
10011
00000
00000
the topological sorting list
3 1 2 5 4

You might also like