A Bipartite Graph is a graph whose vertices can be divided into two independent sets, U and V such that every edge (u, v) either connects a vertex from U to V or a vertex from V to U. In other words, for every edge (u, v), either u belongs to U and v to V, or u belongs to V and v to U. We can also say that there is no edge that connects vertices of same set.

A bipartite graph is possible if the graph coloring is possible using two colors such that vertices in a set are colored with the same color. Note that it is possible to color a cycle graph with even cycle using two colors. For example, see the following graph.

It is not possible to color a cycle graph with odd cycle using two colors.

Algorithm to check if a graph is Bipartite:
One approach is to check whether the graph is 2-colorable or not using backtracking algorithm m coloring problem.
Following is a simple algorithm to find out whether a given graph is Birpartite or not using Breadth First Search (BFS).
1. Assign RED color to the source vertex (putting into set U).
2. Color all the neighbors with BLUE color (putting into set V).
3. Color all neighbor’s neighbor with RED color (putting into set U).
4. This way, assign color to all vertices such that it satisfies all the constraints of m way coloring problem where m = 2.
5. While assigning colors, if we find a neighbor which is colored with same color as current vertex, then the graph cannot be colored with 2 vertices (or graph is not Bipartite)

[ad type=”banner”]

C++ Programming:

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20to%20find%20out%20whether%20a%20given%20graph%20is%20Bipartite%20or%20not%0A%23include%20%3Ciostream%3E%0A%23include%20%3Cqueue%3E%0A%23define%20V%204%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20This%20function%20returns%20true%20if%20graph%20G%5BV%5D%5BV%5D%20is%20Bipartite%2C%20else%20false%0Abool%20isBipartite(int%20G%5B%5D%5BV%5D%2C%20int%20src)%0A%7B%0A%20%20%20%20%2F%2F%20Create%20a%20color%20array%20to%20store%20colors%20assigned%20to%20all%20veritces.%20Vertex%20%0A%20%20%20%20%2F%2F%20number%20is%20used%20as%20index%20in%20this%20array.%20The%20value%20′-1’%20of%20%20colorArr%5Bi%5D%20%0A%20%20%20%20%2F%2F%20is%20used%20to%20indicate%20that%20no%20color%20is%20assigned%20to%20vertex%20’i’.%20%20The%20value%20%0A%20%20%20%20%2F%2F%201%20is%20used%20to%20indicate%20first%20color%20is%20assigned%20and%20value%200%20indicates%20%0A%20%20%20%20%2F%2F%20second%20color%20is%20assigned.%0A%20%20%20%20int%20colorArr%5BV%5D%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20V%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20colorArr%5Bi%5D%20%3D%20-1%3B%0A%20%0A%20%20%20%20%2F%2F%20Assign%20first%20color%20to%20source%0A%20%20%20%20colorArr%5Bsrc%5D%20%3D%201%3B%0A%20%0A%20%20%20%20%2F%2F%20Create%20a%20queue%20(FIFO)%20of%20vertex%20numbers%20and%20enqueue%20source%20vertex%0A%20%20%20%20%2F%2F%20for%20BFS%20traversal%0A%20%20%20%20queue%20%3Cint%3E%20q%3B%0A%20%20%20%20q.push(src)%3B%0A%20%0A%20%20%20%20%2F%2F%20Run%20while%20there%20are%20vertices%20in%20queue%20(Similar%20to%20BFS)%0A%20%20%20%20while%20(!q.empty())%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Dequeue%20a%20vertex%20from%20queue%20(%20Refer%20http%3A%2F%2Fgoo.gl%2F35oz8%20)%0A%20%20%20%20%20%20%20%20int%20u%20%3D%20q.front()%3B%0A%20%20%20%20%20%20%20%20q.pop()%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%2F%2F%20Find%20all%20non-colored%20adjacent%20vertices%0A%20%20%20%20%20%20%20%20for%20(int%20v%20%3D%200%3B%20v%20%3C%20V%3B%20%2B%2Bv)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20An%20edge%20from%20u%20to%20v%20exists%20and%20destination%20v%20is%20not%20colored%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(G%5Bu%5D%5Bv%5D%20%26%26%20colorArr%5Bv%5D%20%3D%3D%20-1)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Assign%20alternate%20color%20to%20this%20adjacent%20v%20of%20u%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20colorArr%5Bv%5D%20%3D%201%20-%20colorArr%5Bu%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20q.push(v)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20%20An%20edge%20from%20u%20to%20v%20exists%20and%20destination%20v%20is%20colored%20with%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20same%20color%20as%20u%0A%20%20%20%20%20%20%20%20%20%20%20%20else%20if%20(G%5Bu%5D%5Bv%5D%20%26%26%20colorArr%5Bv%5D%20%3D%3D%20colorArr%5Bu%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20If%20we%20reach%20here%2C%20then%20all%20adjacent%20vertices%20can%20be%20colored%20with%20%0A%20%20%20%20%2F%2F%20alternate%20color%0A%20%20%20%20return%20true%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20G%5B%5D%5BV%5D%20%3D%20%7B%7B0%2C%201%2C%200%2C%201%7D%2C%0A%20%20%20%20%20%20%20%20%7B1%2C%200%2C%201%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%7B0%2C%201%2C%200%2C%201%7D%2C%0A%20%20%20%20%20%20%20%20%7B1%2C%200%2C%201%2C%200%7D%0A%20%20%20%20%7D%3B%0A%20%0A%20%20%20%20isBipartite(G%2C%200)%20%3F%20cout%20%3C%3C%20%22Yes%22%20%3A%20cout%20%3C%3C%20%22No%22%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

Yes

The above algorithm works only if the graph is strongly connected. In above code, we always start with source 0 and assume that vertices are visited from it. One important observation is a graph with no edges is also Bipiartite. Note that the Bipartite condition says all edges should be from one set to another.

We can extend the above code to handle cases when a graph is not connected. The idea is repeatedly call above method for all not yet visited vertices.

[ad type=”banner”]

C++ Programming:

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20to%20find%20out%20whether%20a%20given%20graph%20is%20Bipartite%20or%20not.%0A%2F%2F%20It%20works%20for%20disconnected%20graph%20also.%0A%23include%20%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0Aconst%20int%20V%20%3D%204%3B%0A%20%0A%2F%2F%20This%20function%20returns%20true%20if%20graph%20G%5BV%5D%5BV%5D%20is%20Bipartite%2C%0A%2F%2F%20else%20false%0Abool%20isBipartiteUtil(int%20G%5B%5D%5BV%5D%2C%20int%20src%2C%20int%20colorArr%5B%5D)%0A%7B%0A%20%20%20%20colorArr%5Bsrc%5D%20%3D%201%3B%0A%20%0A%20%20%20%20%2F%2F%20Create%20a%20queue%20(FIFO)%20of%20vertex%20numbers%20and%20enqueue%0A%20%20%20%20%2F%2F%20source%20vertex%20for%20BFS%20traversal%0A%20%20%20%20queue%20%3Cint%3E%20q%3B%0A%20%20%20%20q.push(src)%3B%0A%20%0A%20%20%20%20%2F%2F%20Run%20while%20there%20are%20vertices%20in%20queue%20(Similar%20to%20BFS)%0A%20%20%20%20while%20(!q.empty())%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Dequeue%20a%20vertex%20from%20queue%20(%20Refer%20http%3A%2F%2Fgoo.gl%2F35oz8%20)%0A%20%20%20%20%20%20%20%20int%20u%20%3D%20q.front()%3B%0A%20%20%20%20%20%20%20%20q.pop()%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%2F%2F%20Find%20all%20non-colored%20adjacent%20vertices%0A%20%20%20%20%20%20%20%20for%20(int%20v%20%3D%200%3B%20v%20%3C%20V%3B%20%2B%2Bv)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20An%20edge%20from%20u%20to%20v%20exists%20and%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20destination%20v%20is%20not%20colored%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(G%5Bu%5D%5Bv%5D%20%26%26%20colorArr%5Bv%5D%20%3D%3D%20-1)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Assign%20alternate%20color%20to%20this%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20adjacent%20v%20of%20u%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20colorArr%5Bv%5D%20%3D%201%20-%20colorArr%5Bu%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20q.push(v)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20An%20edge%20from%20u%20to%20v%20exists%20and%20destination%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20v%20is%20colored%20with%20same%20color%20as%20u%0A%20%20%20%20%20%20%20%20%20%20%20%20else%20if%20(G%5Bu%5D%5Bv%5D%20%26%26%20colorArr%5Bv%5D%20%3D%3D%20colorArr%5Bu%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20If%20we%20reach%20here%2C%20then%20all%20adjacent%20vertices%20can%0A%20%20%20%20%2F%2F%20be%20colored%20with%20alternate%20color%0A%20%20%20%20return%20true%3B%0A%7D%0A%20%0A%2F%2F%20Returns%20true%20if%20G%5B%5D%5B%5D%20is%20Bipartite%2C%20else%20false%0Abool%20isBipartite(int%20G%5B%5D%5BV%5D)%0A%7B%0A%20%20%20%20%2F%2F%20Create%20a%20color%20array%20to%20store%20colors%20assigned%20to%20all%0A%20%20%20%20%2F%2F%20veritces.%20Vertex%2F%20number%20is%20used%20as%20index%20in%20this%0A%20%20%20%20%2F%2F%20array.%20The%20value%20′-1’%20of%20%20colorArr%5Bi%5D%20is%20used%20to%0A%20%20%20%20%2F%2F%20ndicate%20that%20no%20color%20is%20assigned%20to%20vertex%20’i’.%0A%20%20%20%20%2F%2F%20The%20value%201%20is%20used%20to%20indicate%20first%20color%20is%0A%20%20%20%20%2F%2F%20assigned%20and%20value%200%20indicates%20second%20color%20is%0A%20%20%20%20%2F%2F%20assigned.%0A%20%20%20%20int%20colorArr%5BV%5D%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20V%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20colorArr%5Bi%5D%20%3D%20-1%3B%0A%20%0A%20%20%20%20%2F%2F%20This%20code%20is%20to%20handle%20disconnected%20graoh%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20if%20(colorArr%5Bi%5D%20%3D%3D%20-1)%0A%20%20%20%20%20%20%20%20if%20(isBipartiteUtil(G%2C%20i%2C%20colorArr)%20%3D%3D%20false)%0A%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%0A%20%20%20%20%20return%20true%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20G%5B%5D%5BV%5D%20%3D%20%7B%7B0%2C%201%2C%200%2C%201%7D%2C%0A%20%20%20%20%20%20%20%20%7B1%2C%200%2C%201%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%7B0%2C%201%2C%200%2C%201%7D%2C%0A%20%20%20%20%20%20%20%20%7B1%2C%200%2C%201%2C%200%7D%0A%20%20%20%20%7D%3B%0A%20%0A%20%20%20%20isBipartite(G)%20%3F%20cout%20%3C%3C%20%22Yes%22%20%3A%20cout%20%3C%3C%20%22No%22%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

Yes

Time Complexity of the above approach is same as that Breadth First Search. In above implementation is O(V^2) where V is number of vertices. If graph is represented using adjacency list, then the complexity becomes O(V+E).

[ad type=”banner”]