10

I am using python with 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 generate a random graph with 10000 nodes and 100000 edges. The edges can be random. Please suggest a way to have random edges (using numpy.random.rand )

Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
Kush Jain
  • 651
  • 2
  • 8
  • 11

2 Answers2

16

Do you have to use numpy.random.rand? If not, just use Graph.Erdos_Renyi, which lets you specify the number of nodes and edges directly:

g = Graph.Erdos_Renyi(n=10000, m=100000)
GalDude33
  • 7,071
  • 1
  • 28
  • 38
Tamás
  • 47,239
  • 12
  • 105
  • 124
0

To do it with numpy.random.rand, generate the random array, threshold it with the probability you want, and then pass it to 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)

This generates a directed graph, where adj[i, j] tells you whether there's an edge from i -> j. If you want an undirected one instead, use Graph.Ajacency(adj, ADJ_UNDIRECTED) - then there'll be an edge if either adj[i, j] == 1 or adj[j, i] == 1.

This isn't guaranteed to give you exactly 100,000 edges - do you need that?

babbageclunk
  • 8,523
  • 1
  • 33
  • 37
  • First, this is unfortunately not what he wanted. He wants a given number of edges. Second, this is a very inefficient way to generate a random G(n,p) graph if the graph is sparse. – Gabor Csardi Dec 03 '13 at 12:58
  • Ooh, right you are - I missed the number of edges. I think my basic approach can still work for the number of nodes he's talking about - the adjacency matrix is wasteful (100M cells for 100k edges), but you only need it to create the graph. – babbageclunk Dec 03 '13 at 13:34
  • `numpy.random.rand(edges, edges)` should be `numpy.random.rand(nodes, nodes)` I think – Björn Lindqvist Nov 17 '19 at 14:38