A connected component is a group of nodes all reachable from each other; connected_components() returns the total number of such components and an array labeling which component each node belongs to. For a directed graph, the connection parameter matters: 'weak', the default, treats edges as if they were undirected when checking reachability, while 'strong' requires nodes to be mutually reachable in both directions along actual directed edges to count as connected, a meaningfully stricter and different notion for directed graphs.
1Understanding csgraph.connected_components()
A connected component is a group of nodes all reachable from each other; connected_components() returns the total number of such components and an array labeling which component each node belongs to. For a directed graph, the connection parameter matters: 'weak', the default, treats edges as if they were undirected when checking reachability, while 'strong' requires nodes to be mutually reachable in both directions along actual directed edges to count as connected, a meaningfully stricter and different notion for directed graphs.
For a directed graph, double-check whether you want 'weak' or 'strong' connectivity — 'weak' treats the graph as if edges had no direction for the purpose of grouping, while 'strong' requires genuine mutual, bidirectional reachability, and the two can give very different groupings for the same graph.
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import connected_components
graph = csr_matrix([[0, 1, 0], [1, 0, 0], [0, 0, 0]])
n_components, labels = connected_components(graph, directed=False)
print(n_components, labels)2Practical Example
Here is a real-world application of csgraph.connected_components() showing how it is used in production SciPy code.
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import connected_components
graph = csr_matrix([[0, 1, 0], [0, 0, 1], [0, 0, 0]])
weak_n, _ = connected_components(graph, directed=True, connection="weak")
strong_n, _ = connected_components(graph, directed=True, connection="strong")
print(weak_n, strong_n)3Best Practices
Follow these guidelines when working with csgraph.connected_components():
1. Represent your graph as a sparse adjacency matrix, typically CSR, before passing it to connected_components(), rather than a custom graph data structure
2. Choose 'weak' vs 'strong' connection deliberately for directed graphs, since they answer meaningfully different questions
3. Use the returned per-node labels array to group nodes by component, for further per-component analysis
Tip: For a directed graph, double-check whether you want 'weak' or 'strong' connectivity — 'weak' treats the graph as if edges had no direction for the purpose of grouping, while 'strong' requires genuine mutual, bidirectional reachability, and the two can give very different groupings for the same graph.
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import connected_components
graph = csr_matrix([[0, 1, 0], [1, 0, 0], [0, 0, 0]])
n_components, labels = connected_components(graph, directed=False)
print(n_components, labels)