I want to create a connected graph in IPython
notebook through NetworkX
. 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
.
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.
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):
Social Networks graphs for you:
karate_club_graph()
- Return Zachary's Karate club graph . davis_southern_women_graph()
- Return Davis Southern women social network . florentine_families_graph()
- Return Florentine families graph . caveman_graph(l, k)
- Returns a caveman graph of l
cliques of size k
. connected_caveman_graph(l, k)
- Returns a connected caveman graph of n
cliques of size k
. relaxed_caveman_graph(l, k, p[, seed, directed])
- Return a relaxed caveman graph . random_partition_graph(sizes, p_in, p_out[, ...])
- Return the random partition graph with a partition of sizes. planted_partition_graph(l, k, p_in, p_out[, ...])
- Return the planted l
-partition graph . gaussian_random_partition_graph(n, s, v, ...)
- Generate a Gaussian random partition graph . ring_of_cliques
and so on.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.