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?
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With