0% found this document useful (0 votes)
19 views4 pages

DAA Exp-5

The document describes an algorithm to obtain the topological ordering of vertices in a directed graph. It provides code to implement the algorithm in C/C++. The code first initializes arrays and gets graph data as input. It then sorts the vertices based on in-degrees and outputs the topological ordering if possible.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

DAA Exp-5

The document describes an algorithm to obtain the topological ordering of vertices in a directed graph. It provides code to implement the algorithm in C/C++. The code first initializes arrays and gets graph data as input. It then sorts the vertices based on in-degrees and outputs the topological ordering if possible.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EXPERIMENT NO.

Design and Implement C/C++ Program to obtain Topological Ordering of vertices in a given digraph.

Program:

#include<stdio.h>

int temp[10],k=0;

void sort(int a[10][10],int id[],int n)

int i,j;

for(i=1; i<=n; i++)

if(id[i]==0)

id[i]=-1;

temp[++k]=i;

for(j=1; j<=n; j++)

if(a[i][j]==1 && id[j]!=-1)

id[j]--;

i=0;

void main()

int a[10][10],id[10],n,i,j;

printf("\nEnter the n value:");


scanf("%d",&n);

for(i=1; i<=n; i++)

id[i]=0;

printf("\nEnter the graph data:\n");

for(i=1; i<=n; i++)

for(j=1; j<=n; j++)

scanf("%d",&a[i][j]);

if(a[i][j]==1)

id[j]++;

sort(a,id,n);

if(k!=n)

printf("\nTopological ordering not possible");

else

printf("\nTopological ordering is:");

for(i=1; i<=k; i++)

printf("%d ",temp[i]);

OUTPUT
Additional Program: Insertion Sort

Program:

#include <stdio.h>

void insertionSort(int arr[], int n)

int i, key, j;

for (i = 1; i < n; i++) {

key = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;

int main()

int i;

int arr[] = {6,2,1,3,5};

int n = 5;

insertionSort(arr, n);

for (i = 0; i < n; i++)

printf("%d\n", arr[i]);
return 0;

You might also like