C Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph

This is a C Program to perform Topological Sorting. Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge pq, vertex p comes before q in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG.

Here is source code of the C Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int s[100], j, res[100]; /*GLOBAL VARIABLES */
  5.  
  6. void AdjacencyMatrix(int a[][100], int n) { //To generate adjacency matrix for given nodes
  7.  
  8.     int i, j;
  9.     for (i = 0; i < n; i++) {
  10.         for (j = 0; j <= n; j++) {
  11.             a[i][j] = 0;
  12.         }
  13.     }
  14.     for (i = 1; i < n; i++) {
  15.         for (j = 0; j < i; j++) {
  16.             a[i][j] = rand() % 2;
  17.             a[j][i] = 0;
  18.         }
  19.     }
  20. }
  21.  
  22. void dfs(int u, int n, int a[][100]) { /* DFS */
  23.  
  24.     int v;
  25.     s[u] = 1;
  26.     for (v = 0; v < n - 1; v++) {
  27.         if (a[u][v] == 1 && s[v] == 0) {
  28.             dfs(v, n, a);
  29.         }
  30.     }
  31.     j += 1;
  32.     res[j] = u;
  33. }
  34.  
  35. void topological_order(int n, int a[][100]) { /* TO FIND TOPOLOGICAL ORDER*/
  36.  
  37.     int i, u;
  38.     for (i = 0; i < n; i++) {
  39.         s[i] = 0;
  40.     }
  41.     j = 0;
  42.     for (u = 0; u < n; u++) {
  43.         if (s[u] == 0) {
  44.             dfs(u, n, a);
  45.         }
  46.     }
  47.     return;
  48. }
  49. int main() {
  50.     int a[100][100], n, i, j;
  51.  
  52.     printf("Enter number of vertices\n"); /* READ NUMBER OF VERTICES */
  53.     scanf("%d", &n);
  54.  
  55.     AdjacencyMatrix(a, n); /*GENERATE ADJACENCY MATRIX */
  56.  
  57.     printf("\t\tAdjacency Matrix of the graph\n"); /* PRINT ADJACENCY MATRIX */
  58.     for (i = 0; i < n; i++) {
  59.         for (j = 0; j < n; j++) {
  60.             printf("\t%d", a[i][j]);
  61.         }
  62.         printf("\n");
  63.     }
  64.     printf("\nTopological order:\n");
  65.  
  66.     topological_order(n, a);
  67.  
  68.     for (i = n; i >= 1; i--) {
  69.         printf("-->%d", res[i]);
  70.     }
  71.     return 0;
  72. }

Output:

$ gcc TopologicalSorting.c
$ ./a.out
 
Enter number of vertices: 6
 
Adjacency Matrix of the graph
	0	0	0	0	0	0
	1	0	0	0	0	0
	1	0	0	0	0	0
	0	1	0	0	0	0
	0	0	0	0	0	0
	1	1	1	1	1	0
 
Topological order:
-->5-->4-->3-->2-->1-->0

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.