How to find if a graph is bipartite?

前端 未结 7 1741
一生所求
一生所求 2020-12-25 14:16

I have been trying to understand the bipartite graph. To my understanding it is a graph G which can be divided into two subgraphs U and V.So that intersection of U and V is

相关标签:
7条回答
  • 2020-12-25 15:05

    The following is a BFS approach to check whether the graph is bipartite.

    • c = 0
    • pick a node x and set x.class = c
    • let ys be the nodes obtained by BFS
      • c = 1-c
      • for y in ys set y.class = c
      • if any y in ys has a neighbour z with z.class == c then the graph is not bipartite
      • repeat until no more nodes are found
    • the graph is bipartite

    This assumes that the graph is a single connected component -- if it is not, simply do this procedure for each component.

    0 讨论(0)
提交回复
热议问题