ego_graph(G, n, radius=1, center=True, undirected=False, distance=None)[source]#

Returns induced subgraph of neighbors centered at node n within a given radius.


A NetworkX Graph or DiGraph


A single node

radiusnumber, optional

Include all neighbors of distance<=radius from n.

centerbool, optional

If False, do not include center node in graph

undirectedbool, optional

If True use both in- and out-neighbors of directed graphs.

distancekey, optional

Use specified edge data key as distance. For example, setting distance=’weight’ will use the edge weight to measure the distance from the node n.


For directed graphs D this produces the “out” neighborhood or successors. If you want the neighborhood of predecessors first reverse the graph with D.reverse(). If you want both directions use the keyword argument undirected=True.

Node, edge, and graph attributes are copied to the returned subgraph.

Additional backends implement this function

cugraphGPU-accelerated backend.

Weighted ego_graph with negative cycles is not yet supported. NotImplementedError will be raised if there are negative distance edge weights.

Additional parameters:
dtypedtype or None, optional

The data type (np.float32, np.float64, or None) to use for the edge weights in the algorithm. If None, then dtype is determined by the edge values.

graphblas : OpenMP-enabled sparse linear algebra backend.