C Program to Check if Graph is DAG

This is a C Program to check whether graph is DAG. A directed acyclic graph, is a directed graph which has no directed cycles. That is, it is formed by a collection of vertices and directed edges, each edge connecting one vertex to another, such that there is no way to start at some vertex V and follow a sequence of edges that eventually loops back to V again.

Here is source code of the C Program to Check Whether Graph is DAG. 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. #include<conio.h>
  4.  
  5. int c = 0;
  6.  
  7. struct adj_list {
  8.     int dest;
  9.     struct adj_list *next;
  10. }*np = NULL, *np1 = NULL, *p = NULL, *q = NULL;
  11.  
  12. struct Graph {
  13.     int v;
  14.     struct adj_list *ptr;
  15. } array[6];
  16.  
  17. void addReverseEdge(int src, int dest) {
  18.     np1 = malloc(sizeof(struct adj_list));
  19.     np1->dest = src;
  20.     np1->next = NULL;
  21.     if (array[dest].ptr == NULL) {
  22.         array[dest].ptr = np1;
  23.         q = array[dest].ptr;
  24.         q->next = NULL;
  25.     } else {
  26.         q = array[dest].ptr;
  27.         while (q->next != NULL) {
  28.             q = q->next;
  29.         }
  30.         q->next = np1;
  31.     }
  32. }
  33. void addEdge(int src, int dest) {
  34.     np = malloc(sizeof(struct adj_list));
  35.     np->dest = dest;
  36.     np->next = NULL;
  37.     if (array[src].ptr == NULL) {
  38.         array[src].ptr = np;
  39.         p = array[src].ptr;
  40.         p->next = NULL;
  41.     } else {
  42.         p = array[src].ptr;
  43.         while (p->next != NULL) {
  44.             p = p->next;
  45.         }
  46.         p->next = np;
  47.     }
  48.     //addReverseEdge(src, dest);
  49. }
  50. void print_graph(int n) {
  51.     int i;
  52.     for (i = 0; i < n; i++) {
  53.         printf("Adjacency List of %d: ", array[i].v);
  54.         while (array[i].ptr != NULL) {
  55.             printf("%d ", (array[i].ptr)->dest);
  56.             array[i].ptr = (array[i].ptr)->next;
  57.         }
  58.         printf("\n");
  59.     }
  60. }
  61.  
  62. int checkDAG(int n) {
  63.     int count = 0;
  64.     int size = n - 1, i, j;
  65.     for (i = 0; i < n; i++) {
  66.         //cout << "Adjacency List of " << array[i].v << ": ";
  67.         if (count == size) {
  68.             return 1;
  69.         }
  70.         if (array[i].ptr == NULL) {
  71.             count++;
  72.             for (j = 0; j < n; j++) {
  73.  
  74.                 while (array[j].ptr != NULL) {
  75.                     if ((array[j].ptr)->dest == (array[i].ptr)->dest) {
  76.                         (array[j].ptr)->dest = -1;
  77.                     }
  78.                     array[i].ptr = (array[i].ptr)->next;
  79.                 }
  80.             }
  81.  
  82.         }
  83.     }
  84.     return 0;
  85. }
  86. int main() {
  87.     int n = 6, i;
  88.     printf("Number of vertices: %d\n", n);
  89.  
  90.     for (i = 0; i < n; i++) {
  91.         array[i].v = i;
  92.         array[i].ptr = NULL;
  93.     }
  94.     addEdge(0, 1);
  95.     addEdge(1, 2);
  96.     addEdge(1, 3);
  97.     addEdge(3, 4);
  98.     addEdge(4, 5);
  99.     addEdge(5, 3);
  100.     addEdge(5, 2);
  101.     print_graph(n);
  102.     printf("The given graph is 'Directed Acyclic Graph' :");
  103.     if (checkDAG(n) == 1)
  104.         printf(" True");
  105.     else
  106.         printf(" False");
  107. }

Output:

$ gcc CheckDAG.c
$ ./a.out
 
Number of vertices: 6
Adjacency List of 0: 1 
Adjacency List of 1: 2 3 
Adjacency List of 2: 
Adjacency List of 3: 4 
Adjacency List of 4: 5 
Adjacency List of 5: 3 2 
The given graph is 'Directed Acyclic Graph' : True

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.