简体   繁体   English

如何在networkx/python中获得二部补图?

[英]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 :这是此链接中解释的 G' 图:

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 https://www.quora.com/Given-a-bipartite-graph-how-can-I-find-its-subgraph-that-is-a-complete-bipartite-graph-and-has-the-most-顶点

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.我尝试使用 Networkx 库的补码算法来做到这一点,但我的顶点 A 和 B 之间有不应连接的边,因为在二部图中,同一组顶点之间没有边。

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 ?是否有处理它的 networkx 函数或 Python 函数?

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 .这使用了差异,它返回一个图,其边是G中的边而不是B中的边。


The problem with what you were doing is that complement does not return the bipartite complement, but rather the full complement.你所做的问题是complement不返回二分补码,而是全补码。 It contains edges between all pairs that were not joined in the original graph.它包含原始图中未连接的所有对之间的边。

using * helps us to import all the modules from networkx package.使用 * 帮助我们从 networkx 包中导入所有模块。 We have a function called complement in networkx package, I used it in the code given below.我们在 networkx 包中有一个叫做补全的函数,我在下面给出的代码中使用了它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM