Sub topic 2
What a Connected Component Is
This chapter assumes you know basic graph vocabulary (vertices, edges, undirected graphs) and can read the adjacency lists built in the previous chapter. It defines the object that every later algorithm in this tutorial computes, so the goal here is a precise working model, not code that finds components.
Reachability
In an undirected graph, vertex v is reachable from vertex u if a path of edges leads from u to v. Every vertex counts as reachable from itself, through a path of length zero.
Reachability in an undirected graph has three properties:
| Property | Meaning | Why it holds |
|---|---|---|
| Reflexive | `u` reaches `u` | The empty path |
| Symmetric | if `u` reaches `v`, then `v` reaches `u` | Walk the same path backward |
| Transitive | if `u` reaches `v` and `v` reaches `w`, then `u` reaches `w` | Join the two paths |
A relation with all three properties is an equivalence relation, and every equivalence relation splits its set into non-overlapping groups. Those groups are the connected components.
The definition
A connected component is a maximal set of vertices in which every pair is reachable from each other. "Maximal" means you cannot add another vertex and keep the property. In other words, a component is everything reachable from one vertex.
This has three consequences:
- Every vertex belongs to exactly one component.
- No edge joins two different components. If one did, the two would be a single component.
- A vertex with no edges is a component by itself.
Here is the graph used throughout the tutorial: six vertices, numbered 0 to 5, with edges (0, 1), (1, 2) and (3, 4).
The components are {0, 1, 2}, {3, 4} and {5}. Vertices 0 and 2 have no edge between them, but they share a component because the path 0–1–2 connects them.
Two ways to represent the answer
Algorithms report components in one of two forms:
- A label array, where
label[v]is the index ofv's component. For the graph above:[0, 0, 0, 1, 1, 2]. - A list of vertex groups:
[[0, 1, 2], [3, 4], [5]].
Both forms hold the same information, and you can convert between them in linear time:
from collections import defaultdict
def group_by_label(label):
groups = defaultdict(list)
for v, c in enumerate(label):
groups[c].append(v)
return [groups[c] for c in sorted(groups)]
print(group_by_label([0, 0, 0, 1, 1, 2]))
# [[0, 1, 2], [3, 4], [5]]A label array answers "are u and v connected?" in O(1) time by comparing label[u] == label[v]. A group list is easier to iterate over when you want to process each component as a whole.
A useful counting fact
A graph with V vertices and k components has at least V − k edges. A connected group of n vertices needs at least n − 1 edges, and summing that over the k components gives V − k. The bound is exact when the graph has no cycles. It works as a quick sanity check: 6 vertices with 3 edges can never be fully connected.
Common errors
- Confusing "connected subgraph" with "component." The set
{0, 1}is connected, but it is not a component because adding vertex 2 keeps it connected. - Counting edges instead of vertices. Components partition the vertices. Edges follow from that, since every edge lies inside exactly one component.
- Applying this definition to directed graphs. With directed edges, reachability is no longer symmetric, so the clean split into groups breaks down. Later chapters handle that case with weakly and strongly connected components.
Exercise
A graph has vertices 0–7 and edges (0, 1), (2, 3), (3, 4), (4, 2), (6, 7). List its connected components and give the label array a scan from vertex 0 upward would produce.
Show answer
The components are {0, 1}, {2, 3, 4}, {5} and {6, 7}. If you number them in the order their smallest vertex appears, the labels are [0, 0, 1, 1, 1, 2, 3, 3].
Exercise
A graph has 10 vertices and 6 edges. What is the smallest number of connected components it can have, and why?
Show answer
4. A graph with V vertices and k components needs at least V − k edges, so 6 ≥ 10 − k, which gives k ≥ 4. You reach 4 when the edges form no cycles.