6

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.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
lala
  • 291
  • 1
  • 3
  • 9
  • option 1) take the largest component of the Erdos-Renyi graph. option 2) Read the original papers on small-world networks and you'll have a better idea of how one shows that a graph is a small-world network. – Joel Apr 18 '17 at 13:50
  • [Here's one approach](https://stackoverflow.com/a/61961881/9698684) adapting the Erdős-Rényi graph generator – yatu May 22 '20 at 19:28

2 Answers2

4

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.

xTikka
  • 103
  • 5
3

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.

VMAtm
  • 27,943
  • 17
  • 79
  • 125