How to find if a graph is bipartite?

前端 未结 7 1815
一生所求
一生所求 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 14:49

    Taken from GeeksforGeeks

    Following is a simple algorithm to find out whether a given graph is Birpartite or not using Breadth First Search (BFS) :-

    1. Assign RED color to the source vertex (putting into set U).
    2. Color all the neighbors with BLUE color (putting into set V).
    3. Color all neighbor’s neighbor with RED color (putting into set U).
    4. This way, assign color to all vertices such that it satisfies all the constraints of m way coloring problem where m = 2.
    5. 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).

    A bipartite graph is possible if the graph coloring is possible using two colors such that vertices in a set are colored with the same color.

    Also, NOTE :-

    -> It is possible to color a cycle graph with even cycle using two colors.

    -> It is not possible to color a cycle graph with odd cycle using two colors.

    EDIT :-

    If a graph is not connected, it may have more than one bipartition. You need to check all those components separately with the algorithm as mentioned above.

    So, for various disconnected sub-graph of the same graph, you need to perform this bipartition check on all of them separately using the same algorithm discussed above. All of those various disconnected sub-graph of the same graph will account for its own set of bipartition.

    And, the graph will be termed bipartite, IF AND ONLY IF, each of its connected components are proved to be bipartite .

提交回复
热议问题