简体   繁体   English

如何在networkx中创建连接图

[英]How to create a connected graph in networkx

I want to create a connected graph in IPython notebook through NetworkX . 我想通过NetworkXIPython笔记本中创建连接图。 Previously, I use 以前,我用

erdos_renyi_graph

to generate a random graph, but I never get a connected graph, I want to use this graph to prove that my graph is a small world network. 生成随机图,但我从来没有得到连接图,我想用这个图来证明我的图是一个小世界网络。 But the unconnected graph's average shortest path cannot be calculated. 但无法计算未连接图的平均最短路径。 So please tell me how to generate a connected graph through NetworkX . 所以请告诉我如何通过NetworkX生成连接图。

As you didn't mention the exact parameters of your graph I want to suggest playing with the probability of creating edges. 由于您没有提到图表的确切参数,我想建议使用创建边缘的概率。 The following networkx function allows you to provide a probability (p) for an edge to exist in the graph. 以下networkx函数允许您为图中存在的边提供概率(p)。

erdos_renyi_graph(n, p, seed=None, directed=False)

As an example: 举个例子:

G = nx.erdos_renyi_graph(500, 0.5, seed=123, directed=False)

provides you a fully connected graph. 为您提供完全连接的图表。

There are a lot of graph generators for NetworkX defined, you can use not only the erdos_renyi_graph (which can be adjusted to nearly connected by second parameter): NetworkX定义了很多图形生成器,不仅可以使用erdos_renyi_graph (可以通过第二个参数调整到几乎连接):

Social Networks graphs for you: 社交网络图表适合您:

Community graphs : 社区图

and so on. 等等。

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

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