networkx에서 임의의 에지 가중치를 사용하여 여러 임의의 그래프를 효율적으로 생성하는 방법 (How to efficiently generate multiple random graphs with random edge weights in networkx)


문제 설명

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_208Jérôme Richard)

참조 문서

  1. How to efficiently generate multiple random graphs with random edge weights in networkx (CC BY‑SA 2.5/3.0/4.0)

#networkx #performance #Python #for-loop #nested-loops






관련 질문

단순 팻테일 로그 비닝 (Simple fat-tailed log-binning)

NetworkX - 노드 묶기 중지 - 시도된 Scale/K 매개변수 (NetworkX - Stop Nodes from Bunching Up - Tried Scale/K parameters)

문자열의 int 부분으로 문자열 레이블의 노드에 액세스 (Access a node of string label with an int part of the string)

시계열 분석을 위해 Pandas Groupby 및 date_range를 사용하는 동안 오류가 발생했습니다. (Error using Pandas Groupby and date_range for timeseries analysis)

regraph 계층 구조에서 가장자리 속성을 자리 표시자로 사용하는 방법이 있습니까? (Is there a way to have edge attributes as placeholders in a regraph hierarchy?)

networkx .module 'matplotlib.pyplot'에서 그래프를 그리는 동안 오류가 발생했습니다. (Error while drawing a graph in networkx .module 'matplotlib.pyplot' has no attribute 'ishold')

svg의 NetworkX node_shape (NetworkX node_shape from svg)

그래프를 통해 그래프 노드인 클래스 인스턴스의 속성에 액세스하는 방법은 무엇입니까? (How to access attributes of a class instance which is a graph node via the graph?)

그래프의 이미지를 분리하는 방법은 무엇입니까? (How to separate images of graphs?)

networkx 도면을 업데이트하는 방법 (How to update a networkx drawing)

그래프 노드 간의 메시지 흐름을 위한 Python 함수 (Python function for message flow between nodes of a graph)

networkx에서 임의의 에지 가중치를 사용하여 여러 임의의 그래프를 효율적으로 생성하는 방법 (How to efficiently generate multiple random graphs with random edge weights in networkx)







코멘트