C Program to Find the Vertex Connectivity of a Graph

This is a C Program to find vertex connectivity of a graph. A vertex in an undirected connected graph is an articulation point if and only if removing it disconnects the graph.

Here is source code of the C Program to Find the Vertex Connectivity of 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 <string.h>
  3.  
  4. #define MAXN 200
  5. #define min(a,b) (((a)<(b))?(a):(b))
  6.  
  7. typedef struct {
  8.     int deg;
  9.     int adj[MAXN];
  10. } Node;
  11.  
  12. Node alist[MAXN];
  13. char ART[MAXN], val[MAXN];
  14. int id;
  15.  
  16. void addEdge(int x, int y) {
  17.     alist[x].adj[alist[x].deg++] = y;
  18.     alist[y].adj[alist[y].deg++] = x;
  19. }
  20.  
  21. void clearList() {
  22.     memset(alist, 0, sizeof(alist));
  23. }
  24.  
  25. int visit(int x, int root) {
  26.     int i, y, m, res, child = 0;
  27.  
  28.     res = val[x] = ++id;
  29.     for (i = 0; i < alist[x].deg; i++) {
  30.         y = alist[x].adj[i];
  31.         if (!val[y]) {
  32.             if (root && ++child > 1)
  33.                 ART[x] = 1;
  34.             m = visit(y, 0);
  35.             res = min(res, m);
  36.             if (m >= val[x] && !root)
  37.                 ART[x] = 1;
  38.         } else {
  39.             res = min(val[y], res);
  40.         }
  41.     }
  42.     return res;
  43. }
  44.  
  45. void articulate(int n) {
  46.     int i;
  47.  
  48.     memset(ART, 0, sizeof(ART));
  49.     memset(val, 0, sizeof(val));
  50.     for (id = i = 0; i < n; i++)
  51.         if (!val[i])
  52.             visit(i, 1);
  53. }
  54.  
  55. int main() {
  56.     int i, n, m, x, y, found;
  57.  
  58.     /* Read in number of vertices, number of edges */
  59.     while (scanf("%d %d", &n, &m) == 2) {
  60.  
  61.         /* Read in edge between node x and node y */
  62.         for (i = 0; i < m; i++) {
  63.             scanf("%d %d", &x, &y);
  64.             addEdge(x, y);
  65.         }
  66.  
  67.         /* Find articulation points */
  68.         articulate(n);
  69.  
  70.         for (found = i = 0; i < n; i++)
  71.             if (ART[i]) {
  72.                 printf("Node %d is an articulation point\n", i);
  73.                 found = 1;
  74.             }
  75.         if (!found)
  76.             printf("No articulation points\n");
  77.         clearList();
  78.     }
  79.     return 0;
  80. }

Output:

$ gcc VertexConnectivity.c
$ ./a.out
 
6 7
0 1
1 2
1 3
3 4
4 5
5 3
5 2
 
Node 1 is an articulation point

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.