How to Find a Vertex That Minimizes Total Distance in a Graph
At a glance
- 1. Define the objective
For each vertex, add its shortest-path distances to every other vertex.
- 2. Compute distances
Use breadth-first search from each vertex in an unweighted graph.
- 3. Compare totals
Choose a vertex with the smallest distance sum; ties give multiple minimizers.
- 4. Use tree rerooting
For a tree, compute one starting sum, then update sums across edges in linear time.
A common meaning of “minimizing sum of distances” in graph theory is to find a vertex whose total distance to all the other vertices is as small as possible. This vertex is called a graph median. The distances are shortest-path distances, not the lengths of arbitrary routes.
The exact answer depends on the graph: there may be one minimizing vertex or several. Here is how to define the objective and find its minimizer in an unweighted graph, with a faster method for trees.
Define the distance-sum objective
Let be a connected graph with vertices. Write for the length of a shortest path between vertices and , measured in number of edges. For each candidate vertex , calculate
Including does not change the sum because . A graph median is any vertex in
There can be ties. The minimum value is the smallest total distance; the vertices attaining it are the medians.
This is different from the graph center. A center minimizes the greatest distance to any vertex, while a median minimizes the sum of all distances. One favors a small worst-case distance; the other favors a small overall total.
Find a median in an unweighted graph
For each vertex, run breadth-first search (BFS). BFS computes shortest-path distances in an unweighted graph. Add the distances it returns, then compare the totals.
A straightforward procedure is:
- Choose a vertex .
- Run BFS starting at and record the distance to every vertex.
- Add those distances to obtain .
- Repeat for each vertex and select the smallest sum.
One BFS takes time, where is the number of edges. Repeating it from all vertices takes time. This works for general connected, unweighted graphs. If edges have nonnegative weights, use Dijkstra’s algorithm from each vertex instead; the relevant distance is then the minimum total edge weight along a path.
Worked example: a tree with a branch
Consider the tree with edges
It is a path from 1 to 5, with an extra leaf 6 attached to 3. Adding the distances from each vertex gives:
| Vertex | Distances to the other five vertices | |
|---|---|---|
| 1 | 13 | |
| 2 | 9 | |
| 3 | 7 | |
| 4 | 9 | |
| 5 | 13 | |
| 6 | 11 |
Vertex 3 is the unique median, because its distance sum, 7, is the smallest. Notice that vertex 3 is also the tree’s center here, but that need not be true for every graph.
A faster method for trees
For a tree, you can calculate all distance sums without running BFS from every vertex. Root the tree anywhere and first compute for the root . Then move from a parent to a child . Let be the number of vertices in the subtree rooted at , and let be the total number of vertices. The update is
Why? Moving from to shortens the distance by one for each of the vertices in ’s subtree, and lengthens it by one for each of the other vertices. The net change is .
In the example, root the tree at 3. Its distance sum is . The subtree containing vertex 2 and vertex 1 has size 2, so
The subtree rooted at 6 has size 1, giving . Applying the same update throughout the tree produces every total. A traversal computes subtree sizes and distance sums in time.
Common mistakes
- Using the longest distance instead of the sum. That finds a center, not a median.
- Counting routes instead of shortest paths. The distance between two vertices is the length of a shortest path.
- Assuming the minimizer is always unique. Symmetry can produce multiple medians.
- Ignoring disconnectedness. If the graph is disconnected, some distances are infinite or undefined, so the ordinary finite-sum problem needs a convention. Usually the problem is restricted to connected graphs or handled separately by component.
- Confusing graph medians with median graphs. A median graph is a separate graph class defined by a property of shortest paths; it is not simply any graph with a distance-sum minimizer.
References
- Introduction to Graph Theory, Douglas B. West
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.