How to find if a graph is bipartite?

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

提交回复
热议问题