문제 설명
networkx에서 임의의 에지 가중치를 사용하여 여러 임의의 그래프를 효율적으로 생성하는 방법 (How to efficiently generate multiple random graphs with random edge weights in networkx)
무작위 간선 가중치를 사용하여 여러 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)
igraph
를 사용하여 R
에서 유사한 코드 블록을 실행하면 정말 좋습니다. 네트워크 크기에 관계없이 빠릅니다. 느린 실행 시간 없이 동일한 작업을 수행할 수 있는 다른 방법은 무엇입니까?
참조 솔루션
방법 1:
This benchmark shows the performance of many graphs libraries (from different languages). It confirms NetworkX is very slow. The graph‑tool Python package seems a significantly faster alternative to NetworkX. Please note that the performance of a given package is dependent of what you want to achieve because the performance of a graph algorithm is very dependent of the chosen internal representation.
(by sergey_208、Jérôme Richard)