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

5

The document contains a C program that implements a topological sorting algorithm using an adjacency matrix representation of a directed graph. It calculates the indegree of each node, pushes nodes with zero indegree onto a stack, and processes them to generate a topological sequence. The program prompts the user for the number of nodes and the adjacency matrix, then displays the topological order of the nodes.

Uploaded by

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

5

The document contains a C program that implements a topological sorting algorithm using an adjacency matrix representation of a directed graph. It calculates the indegree of each node, pushes nodes with zero indegree onto a stack, and processes them to generate a topological sequence. The program prompts the user for the number of nodes and the adjacency matrix, then displays the topological order of the nodes.

Uploaded by

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

#include <stdio.

h>

void findIndegree(int a[10][10], int indegree[10], int n) {


int i, j, sum;
for (j = 1; j <= n; j++) {
sum = 0;
for (i = 1; i <= n; i++) {
sum += a[i][j];
}
indegree[j] = sum;
}
}

void topological(int n, int a[10][10]) {


int stack[20], top = -1;
int indegree[10], t[100], k = 1;
findIndegree(a, indegree, n);

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


if (indegree[i] == 0)
stack[++top] = i;
}

while (top != -1) {


int u = stack[top--];
t[k++] = u;

for (int v = 1; v <= n; v++) {


if (a[u][v] == 1) {
indegree[v]--;
if (indegree[v] == 0)
stack[++top] = v;
}
}
}

printf("\nTopological Sequence:\n");
for (int i = 1; i < k; i++)
printf("%d\t", t[i]);
printf("\n");
}

int main() {
int a[10][10], i, j, n;

printf("Enter the number of nodes: ");


scanf("%d", &n);

printf("\nEnter the adjacency matrix:\n");


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

printf("\nThe adjacency matrix is:\n");


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

return 0;
}

You might also like