Walks, paths, and connected components
Turn reachability into an equivalence relation and use breadth-first layers to certify unweighted distances.
On this page
The question for today
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 is a vertex sequence 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.
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 receives layer . Inductively, every path of at most edges has already been explored by the end of layer , so first discovery gives the actual shortest distance. A predecessor edge for each discovered non-source vertex then supplies a concrete shortest-path witness.
Counting walks is a different task
Let be the zero-one adjacency matrix in a fixed vertex order. Its entry records a length-one walk, and the entry of sums over every possible intermediate vertex. Induction with matrix multiplication shows that counts length- 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.
Work it through
Build distance layers
Consider vertices and edges . Find the distance from to , the components, and the number of length-two walks from to .
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.
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.
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 distance is three, there are two components, and the length-two walk count is two.
The living notebook
Explore the model
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.
- Residue r0
- 0
- Orbit period · m/gcd(m,a)
- 12
- Distinct residues visited
- 1 of 12
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.
Pause & reason
Check your understanding
Your turn
Try it yourself
In the path graph with edges , find and list every length-three walk from to . 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.
Keep the idea
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.