问题描述
我有一个有向图,在这里我想有效地找到一个节点的所有K阶邻居的列表.将第K阶邻居定义为从正好在 K
个跃点中的有关节点可以到达的所有节点.
I have a directed graph in which I want to efficiently find a list of all K-th order neighbors of a node. K-th order neighbors are defined as all nodes which can be reached from the node in question in exactly K
hops.
我查看了networkx
,唯一相关的功能是neighbors
.但是,这仅返回顺序1的邻居.对于更高的阶数,我们需要迭代确定完整的集合.我相信应该有一种更有效的方法来访问networkx
中的第K个邻居.
I looked at networkx
and the only function relevant was neighbors
. However, this just returns the order 1 neighbors. For higher order, we need to iterate to determine the full set. I believe there should be a more efficient way of accessing K-th order neighbors in networkx
.
有没有一种功能可以有效地返回第K阶邻居,而无需增量构建集合?
Is there a function which efficiently returns the K-th order neighbors, without incrementally building the set?
如果Python中存在其他可能在这里有用的图形库,请务必提及这些.
In case there exist other graph libraries in Python which might be useful here, please do mention those.
推荐答案
您可以使用:nx.single_source_shortest_path_length(G, node, cutoff=K)
其中G
是您的图形对象.
这篇关于图中的第K阶邻居-Python NetworkX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!