Distance (graph theory)
.svg.png)
iIn the mathematical field of graph theory, the distance between two vertices in a graph is the number of edges in a shortest path (also called a graph geodesic) connecting them. This is also known as the geodesic distance or shortest-path distance.[1] Notice that there may be more than one shortest path between two vertices.[2] If there is no path connecting the two vertices, i.e., if they belong to different connected components, then conventionally the distance is defined as infinite.
In the case of a directed graph the distance d(u,v) between two vertices u and v is defined as the length of a shortest directed path from u to v consisting of arcs, provided at least one such path exists.[3] Notice that, in contrast with the case of undirected graphs, d(u,v) does not necessarily coincide with d(v,u)—so it is just a quasi-metric, and it might be the case that one is defined while the other is not.
Computational Representation
In graph theory, distances between nodes can be represented computationally by using a distance matrix (also called the all-pairs shortest-path matrix). This is the square matrix , where each entry indicates the length of a shortest path between the two vertices and .[4]

Distance matrices have several applications, which include telecommunications and chemistry. In chemical graph theory, multiple topological indices that characterize molecular structure were able to be derived from the distance matrix.[5]
Beyond exact matrix representations, graph distances can also be represented using metric embeddings. This approach maps the vertices of a graph to points in a geometric space (such as Euclidean space) in a way that preserves the graph distances as closely as possible.[6]
Directed Graph Distance
In a directed graph, edges have an assigned direction, meaning travel between vertices is not necessarily bidirectional. As a result, the distance from to may differ from the distance from to . This makes directed graph distance a quasi-metric rather than a true metric.
Formally, for vertices and in a directed graph , it is not guaranteed that , and may be undefined if no directed path from to exists.[7]

