gpt4 book ai didi

python - Networkx:将 MultiDiGraph 转换为 DiGraph

转载 作者:行者123 更新时间:2023-12-01 06:43:12 33 4
gpt4 key购买 nike

有没有一种快速方法可以将networkx MultiDiGraph对象转换为DiGraph?我正在创建一个 scale_free_graph使用此代码:

import networkx as nx
G = nx.scale_free_graph(100)

但我想从我的图中删除所有自环和平行边。

最佳答案

您可以使用nx.MultiDiGraph as input graph创建新的nx.DiGraph :

incoming_graph_data (input graph (optional, default: None)) – Data to initialize graph. If None (default) an empty graph is created. The data can be an edge list, or any NetworkX graph object. If the corresponding optional Python packages are installed the data can also be a NumPy matrix or 2d ndarray, a SciPy sparse matrix, or a PyGraphviz graph.

import networkx as nx

G = nx.MultiDiGraph()
G.add_edges_from([
(1, 2),
(1, 2),
(1, 2)
])
print('G edges:', G.edges)
H = nx.DiGraph(G)
print('H edges:', H.edges)

将打印:

G edges: [(1, 2, 0), (1, 2, 1), (1, 2, 2)]
H edges: [(1, 2)]

关于python - Networkx:将 MultiDiGraph 转换为 DiGraph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59355214/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com