简体   繁体   中英

How to create a connected graph in networkx

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:

Community graphs :

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.

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