Skip to content

Chapter 6. Walks, Trails, and Paths

Graphs describe connection. To use those connections, one must define what it means to move through a graph. The basic notions are walk, trail, and path. They are similar, but they impose different restrictions on repetition.

A walk may repeat vertices and edges. A trail may repeat vertices but not edges. A path repeats neither vertices nor edges. These distinctions become important in connectivity, Eulerian graphs, Hamiltonian graphs, shortest paths, and graph algorithms.

6.1 Movement in a Graph

Let

G=(V,E) G = (V,E)

be an undirected graph. A movement through GG begins at a vertex, follows an edge to another vertex, then may continue along another edge.

For example, if

{a,b},{b,c},{c,d}E, \{a,b\}, \{b,c\}, \{c,d\} \in E,

then one may move from aa to dd through the sequence

a,b,c,d. a,b,c,d.

This sequence records the vertices visited. Consecutive vertices must be adjacent.

The graph does not need geometry. Movement means adjacency, not physical distance.

6.2 Walks

A walk in a graph is a finite sequence of vertices

v0,v1,,vk v_0, v_1, \ldots, v_k

such that each consecutive pair is joined by an edge:

{vi1,vi}E(G) \{v_{i-1},v_i\} \in E(G)

for every

1ik. 1 \leq i \leq k.

The integer kk is the length of the walk. It counts edges used, not vertices listed.

A walk of length 00 consists of a single vertex:

v0. v_0.

Walks allow repetition. A walk may visit the same vertex several times. It may also use the same edge several times.

For example, if {a,b}\{a,b\} and {b,c}\{b,c\} are edges, then

a,b,c,b,a a,b,c,b,a

is a walk. It repeats bb, and it uses edges in both directions.

6.3 Endpoints of a Walk

In the walk

v0,v1,,vk, v_0, v_1, \ldots, v_k,

the vertex v0v_0 is the initial vertex, and vkv_k is the terminal vertex.

The walk is said to go from v0v_0 to vkv_k.

If v0=vkv_0 = v_k, then the walk is closed. If v0vkv_0 \ne v_k, then the walk is open.

For example,

a,b,c,a a,b,c,a

is closed if all required edges exist. The walk starts and ends at aa.

The walk

a,b,c,d a,b,c,d

is open if ada \ne d.

6.4 Trails

A trail is a walk in which no edge is repeated.

Vertices may repeat in a trail, but edges may not.

For example, suppose the edges

{a,b},{b,c},{c,a},{a,d} \{a,b\}, \{b,c\}, \{c,a\}, \{a,d\}

belong to a graph. Then

b,c,a,b b,c,a,b

is a closed trail, provided the edges {b,c}\{b,c\}, {c,a}\{c,a\}, and {a,b}\{a,b\} are distinct. It starts and ends at bb, and it uses no edge twice.

The sequence

a,b,c,b a,b,c,b

is not a trail if it uses the same edge {b,c}\{b,c\} twice.

Trails are central in Eulerian graph theory, where one asks whether a graph has a trail using every edge exactly once.

6.5 Paths

A path is a walk in which no vertex is repeated.

If

v0,v1,,vk v_0, v_1, \ldots, v_k

is a path, then the vertices

v0,v1,,vk v_0, v_1, \ldots, v_k

are all distinct.

Every path is a trail, because repeating an edge would force a repeated vertex. The converse need not hold. A trail may repeat a vertex without repeating an edge.

For example,

a,b,c,d a,b,c,d

is a path when all four vertices are distinct and consecutive vertices are adjacent.

The sequence

a,b,c,a,d a,b,c,a,d

is not a path because aa appears twice. It may still be a trail if no edge is repeated.

6.6 Comparing Walks, Trails, and Paths

The three notions form a hierarchy.

ObjectRepeated vertices allowedRepeated edges allowed
WalkYesYes
TrailYesNo
PathNoNo

Every path is a trail. Every trail is a walk. Thus

pathtrailwalk. \text{path} \subseteq \text{trail} \subseteq \text{walk}.

The restrictions become stronger as one moves from walk to trail to path.

A walk is the most flexible object. A path is the most restrictive.

6.7 Length

The length of a walk, trail, or path is the number of edges used.

If a vertex sequence has the form

v0,v1,,vk, v_0, v_1, \ldots, v_k,

then its length is

k. k.

It contains k+1k+1 vertex entries.

For example,

a,b,c,d a,b,c,d

has length 33, because it uses three edges:

{a,b},{b,c},{c,d}. \{a,b\}, \{b,c\}, \{c,d\}.

Length is important in distance, shortest paths, graph diameter, and algorithmic search.

6.8 Distance

If two vertices uu and vv are connected by at least one path, the distance from uu to vv is the length of a shortest path between them. It is written as

d(u,v). d(u,v).

Thus

d(u,v)=min{length(P):P is a path from u to v}. d(u,v)=\min\{\text{length}(P): P \text{ is a path from } u \text{ to } v\}.

If no path exists between uu and vv, then the distance is often left undefined or taken to be \infty, depending on convention.

In an unweighted graph, every edge has length 11. In a weighted graph, distance usually means minimum total weight, not minimum number of edges.

6.9 Geodesics

A shortest path between two vertices is sometimes called a geodesic.

If

d(u,v)=k, d(u,v)=k,

then any path from uu to vv of length kk is a geodesic.

There may be more than one geodesic between the same pair of vertices. For example, in a square graph, two opposite vertices have two different shortest paths of length 22.

Geodesics describe efficient movement through a graph. They also define metric properties such as eccentricity, radius, center, and diameter.

6.10 Closed Walks

A walk is closed if its first and last vertices are equal.

A closed walk has the form

