[英]How to create a connected graph in networkx
I want to create a connected graph in IPython
notebook through NetworkX
. 我想通过NetworkX
在IPython
笔记本中创建连接图。 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: 社交网络图表适合您:
karate_club_graph()
- Return Zachary's Karate club graph . karate_club_graph()
- 返回Zachary的空手道俱乐部图表 。 davis_southern_women_graph()
- Return Davis Southern women social network . davis_southern_women_graph()
- 返回戴维斯南方女性社交网络 。 florentine_families_graph()
- Return Florentine families graph . florentine_families_graph()
- 返回佛罗伦萨家族图 。 Community graphs : 社区图 :
caveman_graph(l, k)
- Returns a caveman graph of l
cliques of size k
. caveman_graph(l, k)
- 返回大小为k
的l
派系的穴居人图 。 connected_caveman_graph(l, k)
- Returns a connected caveman graph of n
cliques of size k
. connected_caveman_graph(l, k)
- 返回大小为k
的n
集团的连通穴居人图 。 relaxed_caveman_graph(l, k, p[, seed, directed])
- Return a relaxed caveman graph . relaxed_caveman_graph(l, k, p[, seed, directed])
- 返回一个放松的穴居人图 。 random_partition_graph(sizes, p_in, p_out[, ...])
- Return the random partition graph with a partition of sizes. random_partition_graph(sizes, p_in, p_out[, ...])
- 返回带有大小分区的随机分区图 。 planted_partition_graph(l, k, p_in, p_out[, ...])
- Return the planted l
-partition graph . planted_partition_graph(l, k, p_in, p_out[, ...])
- 返回种植的l
分区图 。 gaussian_random_partition_graph(n, s, v, ...)
- Generate a Gaussian random partition graph . gaussian_random_partition_graph(n, s, v, ...)
- 生成高斯随机分区图 。 ring_of_cliques
and so on. 等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.