🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

csgraph.connected_components()

AI & DATA SCIENCE // csgraph-connected-components

scipy.sparse.csgraph.connected_components() finds and labels the connected components of a graph represented as a sparse adjacency matrix.

Syntax

scipy.sparse.csgraph.connected_components(csgraph, directed=True, connection='weak')

Deep Dive Course

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.

editor.html
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)
localhost:3000

2Practical Example

Here is a real-world application of csgraph.connected_components() showing how it is used in production SciPy code.

editor.html
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)
localhost:3000

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.

editor.html
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)
localhost:3000

Examples

Example 01Basic Usage
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)
Example 02Advanced Example
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)

Best Practices

  • Represent your graph as a sparse adjacency matrix, typically CSR, before passing it to connected_components(), rather than a custom graph data structure
  • Choose 'weak' vs 'strong' connection deliberately for directed graphs, since they answer meaningfully different questions
  • Use the returned per-node labels array to group nodes by component, for further per-component analysis

Interview Question

For a directed graph like A to B to C, with no edges back, why does 'weak' connectivity report 1 component while 'strong' connectivity reports 3?

Hint: Think about what 'strong' connectivity actually requires between every pair of nodes in the same component.

Weak connectivity ignores edge direction entirely when checking reachability, so it just asks whether the nodes form one connected group if you could travel along edges in either direction — since A, B, and C are all linked in a chain, that's a single weakly-connected component. Strong connectivity instead requires that every pair of nodes in the same component be mutually reachable from each other following only the actual directed edges — you can go from A to C, but there's no path back from C to A or B, so no two of these nodes are actually mutually reachable, meaning each node ends up in its own separate strongly-connected component.

Exercises

MediumPractice using csgraph.connected_components() in a real scenario.
View Solution
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)

Frequently Asked Questions

For a directed graph like A to B to C, with no edges back, why does 'weak' connectivity report 1 component while 'strong' connectivity reports 3?

Weak connectivity ignores edge direction entirely when checking reachability, so it just asks whether the nodes form one connected group if you could travel along edges in either direction — since A, B, and C are all linked in a chain, that's a single weakly-connected component. Strong connectivity instead requires that every pair of nodes in the same component be mutually reachable from each other following only the actual directed edges — you can go from A to C, but there's no path back from C to A or B, so no two of these nodes are actually mutually reachable, meaning each node ends up in its own separate strongly-connected component.

Related Functions

csgraph-dijkstrasparse-csr-matrixcsgraph-depth-first-order