python - 在 networkx 和 python 中查找距离内的节点

标签 python networkx

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)

Original graph Graph centered in 0

关于python - 在 networkx 和 python 中查找距离内的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62843205/

相关文章:

python - Networkx图: finding if path exists between any node in a given set of nodes and another set of nodes

graph - 在networkx中绘制图形并设置边缘透明度

python - 检查python中networkx中是否存在具有相同标签的节点

python - 计算 pandas 列中 False 或 True 的出现次数

python - 从多个列中获取最近的观察和日期

python - 在 HTML 中显示 networkx 图

Python networkx 图形标签

python - Python 如何处理子包?

python - 在 Python 中从 Linux 上的子目录中查找顶级目录

python - Py - 使用输入打印数组的数据