node_connected_component#

node_connected_component(G, n)[source]#

Returns the set of nodes in the component of graph containing node n.

A connected component is a set of nodes that induces a subgraph of graph G that is connected and not part of any larger connected subgraph.

A graph is connected (is_connected()) if, for every pair of distinct nodes, there is a path between them. If there is a pair of nodes for which such path does not exist, the graph is not connected (also referred to as “disconnected”).

A graph consisting of a single node and no edges is connected. Connectivity is undefined for the null graph (graph with no nodes).

Parameters:
GNetworkX Graph

An undirected graph.

nnode label

A node in G

Returns:
compset

A set of nodes in the component of G containing node n.

Raises:
NetworkXNotImplemented

If G is directed.

Notes

This function is for undirected graphs only.

The algorithm is based on a Breadth-First Search (BFS) traversal and its time complexity is \(O(n + m)\), where \(n\) is the number of nodes and \(m\) the number of edges in the graph.

Examples

>>> G = nx.Graph([(0, 1), (1, 2), (5, 6), (3, 4)])
>>> nx.node_connected_component(G, 0)  # nodes of component that contains node 0
{0, 1, 2}
----

Additional backends implement this function

cugraph : GPU-accelerated backend.

graphblas : OpenMP-enabled sparse linear algebra backend.