all_neighbors#
- all_neighbors(graph, node)[source]#
Returns all of the neighbors of a node in the graph.
If the graph is directed returns predecessors as well as successors.
- Parameters:
- graphNetworkX graph
Graph to find neighbors.
- nodenode
The node whose neighbors will be returned.
- Returns:
- neighborsiterator
Iterator of neighbors
- Raises:
- NetworkXError
If
nodeis not in the graph.
See also
Graph.neighborsReturns successors for both Graph and DiGraph
DiGraph.predecessorsReturns predecessors for directed graphs only
DiGraph.successorsReturns successors for directed graphs only
Notes
This function iterates over all neighbors (both predecessors and successors).
Examples
For undirected graphs, this function is equivalent to
G.neighbors(node).>>> G = nx.path_graph(4) # or DiGraph, MultiGraph, MultiDiGraph, etc >>> list(nx.all_neighbors(G, 1)) [0, 2]
For directed graphs, this function returns both predecessors and successors, which may include duplicates if a node is both a predecessor and successor (e.g., in bidirectional edges or self-loops).
>>> DG = nx.DiGraph([(0, 1), (1, 2), (2, 1)]) >>> list(nx.all_neighbors(DG, 1)) [0, 2, 2]