[Python图论]在用图nx.shortest_path求解最短路径时,节点之间有多条边edge,会如何处理?

问: 在使用图求最短路径时,如果节点之间有多条路径,shortest_route = nx.shortest_path(G, source=start_node, target=end_node, weight='length')会如何处理,会自动选择最短那条吗?

python 复制代码
# 输出图G各节点之间有多少条边edge,并给出其长度
Edges between 103928 and 25508583:共2条
Edge: 103928 -> 25508583, Key: 0, Length: 77.277
Edge: 103928 -> 25508583, Key: 1, Length: 112.16900000000001
Edges between 26630792 and 5521427550:共2条
Edge: 26630792 -> 5521427550, Key: 0, Length: 85.091
Edge: 26630792 -> 5521427550, Key: 1, Length: 88.07600000000001
Edges between 107876 and 107873:共2条
Edge: 107876 -> 107873, Key: 0, Length: 83.102
Edge: 107876 -> 107873, Key: 1, Length: 186.669

答:是的,nx.shortest_path 会自动选择权重最小的那条路径。

当你在 NetworkX 中使用 nx.shortest_path(G, source=start_node, target=end_node, weight='length') 时,它会根据你指定的权重(在这个例子中是 'length')找到源节点和目标节点之间权重最小的路径。

对于节点之间有多条路径的情况,NetworkX 会如下处理:

  • 如果两个节点之间有多条路径,例如 (u, v),NetworkX 会选择所有可能的路径,并根据你指定的权重属性(在这个例子中是 length)找到最小权重的路径。
  • 在你的例子中,每一对节点之间都有两条路径。NetworkX 会比较这两条路径的 length 属性,并选择长度最短的那条路径作为该节点对之间的最短路径。
  • 这个过程会在整个图上进行,直到找到从起点到终点的全局最短路径。

具体示例

以你的数据为例:

  1. 节点 103928 -> 25508583:

    • 第一条路径长度:77.277
    • 第二条路径长度:112.169
    • NetworkX 选择:77.277 长度的路径(Key: 0)
  2. 节点 26630792 -> 5521427550:

    • 第一条路径长度:85.091
    • 第二条路径长度:88.076
    • NetworkX 选择:85.091 长度的路径(Key: 0)
  3. 节点 107876 -> 107873:

    • 第一条路径长度:83.102
    • 第二条路径长度:186.669
    • NetworkX 选择:83.102 长度的路径(Key: 0)
  4. 节点 11767210 -> 12026764:

    • 第一条路径长度:219.545
    • 第二条路径长度:95.666
    • NetworkX 选择:95.666 长度的路径(Key: 1)

总结

  • NetworkX 的 shortest_path 函数会自动为你选择每对节点之间权重最小的路径。
  • 因此,你不需要手动指定哪条路径是最短的,NetworkX 会基于权重(如 length)自动做出选择。

输出图内各节点之间的边及其长度 代码:

python 复制代码
# 输出图内各节点之间的边及其长度

import networkx as nx


# Get all the nodes in the graph
nodes = G.nodes()

# Iterate over all pairs of nodes
for u in nodes:
    for v in nodes:
        # Skip if u and v are the same node
        if u == v:
            continue

        # Get the edges between u and v
        edges = G.get_edge_data(u, v)
        
        # If there are no edges between u and v, skip to the next pair of nodes
        if edges is None:
            continue
        edges_count = len(edges.items())
        if edges_count >1:
          # Print the edges and their lengths
          print(f"Edges between {u} and {v}:共{edges_count}条")
          for key, data in edges.items():
              print(f"Edge: {u} -> {v}, Key: {key}, Length: {data['length']}")
相关推荐
test管家19 分钟前
PyTorch动态图编程与自定义网络层实战教程
python
laocooon52385788631 分钟前
python 收发信的功能。
开发语言·python
清水白石00837 分钟前
《Python 责任链模式实战指南:从设计思想到工程落地》
开发语言·python·责任链模式
沛沛老爹44 分钟前
Web开发者快速上手AI Agent:基于LangChain的提示词应用优化实战
人工智能·python·langchain·提示词·rag·web转型
宁大小白1 小时前
pythonstudy Day39
python·机器学习
拾贰_C1 小时前
【VSCode | python | anaconda | cmd | PowerShell】在没有进入conda环境时使用conda命令默认安装位置
vscode·python·conda
大千AI助手1 小时前
基于OpenAPI生成的 SDK 的工业级和消费级概念区别
人工智能·python·机器学习·openai·代码生成·openapi·大千ai助手
骚戴2 小时前
n1n:从替代LiteLLM Proxy自建网关到企业级统一架构的进阶之路
人工智能·python·大模型·llm·gateway·api
秋氘渔2 小时前
智演沙盘 —— 基于大模型的智能面试评估系统
python·mysql·django·drf
爱笑的眼睛112 小时前
超越AdamW:优化器算法的深度实现、演进与自定义框架设计
java·人工智能·python·ai