C Program to Check Whether a Given Graph is Bipartite or not

This is a C Program to check if a graph is Bipartite. A bipartite graph or bigraph is a graph whose vertices can be divided into two disjoint sets P and Q (that is, P and Q are each completely independent sets) such that every edge connects a vertex in P to one in Q. Vertex set P and Q are often denoted as partite sets. Also it does not contain any odd-length cycles.

Here is source code of the C Program to Check if a Given Graph is Bipartite. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2. #define V 4
  3. /*
  4.  * A utility function to check if the current color assignment
  5.  * is safe for vertex v
  6.  */
  7. int isSafe(int v, int graph[V][V], int color[], int c) {
  8.     int i;
  9.     for (i = 0; i < V; i++)
  10.         if (graph[v][i] && c == color[i])
  11.             return 0;
  12.     return 1;
  13. }
  14. /*
  15.  * A recursive utility function to solve m coloring problem
  16.  */
  17. int graphColoringUtil(int graph[V][V], int m, int color[], int v) {
  18.     int c;
  19.     if (v == V)
  20.         return 1;
  21.     for (c = 1; c <= m; c++) {
  22.         if (isSafe(v, graph, color, c)) {
  23.             color[v] = c;
  24.             if (graphColoringUtil(graph, m, color, v + 1) == 1)
  25.                 return 1;
  26.             color[v] = 0;
  27.         }
  28.     }
  29.     return 0;
  30. }
  31. /*
  32.  * This function solves the m Coloring problem using Backtracking.
  33.  */
  34. int graphColoring(int graph[V][V], int m) {
  35.     int *color = malloc(V * sizeof(int[V]));
  36.     int i;
  37.     for (i = 0; i < V; i++)
  38.         color[i] = 0;
  39.     if (graphColoringUtil(graph, m, color, 0) == 0)
  40.         return 0;
  41.     return 1;
  42. }
  43. /*
  44.  * Main Contains Menu
  45.  */
  46. int main() {
  47.     int graph[V][V] = { { 0, 1, 1, 1 },
  48.                         { 1, 0, 1, 0 },
  49.                         { 1, 1, 0, 1 },
  50.                         { 1, 0, 1, 0 },
  51.                       };
  52.     int gr[V][V] = { { 0, 1, 0, 1 },
  53.                      { 1, 0, 1, 0 },
  54.                      { 0, 1, 0, 1 },
  55.                      { 1, 0, 1, 0 }
  56.                    };
  57.     int m = 2;
  58.     if (graphColoring(graph, m))
  59.         printf("The graph 1 is Bipartite\n");
  60.     else
  61.         printf("The graph 1 is not Bipartite\n");
  62.     if (graphColoring(gr, m))
  63.         printf("The graph 2 is Bipartite\n");
  64.     else
  65.         printf("The graph 2 is not Bipartite\n");
  66.     return 0;
  67. }

Output:

$ gcc CheckBipartite.c
$ ./a.out
 
The graph 1 is not Bipartite
The graph 2 is Bipartite

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.