Related concepts
A metric space defined over a set of points in terms of distances in a graph defined over the set is called a graph metric. The vertex set (of an undirected graph) and the distance function form a metric space, if and only if the graph is connected.
The eccentricity ϵ(v) of a vertex v is the greatest distance between v and any other vertex; in symbols,
It can be thought of as how far a node is from the node most distant from it in the graph.
The radius r of a graph is the minimum eccentricity of any vertex or, in symbols,
The diameter d of a graph is the maximum eccentricity of any vertex in the graph. That is, d is the greatest distance between any pair of vertices or, alternatively,
To find the diameter of a graph, first find the shortest path between each pair of vertices. The greatest length of any of these paths is the diameter of the graph.
A central vertex in a graph of radius r is one whose eccentricity is r—that is, a vertex whose distance from its furthest vertex is equal to the radius, equivalently, a vertex v such that ϵ(v) = r.
A peripheral vertex in a graph of diameter d is one whose eccentricity is d—that is, a vertex whose distance from its furthest vertex is equal to the diameter. Formally, v is peripheral if ϵ(v) = d.
A pseudo-peripheral vertex v has the property that, for any vertex u, if u is as far away from v as possible, then v is as far away from u as possible. Formally, a vertex v is pseudo-peripheral if, for each vertex u with d(u,v) = ϵ(v), it holds that ϵ(u) = ϵ(v).
A level structure of the graph, given a starting vertex, is a partition of the graph's vertices into subsets by their distances from the starting vertex.
A geodetic graph is one for which every pair of vertices has a unique shortest path connecting them. For example, all trees are geodetic.[8]
The weighted shortest-path distance generalises the geodesic distance to weighted graphs. In this case it is assumed that the weight of an edge represents its length or, for complex networks the cost of the interaction, and the weighted shortest-path distance dW(u, v) is the minimum sum of weights across all the paths connecting u and v. See the shortest path problem for more details and algorithms.
Algorithm for finding pseudo-peripheral vertices
Many sparse matrix and graph algorithms require a starting vertex of high eccentricity, defined as A peripheral vertex, whose eccentricity equals the graph's diameter, would be ideal but is typically expensive to locate since it requires all-pairs distance information. A pseudo-peripheral vertex is a vertex whose eccentricity is close to the diameter; it can be found cheaply and serves as an effective substitute in practice.
The standard heuristic is the George–Liu algorithm:[9]
- Choose any vertex .
- Among all vertices at maximum distance from , let be one of minimum degree.
- If , set and return to step 2; otherwise is a pseudo-peripheral vertex.
The same procedure in pseudocode, using breadth-first search (BFS) to compute eccentricities:
function PseudoPeripheral(G = (V, E)):
choose any u ∈ V
loop
BFS from u to obtain ε(u) and F = {w : d(u, w) = ε(u)}
let v ← vertex in F of minimum degree
if ε(v) > ε(u) then u ← v
else return u
Pseudo-peripheral vertices are most commonly used as starting points for bandwidth and profile reduction orderings of sparse matrices, particularly the reverse Cuthill–McKee method. Comparative evaluations of seven pseudoperipheral vertex finders have found that the George–Liu algorithm remains competitive with more recent alternatives on matrices with symmetric sparsity patterns.[10]
Distance in trees
A tree is a connected, acyclic graph in which there is exactly one simple path between any pair of vertices. This makes the distance d(u, v) uniquely determined, unlike in general graphs where multiple shortest paths may exist.[11]
For a rooted tree, the distance between two vertices can be computed using their lowest common ancestor (LCA):
where depth(v) is the number of edges from the root to v.
The diameter of a tree always occurs between two leaf nodes and can be found in O(n) time using two breadth-first searches. The center — the vertex minimizing eccentricity — is found by iteratively pruning leaf nodes until one or two vertices remain.[12]
Applications
Graph distance has practical applications across several domains of computer science and engineering, where the theoretical notion of shortest-path distance translates directly into real-world problem solving.
Navigation and GPS systems
Road networks can be modeled as weighted graphs, where intersections represent vertices and roads represent edges with weights corresponding to distance or travel time. Finding the shortest route between two locations - as performed by GPS applications such as Google Maps is a direct application of weighted graph distance, typically computed using Dijkstra's algorithm.[13]
Network routing protocols
In computer networking, protocols such as OSPF (Open Shortest Path First) model the internet as a weighted graph and use shortest-path distance to determine optimal routes for data packets between routers.[13]
Social network analysis
Graph distance is used to quantify the degrees of separation between individuals in a social network. The average shortest-path distance across all pairs of nodes reflects how tightly connected a network is - a property central to the study of small-world networks.[13]
| Application | Graph Model | Distance Concept Used |
|---|---|---|
| GPS/ Navigation | Weighted directed graph | Weighted shortest-path distance |
| Network routing (OSPF) | Weighted undirected graph | Shortest-path distance |
| Social network analysis | Unweighted undirected graph | Geodesic distance |
See also
- Resistance distance
- Betweenness centrality
- Centrality
- Closeness
- Degree diameter problem for graphs and digraphs
- Diameter (graph theory)
- Triameter (graph theory)
- Metric graph
Notes
- ^ Bouttier, Jérémie; Di Francesco, P.; Guitter, E. (July 2003). "Geodesic distance in planar graphs". Nuclear Physics B. 663 (3): 535–567. arXiv:cond-mat/0303272. Bibcode:2003NuPhB.663..535B. doi:10.1016/S0550-3213(03)00355-9. S2CID 119594729.
By distance we mean here geodesic distance along the graph, namely the length of any shortest path between say two given faces
- ^ Weisstein, Eric W. "Graph Geodesic". MathWorld--A Wolfram Web Resource. Wolfram Research. Archived from the original on 2008-04-23. Retrieved 2008-04-23.
The length of the graph geodesic between these points d(u,v) is called the graph distance between u and v
- ^ F. Harary, Graph Theory, Addison-Wesley, 1969, p.199.
- ^ Weisstein, Eric W. "Graph Distance Matrix". MathWorld. Wolfram Research. Retrieved 2026-04-20.
- ^ Dadedzi, Kenneth (2014). The Distance Matrix of a Graph (Masters thesis). Stellenbosch University.
- ^ Matoušek, Jiří (2002). "15: Embedding finite metric spaces into normed spaces". Lectures on Discrete Geometry. Springer. ISBN 978-0-387-95373-1.
- ^ Grinberg, Darij (2022). "An Introduction to Graph Theory" (PDF).
- ^ Øystein Ore, Theory of graphs [3rd ed., 1967], Colloquium Publications, American Mathematical Society, p. 104
- ^ George, Alan; Liu, Joseph W. H. (1979). "An implementation of a pseudoperipheral node finder". ACM Transactions on Mathematical Software. 5 (3): 284–295. doi:10.1145/355841.355845.
- ^ Gonzaga de Oliveira, Sanderson L.; Abreu, Alexandre A. A. M. (2018). "An evaluation of pseudoperipheral vertex finders for the Reverse Cuthill-McKee method for bandwidth and profile reductions of symmetric matrices". 2018 37th International Conference of the Chilean Computer Science Society (SCCC). IEEE. doi:10.1109/SCCC.2018.8705263.
- ^ Bondy, J. A.; Murty, U. S. R. (1976). Graph Theory with Applications. Macmillan.
- ^ Bondy, J. A.; Murty, U. S. R. (1976). Graph Theory with Applications. Macmillan.
- ^ a b c Easley, David; Kleinberg, Jon (2010). Networks, Crowds, and Markets. Cambridge University Press. https://www.cs.cornell.edu/home/kleinber/networks-book/