查找深度2 Networkx的所有子图 [英] Finding all subgraphs of depth 2 Networkx

查看:1313
本文介绍了查找深度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()

这篇关于查找深度2 Networkx的所有子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