Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding all subgraphs of depth 2 Networkx

Tags:

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?

like image 336
user2925213 Avatar asked Oct 15 '25 07:10

user2925213


1 Answers

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()

enter image description here

like image 95
Paul Brodersen Avatar answered Oct 19 '25 12:10

Paul Brodersen