Last active
August 29, 2015 14:04
-
-
Save kartikkukreja/467753d8c5e50b3751b9 to your computer and use it in GitHub Desktop.
Testing Bipartiteness
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Assume graph G is connected. Otherwise, we can run the algorithm for each connected component of G. | |
Let q be an empty queue | |
Pick any vertex s ∈ V and color it Red | |
q.enqueue(s) | |
while !q.empty() | |
u = q.dequeue() | |
foreach v in u.adjList: | |
if v.color is nil: | |
v.color = (u.color == Red) ? Black : Red | |
q.enqueue(v) | |
elif v.color == u.color: | |
return "Not Bipartite" | |
return "Bipartite" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment