Ma · Mathematics

How to Find a Vertex That Minimizes Total Distance in a Graph

Published By the 9to5Science editorial team

At a glance

  1. 1. Define the objective

    For each vertex, add its shortest-path distances to every other vertex.

  2. 2. Compute distances

    Use breadth-first search from each vertex in an unweighted graph.

  3. 3. Compare totals

    Choose a vertex with the smallest distance sum; ties give multiple minimizers.

  4. 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 G=(V,E)G=(V,E) be a connected graph with nn vertices. Write d(v,u)d(v,u) for the length of a shortest path between vertices vv and uu, measured in number of edges. For each candidate vertex vv, calculate

S(v)=∑u∈Vd(v,u).S(v)=\sum_{u\in V} d(v,u).

Including u=vu=v does not change the sum because d(v,v)=0d(v,v)=0. A graph median is any vertex in

arg min⁡v∈VS(v).\operatorname*{arg\,min}_{v\in V} S(v).

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:

  1. Choose a vertex vv.
  2. Run BFS starting at vv and record the distance to every vertex.
  3. Add those distances to obtain S(v)S(v).
  4. Repeat for each vertex and select the smallest sum.

One BFS takes O(n+m)O(n+m) time, where mm is the number of edges. Repeating it from all nn vertices takes O(n(n+m))O(n(n+m)) 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

1 ⁣−2,2 ⁣−3,3 ⁣−4,4 ⁣−5,3 ⁣−6.1\!-2,\quad 2\!-3,\quad 3\!-4,\quad 4\!-5,\quad 3\!-6.

It is a path from 1 to 5, with an extra leaf 6 attached to 3. Adding the distances from each vertex gives:

Vertex vvDistances to the other five verticesS(v)S(v)
11,2,3,4,31,2,3,4,313
21,1,2,3,21,1,2,3,29
32,1,1,2,12,1,1,2,17
43,2,1,1,23,2,1,1,29
54,3,2,1,34,3,2,1,313
63,2,1,2,33,2,1,2,311

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 S(r)S(r) for the root rr. Then move from a parent pp to a child cc. Let ss be the number of vertices in the subtree rooted at cc, and let nn be the total number of vertices. The update is

S(c)=S(p)+n−2s.S(c)=S(p)+n-2s.

Why? Moving from pp to cc shortens the distance by one for each of the ss vertices in cc’s subtree, and lengthens it by one for each of the other n−sn-s vertices. The net change is (n−s)−s=n−2s(n-s)-s=n-2s.

In the example, root the tree at 3. Its distance sum is S(3)=7S(3)=7. The subtree containing vertex 2 and vertex 1 has size 2, so

S(2)=7+6−2(2)=9.S(2)=7+6-2(2)=9.

The subtree rooted at 6 has size 1, giving S(6)=7+6−2(1)=11S(6)=7+6-2(1)=11. Applying the same update throughout the tree produces every total. A traversal computes subtree sizes and distance sums in O(n)O(n) 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

  1. Introduction to Graph Theory, Douglas B. West

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.