core23 min studyLesson 212 in Mathematics

Walks, paths, and connected components

Turn reachability into an equivalence relation and use breadth-first layers to certify unweighted distances.

On this page

What you’ll understand

  • Distinguish walks, trails, and simple paths.
  • Explain why undirected reachability partitions the vertices.
  • Use breadth-first layers and adjacency powers for different counting questions.

Knowing that every vertex has neighbors does not tell us whether a destination can be reached. Reachability depends on successive compatible edges, and different questions impose different repetition rules. A delivery route may revisit a junction, a simple path may not, and a count of walks may deliberately include immediate returns. Keeping these objects separate prevents several common mistakes in network reasoning.

Repetition changes the object

A walk of length kk is a vertex sequence v0,v1,…,vkv_0,v_1,\ldots,v_k with consecutive vertices adjacent. A trail repeats no edge, while a simple path repeats no vertex. We allow a length-zero walk and path from a vertex to itself. Whenever a walk connects two distinct vertices, a simple path also does: if a vertex repeats, delete the closed portion between two occurrences. Each deletion shortens the walk, so the process terminates in a finite graph.

That deletion argument proves existence, not a formula for how many paths there are. It also explains why a shortest walk between distinct vertices cannot contain a repeated vertex: removing the intervening closed portion would shorten it. With nonnegative edge lengths, removing a closed portion does not increase length; with negative weights, shortest-walk questions may behave differently or have no finite minimum. Throughout the distance discussion here, every edge has unit length and the graph is finite, simple, and undirected.

d(u,v)=min⁡{k:there is a length-k walk from u to v}d(u,v)=\min\{k:\text{there is a length-}k\text{ walk from }u\text{ to }v\}
For reachable vertices, unweighted distance is the smallest number of edges; unreachable pairs have distance infinity.↔ Scroll wide equations horizontally.

Reachability creates components

Declare two vertices equivalent when a walk connects them. The length-zero walk gives reflexivity, reversing a walk gives symmetry, and concatenation gives transitivity. The equivalence classes are the connected components. Every edge stays within one component, and no vertex belongs to two components. This description also handles isolated vertices correctly: each isolated vertex is a component even though it participates in no positive-length walk.

Breadth-first search explores one component in distance layers. Start with a source at layer zero, then place its undiscovered neighbors at layer one, and continue from the current frontier. A vertex first discovered from layer jj receives layer j+1j+1. Inductively, every path of at most jj edges has already been explored by the end of layer jj, so first discovery gives the actual shortest distance. A predecessor edge for each discovered non-source vertex then supplies a concrete shortest-path witness.

L0={s},Lj+1=N(Lj)∖⋃i=0jLiL_0=\{s\},\qquad L_{j+1}=N(L_j)\setminus\bigcup_{i=0}^{j}L_i
Breadth-first layers add neighbors of the current frontier that have not appeared in earlier layers.↔ Scroll wide equations horizontally.

Counting walks is a different task

Let AA be the zero-one adjacency matrix in a fixed vertex order. Its entry AuvA_{uv} records a length-one walk, and the entry of A2A^2 sums over every possible intermediate vertex. Induction with matrix multiplication shows that (Ak)uv(A^k)_{uv} counts length-kk walks. This formula includes repeated vertices and edges; it does not count only simple paths. The identity matrix at power zero matches the convention that each vertex has exactly one empty walk to itself.

For a directed graph, following arrows defines a reachability relation that is generally not symmetric. One-way reachability therefore does not give the undirected component partition just proved. Mutual directed reachability does define equivalence classes, called strongly connected components. Weak connectivity instead discards arrow direction before finding components. An application should name which notion it needs: the ability to send a request does not automatically guarantee a directed route for its response.

Build distance layers

Consider vertices {s,a,b,c,d,z}\{s,a,b,c,d,z\} and edges sa,sb,ac,bc,cdsa,sb,ac,bc,cd. Find the distance from ss to dd, the components, and the number of length-two walks from ss to cc.

  1. Breadth-first search starts with {s}; its neighbors give layer one {a,b}. Both lead to c, which appears once in layer two. Vertex d appears in layer three.

  2. The predecessor choices s–a–c–d and s–b–c–d both certify a three-edge route. No earlier layer contains d, so no shorter route exists.

  3. The reached component is {s,a,b,c,d}; z forms its own isolated component. The two intermediate vertices a and b give exactly two length-two walks from s to c, agreeing with the corresponding entry of the squared adjacency matrix.

The result

The distance is three, there are two components, and the length-two walk count is two.

Explore the model

Interactive lab

Interactive lab · modular

Reachable sets under a fixed step

Pause and Reset, then set m = 6 and a = 2. Predict only {0,2,4} can be reached from zero; Step until the orbit returns. Reset and set both m = 6 and a = 1 to compare all six reachable residues. Interpreting the modular arrows as a directed graph gives a deterministic walk, not breadth-first search of a general graph.

Reachable sets under a fixed stepAddition modulo m starts at zero: rₙ = na mod m. Playback advances two additions per second. The orbit has period m/gcd(m,a), including period one for a = 0. This is not a cryptographic security test or a primality algorithm. Current readings: Residue r0: 0; Orbit period · m/gcd(m,a): 12; Distinct residues visited: 1 of 12. All parameters are adjustable with the labeled controls below.Repeated addition modulo 12: the current residue is 0, and the orbit period is 12.01234567891011+ 5mod 12An additive orbitn = 0rₙ = 0Period = 12Cycle starts at 0:0 → 5 → 10 → 3 →8 → 1 → 6 → 11 →4 → 9 → 2 → 7 → 0Exact residues · two additions per second
Additive orbitCurrent residue / next edge
Residue r0
0
Orbit period · m/gcd(m,a)
12
Distinct residues visited
1 of 12
t = 0 s

Addition modulo m starts at zero: rₙ = na mod m. Playback advances two additions per second. The orbit has period m/gcd(m,a), including period one for a = 0. This is not a cryptographic security test or a primality algorithm. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA locally valid move can remain confined to one reachable component; the visited set depends on the edge rule.

Check your understanding

For the adjacency matrix A of a finite simple graph, what does the diagonal entry of A² count?

Try it yourself

In the path graph with edges ab,bc,cdab,bc,cd, find d(a,d)d(a,d) and list every length-three walk from aa to bb. Explain why the walk count is not a simple-path count.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

The first move from a is forced. After arriving at b, consider its two possible next moves and which can finish at b.

Compare with the solution

The unique simple path from a to d is a–b–c–d, so the distance is three. The length-three walks from a to b are a–b–a–b and a–b–c–b. Both repeat vertex b, and the first also repeatedly uses edge ab. They therefore contribute two to the adjacency-cube entry but neither is a simple length-three path. The only simple path from a to b has length one.

Take it with you

  • A walk may repeat vertices; deleting closed portions yields a simple path.
  • Undirected reachability is an equivalence relation whose classes are components.
  • Adjacency powers count walks, whereas breadth-first search finds unit-edge distances.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.