How to generate a random graph, given the number of nodes and edges?

I am using python with the igraph library:

from igraph import *
g = Graph()
g.add_vertices(4)
g.add_edges([(0,2),(1,2),(3,2)])
print g.betweenness()

I would like to create a random graph with 10,000 nodes and 100,000 edges. Edges may be random. Please suggest a way to have random edges (using numpy.random.rand)

+4
source share
2 answers

Do you need to use numpy.random.rand? If not, just use Graph.Erdos_Renyione that allows you to directly specify the number of nodes and edges:

g = Graph.Erdos_Renyi(n=10000, m=100000)
+11
source

To do this with numpy.random.rand, generate a random array, set it with the probability you want, and then pass it Graph.Adjacency:

adj = numpy.random.rand(edges, edges)
adj[adj > 0.999] = 1 # sets everything over 0.999 to 1
adj[adj <= 0.999] = 0 # sets everything below to 0
graph = Graph.Adjacency(adj)

, adj [i, j] , → j. , Graph.Ajacency(adj, ADJ_UNDIRECTED) - , adj[i, j] == 1, adj[j, i] == 1.

, 100 000 - ?

0

All Articles