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)
The above algorithm works only if the graph is 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 Bipartite. 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.
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).
Another approach by observation :
From the property of graphs we can infer that , A graph containing odd number of cycles or Self loop is Not Bipartite.
Therefore if we found any vertex with odd number of edges or a self loop , we can say that it is Not Bipartite.
Below is the implementation of above observation:
link brightness_4 code
# Python3 program to find out whether a
# given graph is Bipartite or not
self.graph =[[0forcolumn inrange(V)]
# Return false if there is a self-loop
# An edge from i to j
# exists and count number of edges
# if there are odd number of edges return False
g.graph =[[0, 1, 0, 1],
[1, 0, 1, 0],
[0, 1, 0, 1],
[1, 0, 1, 0]]
# This code is contributed by Aanchal Tiwari.
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.
Exercise: 1. Can DFS algorithm be used to check the bipartite-ness of a graph? If yes, how? Solution :
link brightness_4 code
// C++ program to find out whether a given graph is Bipartite or not.