查找深度2 Networkx的所有子图
问题描述:
我在networkx中有一个巨大的图,我想从每个节点获取深度为2的所有子图.是否可以使用networkx中的内置函数来做到这一点?
I have a huge graph in networkx and I would like to get all the subgraphs of depth 2 from each node. Is there a nice way to do that using buildin function in networkx?
答
正如我在评论中所说,networkx.ego_graph
符合要求.您只需要确保将半径设置为2(默认值为1)即可:
As I said in the comment, networkx.ego_graph
fits the bill. You just need to make sure that you set the radius to 2 (default is 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()