networkx 中有没有一种方法可以找到距特定节点一定距离内的所有节点?就像我指定一个节点和一个距离并取回该距离内的所有节点。这是假设我为每条边都添加了权重。
或者,有没有办法从特定节点找到指定度数内的所有节点?比如,与特定节点相距 2 度以内的所有节点是什么?度的意思是,一个节点连接到一个节点,一个节点连接到该节点。感谢您的帮助!
最佳答案
可以使用networkx
库的ego_graph
函数:
node = 3 # The center node
radius = 3 # Degrees of separation
new_graph = nx.generators.ego_graph(graph, node, radius=radius)
例如:
import networkx as nx
G = nx.gnm_random_graph(n=n, m=30, seed=1)
G = nx.generators.ego_graph(G, 0, radius=2)
关于python - 在 networkx 和 python 中查找距离内的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62843205/