查找深度为 2 Networkx 的所有子图
Finding all subgraphs of depth 2 Networkx
我在 networkx 中有一个巨大的图,我想从每个节点获取深度为 2 的所有子图。在 networkx 中使用 buildin 函数是否有一个很好的方法来做到这一点?
正如我在评论中所说,networkx.ego_graph
符合要求。您只需要确保将半径设置为 2(默认值为 1):
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
# create some test graph
graph = nx.erdos_renyi_graph(1000, 0.005)
# create an ego-graph for some node
node = 0
ego_graph = nx.ego_graph(graph, node, radius=2)
# plot to check
nx.draw(ego_graph); plt.show()
我在 networkx 中有一个巨大的图,我想从每个节点获取深度为 2 的所有子图。在 networkx 中使用 buildin 函数是否有一个很好的方法来做到这一点?
正如我在评论中所说,networkx.ego_graph
符合要求。您只需要确保将半径设置为 2(默认值为 1):
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
# create some test graph
graph = nx.erdos_renyi_graph(1000, 0.005)
# create an ego-graph for some node
node = 0
ego_graph = nx.ego_graph(graph, node, radius=2)
# plot to check
nx.draw(ego_graph); plt.show()