Testing Bipartiteness

  • We can implement an algorithm to test whether a graph is bipartite by simply taking the implementation of BFS and adding an extra array Color over the nodes.
  • Whenever we get to a step in BFS where we are adding a node to a list , we assign:
    • Color[v] = red if is an even number,
    • Color[v] = blue if is an odd number.
  • At the end of this procedure, we simply scan all the edges:
    • if there is any edge for which both ends received the same color, then the graph is not bipartite.
    • Otherwise, the graph is bipartite.
  • Thus, the total running time for the coloring algorithm is , just as it is for BFS.