Given a connected graph, check if the graph is bipartite or not. 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 an odd cycle using two colors.
- Use a color array which stores 0 or 1 for every node which denotes opposite colors.
- Call the function DFS from any node.
- If the node u has not been visited previously, then assign !color[v] to color[u] and call DFS again to visit nodes connected to u.
- If at any point, color[u] is equal to !color[v], then the node is bipartite.
- Modify the DFS function such that it returns a boolean value at the end.
Below is the implementation of the above approach:
Graph is Bipartite
Time Complexity: O(N)
Auxiliary Space: O(N)
- Check whether a given graph is Bipartite or not
- Maximum number of edges in Bipartite graph
- Maximum number of edges to be added to a tree so that it stays a Bipartite graph
- Check for star graph
- Check if a given graph is tree or not
- Check if a directed graph is connected or not
- Check if a given tree graph is linear or not
- Check if the given graph represents a Bus Topology
- Check if the given permutation is a valid DFS of graph
- Check if removing a given edge disconnects a graph
- Check if there is a cycle with odd weight sum in an undirected graph
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Check if the given graph represents a Ring Topology
- Check if the given graph represents a Star Topology
- Maximum Bipartite Matching
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : RishiAgrawal