- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我有一个非常简单的有向多重图
G = nx.MultiDiGraph()
G.add_edge('A', 'B', key=1)
G.add_edge('B', 'C', key=2)
G.add_edge('B', 'C', key=3)
--
(A) -1- (B) -2- (C)
\3/
我希望 nx.all_shortest_paths(G, source='A', target='C')
返回两条路;
A-1-B-2-C
A-1-B-3-C
但是(因为它目前已经实现)all_shortest_paths
只返回节点,而不是节点和边,所以我们只能得到一条路径;
>>> list(nx.all_shortest_paths(G, source='A', target='C'))
[['A', 'B', 'C']]
是否有任何简单/通用的方法来返回实际路径,而不是简单的节点列表?
最佳答案
networkx
没有内置函数来处理它,因此您必须手动完成所有操作。
nx.all_simple_paths()
返回节点列表,因此对于 MultiDiGraph 会有很多重复。因此,首先我们通过将 nx.all_simple_paths()
输出转换为 set
来移除它们,然后对其进行迭代。对于每条路径,我们提取节点对(例如:[1,2,3,4] -> [[1,2],[2,3],[3,4]]
)和对于每一对,我们得到它们之间所有边的 AtlasView
。这是该算法的代码:
import networkx as nx
from pprint import pprint
# Create the graph with unique edges to check the algorithm correctness
G = nx.MultiDiGraph()
G.add_edges_from([
[1,2],
[1,2],
[1,2],
[2,3],
[2,3],
[2,3],
[3,4],
[3,4],
[2,4]
])
G.add_edge(1,2,data='WAKA')
G.add_edge(2,3,data='WAKKA')
G.add_edge(2,4,data='WAKA-WAKA')
# Our source and destination nodes
source = 1
destination = 4
# All unique single paths, like in nx.DiGraph
unique_single_paths = set(
tuple(path) # Sets can't be used with lists because they are not hashable
for path in nx.all_simple_paths(G, source, destination)
)
combined_single_paths = []
for path in unique_single_paths:
# Get all node pairs in path:
# [1,2,3,4] -> [[1,2],[2,3],[3,4]]
pairs = [path[i: i + 2] for i in range(len(path)-1)]
# Construct the combined list for path
combined_single_paths.append([
(pair, G[pair[0]][pair[1]]) # Pair and all node between these nodes
for pair in pairs
])
pprint(combined_single_paths)
[[((1, 2), AtlasView({0: {}, 1: {}, 2: {}, 3: {'data': 'WAKA'}})),
((2, 3), AtlasView({0: {}, 1: {}, 2: {}, 3: {'data': 'WAKKA'}})),
((3, 4), AtlasView({0: {}, 1: {}}))],
[((1, 2), AtlasView({0: {}, 1: {}, 2: {}, 3: {'data': 'WAKA'}})),
((2, 4), AtlasView({0: {}, 1: {'data': 'WAKA-WAKA'}}))]]
关于python - NetworkX MultiDiGraph 路径不区分平行边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56042626/
对于 networkx 中的 multiDiGraph,其边由列表边中的元组表示,请问我如何访问或打印出属性字典中的属性元素,例如我怎样才能打印出 multiDiGraph 的长度或类型或 chann
我有一个来自 networkx 包的 MultiDiGraph 类型的有向图 G。 下面两次通话的号码不同。它们不应该是一样的吗? G.number_of_edges() networkx.conve
如果我有一个非常简单的有向多重图 G = nx.MultiDiGraph() G.add_edge('A', 'B', key=1) G.add_edge('B', 'C', key=2) G.add
我有一个多图,其中的边没有权重。我想增加一些权重 G=MultiDiGraph(): …… for u, v, data in G.edges_iter(data=True): G.ed
我有一个包含自环的 NetworkX MultiDiGraph。根据documentation ,这是 MultiDiGraph 的有效属性。 A MultiDiGraph holds directe
我在 networkx MultiDigraph 中有多个等长且彼此平行的线图,我如何遍历这些图并将边添加到具有边的相对节点(注意,节点在嵌套列表中)即连接 2 & 2a、3 和 3a 等 nodes
我对NetworkX的阅读documentation建议这应该有效,但似乎不起作用? 考虑: import networkx as nx g = nx.MultiDiGraph() g.add_nod
MyNetwork 是 networkx.MultiDiGraph 的实例。我想知道是否保证多次运行以下代码会产生完全相同的列表,其中包含 MyNetwork 中的所有边(以及边属性): AllEdg
有没有一种快速方法可以将networkx MultiDiGraph对象转换为DiGraph?我正在创建一个 scale_free_graph使用此代码: import networkx as nx G
我刚刚开始学习 NetworkX 并尝试学习如何将它与 Shapefile 一起使用。 现在我有一个带有道路网络的 .shp 文件,我想用 NetworkX 在图表中表示它,这样我就可以找到 2 个
我正在使用 OSMnx 返回路线图,这些路线图在 networkx 中表示为 MultiDiGraph。 import osmnx as ox %matplotlib inline sg = ox.g
我正在使用 Python 的流行网络库 networkx。从以下代码中,我希望打印的语句是等效的。 import networkx as nx graph = nx.Graph() mgraph =
是否可以使用 connectionstyle 在具有不同曲率的相同节点处以某种方式绘制不同的边? ? 我写了下面的代码,但我得到了所有三个边重叠: import networkx as nx impo
如何在循环中遍历 G(networkx multidigraph)以导出为 OSM 或导出到某个数据库中?或者是否有任何内置函数可以将其导出为 json ? 最佳答案 您可以使用 OSMnx 的 sa
在我的多向图中,我想找到 2 个节点之间所有可能的(简单)路径。我设法获得了所有路径,但无法区分源节点到达目标节点所采用的边(假定它是多有向图)。 例如,我有 A->B->C,其中 (A,B) 和 (
我正在编写一个 QGIS 插件,它将使用 NetworkX 库来操作和分析流网络。我的数据来自代表流网络的形状文件。 (箭头代表水流方向) 在此流网络内有 braids这是我需要保留的重要特征。我将编
我是一名优秀的程序员,十分优秀!