我想生成多个具有随机边权值的Erdos-Renyi图。然而,因为代码中有两个嵌套循环,所以我的代码运行速度非常慢。我想知道是否有人可以帮助我改进代码。
import networkx as nx
import random
#Suppose I generate 1000 different random graphs
for _ in range(1000):
#Let's say I will have 100 nodes and the connection probability is 0.4
G= nx.fast_gnp_random_graph(100,0.4)
#Then, I assign random edge weights.
for (u, v) in G.edges():
G.edges[u,v]['weight'] = random.randint(15,5000)
当我在R
中使用igraph
运行类似的代码块时,无论网络大小如何,它都非常快。有哪些替代方法可以完成相同的任务而不会遇到执行时间缓慢的问题?
nx.nx.fast...
是否是打字错误。 - Joel