Networkx-最短路径长度 [英] Networkx - Shortest path length
问题描述
我正在使用networkx
来管理由5万个节点组成的大型网络图.
I'm using networkx
to manage large network graph which consists of 50k nodes.
我想计算一组特定节点之间的最短路径长度,例如N.
为此,我正在使用nx.shortest_path_length
函数.
I want to calculate the shortest path length between a specific set of nodes, say N.
For that i'm using the nx.shortest_path_length
function.
在N处的某些节点中可能没有路径,因此networkx正在引发并停止我的程序.
In some of the nodes from N there might not be a path so networkx is raising and stopping my program.
是否有任何方法可以运行此程序而不会出现任何错误?
并告诉shortest_path_length
返回一些最大值?
Is there any way to run this program without any error?
And to tell to shortest_path_length
to return some maximum value?
代码仅在循环中使用nx.shortest_path_length(G,i,j)
.
错误如下
Code simply uses nx.shortest_path_length(G,i,j)
in loop.
and error is as follows
raise nx.NetworkXNoPath("No path between %s and %s." % (source, target))
networkx.exception.NetworkXNoPath: No path between V and J
raise nx.NetworkXNoPath("No path between %s and %s." % (source, target))
networkx.exception.NetworkXNoPath: No path between V and J
推荐答案
import networkx as nx
G=nx.Graph()
G.add_nodes_from([1,2,3,4])
G.add_edge(1,2)
G.add_edge(3,4)
try:
n=nx.shortest_path_length(G,1,4)
print n
except nx.NetworkXNoPath:
print 'No path'
这篇关于Networkx-最短路径长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!