How to find if a graph is bipartite?

前端 未结 7 1740
一生所求
一生所求 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:39

    Make a bfs Tree.If there are edges between the vertexes of the same level of tree.Then the graph is non bipartite,else it is bipartite.

    0 讨论(0)
  • 2020-12-25 14:41

    Do it more simpler way.

    Run the strongly connected component algorithm.

    If any node of metagraph obtained has more than two vertices then the given graph is not bipartite.

    0 讨论(0)
  • 2020-12-25 14:42

    you can refer this link as given below
    this code contains Check whether a given graph is Bipartite or not using BFS Algorithm
    https://github.com/gangwar-yogendra/Graph/blob/master/BipartiteGraphUsingBFS.c

    0 讨论(0)
  • 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 .

    0 讨论(0)
  • 2020-12-25 14:58

    From Carnegie Mellon University:

    "Recall that a graph G = (V, E) is said to be bipartite if its vertex set V can be partitioned into two disjoint sets V1, V2 such that all edges in E. have one endpoint in V1 and one endpoint in V2.

    (source: http://www.cs.cmu.edu/~15251/homework/hw5.pdf)

    Are you sure that you need to use BFS? Determining if a graph if bipartite requires detecting cycle lengths, and DFS is probably better for cycle detection than BFS.

    Anyway, a graph if bipartite if and only if it has no cycles of odd length. If you're allowed to use DFS, you can DFS on the graph and check for back-edges to detect the presence of cycles and use DFS timestamps to compute the size of each cycle.

    0 讨论(0)
  • 2020-12-25 14:59

    Here is a Prolog CLP(FD) solution. Just model each edge in the graph as a variable in the domain 0..1. Then model each vertex as an equation:

    X #\= Y.
    

    Then issue labeling. If the labeling finds a solution, you are done. It might find multiple solutions though. Here is a run for your example:

    Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.23)
    Copyright (c) 1990-2015 University of Amsterdam, VU Amsterdam
    
    :- use_module(library(clpfd)).
    problem(L) :- L=[A,B,C,D,E,F,G],
        A #\= E, A #\= F,
        B #\= E,
        C #\= E, C #\= F, C #\= H,
        D #\= G, D #\= H,
        E #\= A, E #\= B, E #\= C,
        F #\= A, F #\= C, F #\= G,
        G #\= F, G #\= D,
        H #\= C, H #\= D.
    
    ?- problem(L), L ins 0..1, label(L).
    L = [0, 0, 0, 1, 1, 1, 0] ;
    L = [1, 1, 1, 0, 0, 0, 1].
    

    Works also in GNU Prolog, SICStus Prolog, Jekejeke Minlog etc.. mostly with any Prolog system that implements CLP(FD). Alternatively could also use CLP(B) or dif/2.

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