I am using Python NetworkX pygraphviz_layout to plot a graph. I have the same set of nodes and edges, but everytime I run the code, the graph layout is different. I would like to get the same graph everytime.
I tried to search if there are any random seed that can be set but could not find any.
Here is my example code, and example of different graph layout being generated.
import matplotlib.pyplot as plt
import networkx as nx
import random
G = nx.DiGraph()
for i in range(100):
G.add_node(i)
random.seed(1)
for i in range(100):
a = random.randint(0,99)
b = random.randint(0,99)
G.add_edge(a,b)
pos = nx.nx_agraph.pygraphviz_layout(G, prog='fdp')
nx.draw(G, pos=pos, with_labels=True)
plt.savefig("plot_graph_folder/temp.png")
Unfortunately the algorithms we use are nondeterministic. We would like to fix this, there is an issue in the repo, but haven’t gotten the time. It’s probably a big job.
To give some background as to what’s going on in the Graphviz code itself, there are two main sources of non-determinism I’m aware of:
Intended random layout. Calls to rand and friends. Most of this is controllable with explicit seeds.
The use of pointers as IDs, implying ordering. This was a reasonable thing to do when the code was written, but in the modern era of ASLR this causes unintended variation.
Doesn’t that indicate Windows and everything else differ in how they generate a random seed, not what the default is?
Having said that, I don’t know why this needs OS-specific code here, nor why using your PID as a seed component is sensible, nor even why someone would want a randomized layout. But perhaps I am insufficiently creative.
Everything you say is reasonable. When we did this, we thought it would be cool if you could see a different layout every time you ran it. We never thought repeatability could be that important. Not defending it.