gpt4 book ai didi

python - 如何在 python 中的 networkx 中绘制具有重复边的图形

转载 作者:太空宇宙 更新时间:2023-11-04 08:14:31 26 4
gpt4 key购买 nike

我需要绘制一个具有重复边的图(即,2 个节点之间有多个边)。我试过:

import networkx as nx
edges = [(0, 1), (0, 1)]

G = nx.DiGraph ()
G.add_edges_from (edges)

print G.edges ()
#[(0, 1)]

重复的条目被简单地丢弃。有什么办法吗?

最佳答案

我也不是 networkx 方面的专家,但是根据文档 here :

Multigraphs NetworkX provides classes for graphs which allow multiple edges between any pair of nodes. The MultiGraph and MultiDiGraph classes allow you to add the same edge twice, possibly with different edge data. This can be powerful for some applications, but many algorithms are not well defined on such graphs. Shortest path is one example. Where results are well defined, e.g. MultiGraph.degree() we provide the function. Otherwise you should convert to a standard graph in a way that makes the measurement well defined.

因此,如果进行了一项更改,该示例应该可以正常工作 - 按如下方式创建 G:

G = nx.MultiGraph()

关于python - 如何在 python 中的 networkx 中绘制具有重复边的图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16828493/

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