C Program to Find a Good Feedback Edge Set in a Graph

This is a C Program to find feednack arc set. This is the set which contains edges which when removed from the graph, graph becomes directed acyclic graph.

Here is source code of the C Program to Find a Good Feedback Edge Set in a 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. #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 0;
  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.     printf("After checking whether the graph is DAG.");
  85.     int visited[n + 1];
  86.     for (i = 0; i < n; i++) {
  87.         while (array[i].ptr != NULL) {
  88.             printf("%d ", (array[i].ptr)->dest);
  89.             visited[i] = 1;
  90.             for (j = 0; j < n; j++) {
  91.  
  92.                 while (array[j].ptr != NULL) {
  93.                     printf("%d ", (array[j].ptr)->dest);
  94.                     if (visited[array[j].v] == 1) {
  95.                         printf("%d - %d", array[i].v, array[j].v);
  96.                     }
  97.                     array[j].ptr = (array[j].ptr)->next;
  98.                 }
  99.                 printf("\n");
  100.             }
  101.  
  102.             array[i].ptr = (array[i].ptr)->next;
  103.         }
  104.         printf("\n");
  105.     }
  106.  
  107.     return 1;
  108. }
  109. int main() {
  110.     int n = 6, i;
  111.     printf("Number of vertices: %d\n", n);
  112.  
  113.     for (i = 0; i < n; i++) {
  114.         array[i].v = i;
  115.         array[i].ptr = NULL;
  116.     }
  117.     addEdge(0, 1);
  118.     addEdge(1, 2);
  119.     addEdge(1, 3);
  120.     addEdge(3, 4);
  121.     addEdge(4, 5);
  122.     addEdge(3, 5);
  123.     addEdge(5, 2);
  124.     print_graph(n);
  125.     printf("Feedback arc Set: ");
  126.     if (checkDAG(n) == 0)
  127.         printf("None");
  128.  
  129.     return 0;
  130. }

Output:

$ gcc FeedbackArcSet.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 5 
Adjacency List of 4: 5 
Adjacency List of 5: 2 
Feedback arc Set: None

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.