networkx中是否存在有效的函数/方法(因为图形较大)以识别源节点中的所有节点2。非常感谢!
答案 0 :(得分:2)
假设您的图表为G
且源节点为source
,那么您可以使用single_source_dijkstra_path_length获取路径长度,如下所示:
>>> source_path_lengths = networkx.single_source_dijkstra_path_length(G, source)
>>> for (v, l) in source_path_lengths.iteritems():
if l == 2:
print v
答案 1 :(得分:1)
好吧,你可以分配' cutoff'参数为' 2'从Single Source Shortest Path - Networkx的文档中可以看出。因此,基本上算法找到路径直到路径的长度< = cutoff。
print path.keys()
#output : [0, 1, 2, 3, 4]
然后,您可以将以下内容用于距离来源<= 2的顶点列表
print vertex_list
#Output : {0: 0, 1: 1, 2: 2}
或者你可以打印字典本身以准确获得源和顶点之间的距离,直到长度&lt; = cutoff
{ path: 'about', component: AboutComponent, data: {title: 'About Us'}},
{ path: 'resources', component: AboutComponent, data: {title: 'Amazing Resources'}},
{ path: 'newsroom', component: AboutComponent, data: {title: 'News'}},
您可以更改&#39; cutoff&#39;因此,根据您的需要。