din*_*elk 5 python graph shortest-path vertices igraph
我可以使用 python-igraph 创建一个非常简单的图形...
import igraph
g=igraph.Graph.TupleList([("a", "b", 3.0), ("c", "d", 4.0), ("a", "c", 5.0)], weights=True)
print( igraph.summary(g, full=True) )
Run Code Online (Sandbox Code Playgroud)
看起来像这样:
IGRAPH UNW- 4 3 --
+ attr: name (v), weight (e)
+ edges (vertex names):
edge weight
[0] a--b 3
[1] c--d 4
[2] a--c 5
Run Code Online (Sandbox Code Playgroud)
找到最短路径很容易!
source = g.vs[0] // vertex "a"
target = g.vs[len(g.vs)-1] // vertex "d"
g.shortest_paths(source=source, target=target, weights='weight')
Run Code Online (Sandbox Code Playgroud)
打印结果:
[[9.0]]
Run Code Online (Sandbox Code Playgroud)
我很高兴最短路径的边权重为 9.0。但是,我想获得构成图形最短路径的实际顶点和边。得到一个看起来像这样的对象会很好:
shortestPath = [("a", "c", 5.0), ("c", "d", 4.0)]
Run Code Online (Sandbox Code Playgroud)
python-igraph 可以向我提供这个吗?我在没有运气的情况下搜索了文档......