Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
Last active August 29, 2015 14:04
Show Gist options
  • Save kartikkukreja/467753d8c5e50b3751b9 to your computer and use it in GitHub Desktop.
Save kartikkukreja/467753d8c5e50b3751b9 to your computer and use it in GitHub Desktop.
Testing Bipartiteness
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