pythongraphnetworkxbipartitecomplement

How to get the bipartite complement graph in networkx/python?


I have bipartite graph and I would like to extract the bipartite complement of this graph. This is the G' graph explained in this link : 

https://www.quora.com/Given-a-bipartite-graph-how-can-I-find-its-subgraph-that-is-a-complete-bipartite-graph-and-has-the-most-vertices

I tried to do it in using the complement algorithm of the Networkx library but I got edges between my vertices A and B that shouldn't be connected because in bipartite graph there are no edges between a same group of vertices.  

Here is the code that I tried :

from networkx.algorithms.operators.unary import complement

B = bipartite.random_graph(5, 7, 0.2)
B = complement(B)

But I have got connections into same group of vertices. Is there a networkx function or Python function that handle it ?


Solution

  • Try this:

    import networkx as nx
    B = nx.bipartite.random_graph(5, 7, 0.2)
    G = nx.bipartite.complete_bipartite_graph(5,7)   #or use random_graph with probability 1
    H = nx.difference(G,B)
    

    This uses difference, which returns a graph whose edges are the edges in G but not B.


    The problem with what you were doing is that complement does not return the bipartite complement, but rather the full complement. It contains edges between all pairs that were not joined in the original graph.