2

l would like to simulate and reproduce my results. Hence, l find it useful to use random seed number, to regenerate the same graphs.

However in this case l get different results :

seed=1725
my_random = np.random.RandomState(seed)
G = nx.watts_strogatz_graph(30, 3, 0,seed=my_random )
node_positions = nx.spring_layout(G, scale=len(G.nodes()))

l run this lines twice. As a result l get two different graph topology, different adjacency matrix and nodes position. 1) How can l make it works ?

Secondly :

The following graph classes don't accept seed paramete

G=nx.wheel_graph(n)
G=nx.complete_graph(n)
G = nx.balanced_tree(n,tree_depth)
G= nx.star_graph(n) 

2) How can make these graph classes take into consideration seed parameter

Thank you

eric lardon
  • 351
  • 1
  • 6
  • 21

1 Answers1

4

2) these graphs don't have any random aspects to their generation, so there isn't any point in accepting a seed as far as I can tell.

1) is more tricky. It seems that networkx uses a combination of random and numpy.random libraries for random number generation. The graph generators, e.g. watts_strogatz_graph use the former, while layout uses the latter.

Short answer: just set seeds for both libraries:

import random
import numpy as np
seed = 123
random.seed(seed)
np.random.seed(seed)

Explanation for what is going on

Anyway, here is a simple example showing that the seed is taken into account for graphs like the watts-strogatz generator (but you must have p>0 otherwise there is no re-wiring and hence no random component!). The edges differ between G2 and G3 because they used different seeds, but G1 and G2 are identical.

import networkx as nx

seed = 123
G1 = nx.watts_strogatz_graph(30, 3, 0.1, seed=seed)
G2 = nx.watts_strogatz_graph(30, 3, 0.1, seed=seed)
G3 = nx.watts_strogatz_graph(30, 3, 0.1, seed=seed+1)

G1.edge == G2.edge
>>> True
G3.edge == G2.edge
>>> False

To make sure the layout is the same each time, you could use a call to np.random.seed(myseed). This is different than using np.random.RandomState, which is creating a new instance of a random number stream, and would not be used by the nx layout functions. (though in your own random streams it is good practice to use an independent stream).

import numpy as np
np.random.seed(seed)
pos1 = nx.spring_layout(G1, scale=len(G.nodes()))
pos1b = nx.spring_layout(G1, scale=len(G.nodes())) 
# should differ! same graph, but the rng has been called
np.random.seed(seed) #reset seed
pos2 = nx.spring_layout(G2, scale=len(G.nodes())) 
# should be same as p1! G1==G2 (from above), and seed for layout
# is the same.

diffs = 0
for node in pos1:
    if np.all(pos1[node] != pos1b[node]):
       diffs += 1

print diffs
>>> 30

diffs2 = 0
for node in pos1:
    if np.all(pos1[node] != pos2[node]):
       diffs2 += 1

print diffs2
>>> 0

This question/answer Differences between numpy.random and random.random in Python has some description of the two random libraries.

Bonlenfum
  • 19,101
  • 2
  • 53
  • 56
  • Thank you for your answer. So in 2) there is no way to reproduce the graphs ? – eric lardon Mar 22 '18 at 11:18
  • no, it is the reverse: for the same input (eg number of nodes), you will *always* get the same graph! A balanced tree of depth 5 and branching 2 will always have 64-1 nodes with the same organisation; complete graphs have edges between every pair of nodes. They are deterministic graphs. – Bonlenfum Mar 22 '18 at 11:30
  • l mean do l get them in the same position. for instance if l generate two balanced trees of depth five and branching 2 do l get always tge same nodes in the same position ? – eric lardon Mar 22 '18 at 13:06
  • Layout and generation are independent, so repeating the layout for a deterministic graph needs the same approach as for a randomised one: set the numpy seed, as shown. If you didn't control the seed, you would get a different layout each time (at least for spring_layout and random_layout). – Bonlenfum Mar 22 '18 at 16:13
  • See also this: https://stackoverflow.com/a/11484144 which uses an external library to handle layout. In this form of graph it can be more consistent. (though you'd have to read the graphviz docs to be sure of randomisation) – Bonlenfum Mar 22 '18 at 16:15