且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

查找深度2 Networkx的所有子图

更新时间:2023-11-21 15:21:40

正如我在评论中所说,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()