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
be an undirected graph. A movement through begins at a vertex, follows an edge to another vertex, then may continue along another edge.
For example, if
then one may move from to through the sequence
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
such that each consecutive pair is joined by an edge:
for every
The integer is the length of the walk. It counts edges used, not vertices listed.
A walk of length consists of a single vertex:
Walks allow repetition. A walk may visit the same vertex several times. It may also use the same edge several times.
For example, if and are edges, then
is a walk. It repeats , and it uses edges in both directions.
6.3 Endpoints of a Walk
In the walk
the vertex is the initial vertex, and is the terminal vertex.
The walk is said to go from to .
If , then the walk is closed. If , then the walk is open.
For example,
is closed if all required edges exist. The walk starts and ends at .
The walk
is open if .
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
belong to a graph. Then
is a closed trail, provided the edges , , and are distinct. It starts and ends at , and it uses no edge twice.
The sequence
is not a trail if it uses the same edge 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
is a path, then the vertices
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,
is a path when all four vertices are distinct and consecutive vertices are adjacent.
The sequence
is not a path because 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.
| Object | Repeated vertices allowed | Repeated edges allowed |
|---|---|---|
| Walk | Yes | Yes |
| Trail | Yes | No |
| Path | No | No |
Every path is a trail. Every trail is a walk. Thus
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
then its length is
It contains vertex entries.
For example,
has length , because it uses three edges:
Length is important in distance, shortest paths, graph diameter, and algorithmic search.
6.8 Distance
If two vertices and are connected by at least one path, the distance from to is the length of a shortest path between them. It is written as
Thus
If no path exists between and , then the distance is often left undefined or taken to be , depending on convention.
In an unweighted graph, every edge has length . 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
then any path from to of length 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 .
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
with
For example,
is a closed walk if the edges , , and 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,
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
where
the vertices
are distinct, and each consecutive pair is adjacent.
A cycle of length has edges.
For example,
is a cycle of length . 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 vertices is denoted by
It has
vertices and
edges.
For example, has vertex sequence
and edge set
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
such that
for every
The arc must go from the current vertex to the next vertex.
For example, if
are arcs, then
is a directed walk.
But if the graph contains instead of , then one cannot move from to 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,
is a directed cycle if the arcs
all exist.
Directed cycles are important in dependency graphs. If tasks are represented by vertices and an arc means that must precede , 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 is reachable from a vertex if there exists a path from to .
In an undirected graph, reachability is symmetric. If is reachable from , then is reachable from .
In a directed graph, reachability may be asymmetric. There may be a directed path from to but no directed path from to .
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 to contains a path from to .
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,
contains the repeated vertex . Remove the closed portion
The remaining sequence is
provided the original walk included the edge . 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
and
The sequence
is a walk. It repeats the edge , so it is not a trail.
The sequence
is a trail. It uses the edges
once each. It repeats the vertex , so it is not a path.
The sequence
is a path. It has no repeated vertices.
The sequence
is a cycle of length , because it returns to 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.