1

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 ?

anthonya
  • 565
  • 2
  • 6
  • 15

2 Answers2

1

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.

Joel
  • 22,598
  • 6
  • 69
  • 93
  • Thank you. What if I create a bipartite graph from scratch and I want to convert it into a complete bipartite graph to make the difference ? – anthonya May 29 '19 at 13:49
  • If I understand your question correctly: you can do the same thing I've shown, with your own steps for creating `B`. You need to be careful that the node names in your `B` will correspond to the names in `G`. There are ways to rename the nodes in `G` if needed. – Joel May 29 '19 at 14:13
0

using * helps us to import all the modules from networkx package. We have a function called complement in networkx package, I used it in the code given below.

from networkx import *
import networkx as nx
c=nx.complete_bipartite_graph(2,5)
cp=nx.complement(c)
nx.draw(cp)
John Paul
  • 1
  • 1