Check Bipartite Graph
Check Bipartite Graph
1. Idea
Assign RED color to the source vertex (putting into set U).
Color all the neighbors with BLUE color (putting into set V).
Color all neighbor’s neighbor with RED color (putting into set U).
This way, assign color to all vertices such that it satisfies all the constraints of m way coloring problem where m = 2.
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)
2. Implementation
3. Time & Space Complexity
Time: O(V^2) for adjacent matrix and O(V + E) for adjacent list
Space: O(V^2) for adjacent matrix and O(V + E) for adjacent list
Last updated