v0,v1,,vk v_0, v_1, \ldots, v_k

with

v0=vk. v_0 = v_k.

For example,

a,b,c,a a,b,c,a

is a closed walk if the edges {a,b}\{a,b\}, {b,c}\{b,c\}, and {c,a}\{c,a\} exist.

Closed walks may repeat vertices and edges. They are useful in matrix methods because powers of the adjacency matrix count walks, including closed walks.

6.11 Circuits

A circuit is commonly used to mean a closed trail: a closed walk with no repeated edges.

Thus a circuit starts and ends at the same vertex and uses each edge in the sequence at most once.

For example,

a,b,c,d,a a,b,c,d,a

is a circuit if all four edges exist and are distinct.

Terminology varies. Some authors use circuit and cycle differently; others use them almost interchangeably. In this book, a circuit is a closed trail, while a cycle is a closed path with no repeated vertices except the first and last.

6.12 Cycles

A cycle is a closed path.

More explicitly, a cycle is a sequence

v0,v1,,vk,v0 v_0, v_1, \ldots, v_k, v_0

where

k2, k \geq 2,

the vertices

v0,v1,,vk v_0, v_1, \ldots, v_k

are distinct, and each consecutive pair is adjacent.

A cycle of length k+1k+1 has k+1k+1 edges.

For example,

a,b,c,a a,b,c,a

is a cycle of length 33. It is also called a triangle.

Cycles are fundamental because they measure circular structure. Trees are exactly the connected graphs with no cycles.

6.13 Path Graphs

A path graph is a graph whose vertices can be arranged in one line so that consecutive vertices are adjacent and no other edges appear.

The path graph on nn vertices is denoted by

Pn. P_n.

It has

n n

vertices and

n1 n-1

edges.

For example, P4P_4 has vertex sequence

v1,v2,v3,v4 v_1,v_2,v_3,v_4

and edge set

{{v1,v2},{v2,v3},{v3,v4}}. \{\{v_1,v_2\}, \{v_2,v_3\}, \{v_3,v_4\}\}.

A path graph is itself a graph. A path inside a larger graph is a subgraph that has this form.

6.14 Walks in Directed Graphs

In a directed graph, a walk must follow edge directions.

A directed walk is a sequence

v0,v1,,vk v_0, v_1, \ldots, v_k

such that

(vi1,vi)A(G) (v_{i-1},v_i) \in A(G)

for every

1ik. 1 \leq i \leq k.

The arc must go from the current vertex to the next vertex.

For example, if

(a,b),(b,c),(c,d) (a,b), (b,c), (c,d)

are arcs, then

a,b,c,d a,b,c,d

is a directed walk.

But if the graph contains (b,a)(b,a) instead of (a,b)(a,b), then one cannot move from aa to bb along that arc.

Direction makes reachability asymmetric.

6.15 Directed Paths and Directed Cycles

A directed path is a directed walk with no repeated vertices.

A directed cycle is a closed directed path.

For example,

a,b,c,a a,b,c,a

is a directed cycle if the arcs

(a,b),(b,c),(c,a) (a,b), (b,c), (c,a)

all exist.

Directed cycles are important in dependency graphs. If tasks are represented by vertices and an arc uvu \to v means that uu must precede vv, then a directed cycle represents a circular dependency.

A directed graph with no directed cycles is called a directed acyclic graph, or DAG.

6.16 Reachability

A vertex vv is reachable from a vertex uu if there exists a path from uu to vv.

In an undirected graph, reachability is symmetric. If vv is reachable from uu, then uu is reachable from vv.

In a directed graph, reachability may be asymmetric. There may be a directed path from uu to vv but no directed path from vv to uu.

Reachability is the basic relation behind connected components, strongly connected components, traversal algorithms, and transitive closure.

6.17 Removing Repetition from a Walk

Every walk from uu to vv contains a path from uu to vv.

This is a basic but important fact. If a walk repeats a vertex, the closed portion between the two appearances can be removed. Repeating this process eventually gives a walk with no repeated vertices, hence a path.

For example,

a,b,c,d,b,e a,b,c,d,b,e

contains the repeated vertex bb. Remove the closed portion

b,c,d,b. b,c,d,b.

The remaining sequence is

a,b,e, a,b,e,

provided the original walk included the edge {b,e}\{b,e\}. This is shorter and has the same endpoints.

This principle shows that for reachability questions, walks and paths are equivalent: if a walk exists, then a path exists.

6.18 Example

Let

V={a,b,c,d,e} V=\{a,b,c,d,e\}

and

E={{a,b},{b,c},{c,d},{d,b},{d,e}}. E=\{\{a,b\},\{b,c\},\{c,d\},\{d,b\},\{d,e\}\}.

The sequence

a,b,c,d,b,c a,b,c,d,b,c

is a walk. It repeats the edge {b,c}\{b,c\}, so it is not a trail.

The sequence

a,b,c,d,b a,b,c,d,b

is a trail. It uses the edges

{a,b},{b,c},{c,d},{d,b} \{a,b\},\{b,c\},\{c,d\},\{d,b\}

once each. It repeats the vertex bb, so it is not a path.

The sequence

a,b,c,d,e a,b,c,d,e

is a path. It has no repeated vertices.

The sequence

b,c,d,b b,c,d,b

is a cycle of length 33, because it returns to bb and repeats no other vertex.

6.19 Summary

A walk is a sequence of adjacent vertices. A trail is a walk with no repeated edges. A path is a walk with no repeated vertices. A closed walk starts and ends at the same vertex. A circuit is a closed trail. A cycle is a closed path.

These definitions describe movement in a graph. They are the basis for reachability, connectedness, distance, shortest paths, Eulerian trails, Hamiltonian paths, and many graph algorithms.