networkx设置节点的大小_如何在Networkx中指定边长以计算最短距离?

在NetworkX中,为使最短路径计算考虑特定边的长度,需要在添加边时附带`length`属性。例如,将边(2, 3)设置长度为2,可以写成`G.add_edge(2, 3, length=2)`。当寻找从1到5的最短路径时,使用`weight='length'`参数,即`nx.shortest_path_length(G, 1, 5, weight='length')`,将返回正确的距离6。" 124844367,9803657,电话列表兼容性检查算法,"['算法', '测试用例', 'C++编程']
摘要由CSDN通过智能技术生成

I have a list of nodes and edges but I want some edges to be of length two instead of one. So when the distance between the nodes is calculated using the built in algorithm it returns

For example, if I have (1, 2), (2*, 3), (4*, 5) as edges between nodes, where the distance between the nodes with an asterisk have length two, the distance between (1, 2) should be 1, (2,3) should be 2 instead of 1 and then the distance between (1,5) should be 5 instead of 3.

When adding nodes I've tried G.add_edge(4,5,length=2) but nx.shortest_path_length(G,source=4,target=5)) still returns 1 instead of two. How can I specify edge length?

解决方案

You need to have a length attribute attached to your edges, and then specify that you want to weight by those lengths when finding the shortest path:

# Had to add an edge from 3 to 4 to your example edges

# or there&

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值