Search

Search papers and lessons

Average Path Length

Level 2 · 12 min read

The average path length of a network is the mean number of edges on the shortest route between every pair of vertices, a single number for how close the network's members are to each other.

On this page

  • Colour = topic mix
  • Algorithms
  • Discrete math
  • ReadReady to readNot yet

TL;DR

The average path length of a Graph is the mean number of edges on the shortest route between two vertices, averaged over every pair of vertices. It is one number that says how far apart the members of a network typically are. A small value means almost any two vertices can reach each other through only a few links, which is the core of the "small world" idea.

Builds on: Breadth-First Search · Leads to: Watts–Strogatz Small-World Model (a network model built to have short paths and many local triangles at once)

Where it's used

Social networks and the "six degrees" question. In 1967 Stanley Milgram asked people in the United States to pass a letter toward a stranger in another city, sending it only to someone they knew on first-name terms. The chains that arrived were typically about five or six people long. The average path length of the acquaintance network is the precise version of that claim: it asks how many links separate a typical pair of people.

Communication and power networks. Engineers who design a computer network or an electricity grid care how many hops a message or a unit of power must pass through between two points. Long typical routes mean more delay, more equipment in the path and more places for a failure to strike. The average path length summarises the whole layout in one figure, so two designs of the same size can be compared directly.

Biology and neuroscience. Researchers map networks of neurons, proteins and genes as graphs. They measure the average path length of the real network and compare it with the value for a random network of the same size and number of links. A real network that is about as short as the random one, but far more locally clustered, is a finding that shaped the small-world model.

Spread of disease or rumour. An infection or a piece of news travels along the links of a contact network. If typical paths are short, a message can reach most of the network in a few rounds. Average path length is a quick check on how fast this could happen, before any detailed simulation.

How it works

Take a graph with four vertices, labelled 1 to 4, joined in a line: 1 is joined to 2, 2 to 3, and 3 to 4. The distance between two vertices is the number of edges on the shortest route between them. Our job is to find the distance for every pair and average the results.

  1. List the pairs. There are six pairs: (1,2), (1,3), (1,4), (2,3), (2,4) and (3,4). Check: choosing 2 of 4 vertices gives 4 times 3 divided by 2, which is 6.
  2. Find each shortest distance. Two vertices joined by an edge are at distance 1: (1,2), (2,3), (3,4). The pair (1,3) is joined through vertex 2, so its distance is 2, and likewise (2,4) through vertex 3 gives 2. The pair (1,4) must go 1 to 2 to 3 to 4, so its distance is 3. Check: each distance is the length of a route you can actually walk, and no shorter route exists.
  3. Add the distances. The six values are 1, 2, 3, 1, 2, 1. Their sum is 10. Check: count the values again; there should be six of them.
  4. Divide by the number of pairs. The average is 10 divided by 6, which is 5/3, about 1.67. Check: the average must lie between the smallest distance (1) and the largest (3), and 1.67 does.
flowchart LR
  A["Graph with n vertices"] --> B["List every pair of vertices"]
  B --> C["Find each pair's shortest distance, by BFS"]
  C --> D["Add all the distances to get S"]
  D --> E["Divide S by the number of pairs"]

The algorithm in step 2 is the one from Breadth-First Search. Run it once from each vertex: the vertices found in layer 1 are at distance 1 from the start, those in layer 2 are at distance 2, and so on. Because BFS counts edges, not weights, this gives the right distances for an unweighted graph.

The math

Let the graph have nn vertices. For two vertices ii and jj, write d(i,j)d(i,j) for the shortest distance between them, counted in edges. Write PP for the number of unordered pairs of distinct vertices.

Definition: Average path length

For a connected graph with nn vertices,

L=1P∑i<jd(i,j),P=n(n−1)2L = \frac{1}{P} \sum_{i \lt j} d(i,j), \qquad P = \frac{n(n-1)}{2}

The sum runs over each unordered pair once. Read LL as "the mean distance over all pairs".

Step 1 in symbols: the number of pairs

Each of the nn vertices can pair with n−1n-1 others, which counts each pair twice, so the number of unordered pairs is

P=n(n−1)2P = \frac{n(n-1)}{2}

For the four-vertex line, P=4⋅3/2=6P = 4 \cdot 3 / 2 = 6.

Step 2 in symbols: the distances

Run Breadth-First Search once from each vertex ii. The layer at which vertex jj first appears is d(i,j)d(i,j). Running BFS from every vertex gives the whole table of distances, which is what the sum needs.

Steps 3 and 4 in symbols: sum and divide

The sum S=∑i<jd(i,j)S = \sum_{i \lt j} d(i,j) adds the six values in the walkthrough: S=1+2+3+1+2+1=10S = 1 + 2 + 3 + 1 + 2 + 1 = 10. Then

L=SP=106=53≈1.67L = \frac{S}{P} = \frac{10}{6} = \frac{5}{3} \approx 1.67

If you sum over ordered pairs instead, you get 2S2S and divide by n(n−1)n(n-1). The result is the same, because the factor of two cancels.

The cost

One BFS takes O(V+E)O(V + E) time in Big-O notation, where V=nV = n is the number of vertices and EE the number of edges. Running it from all nn vertices costs O(n(n+E))O(n(n + E)). For a large network this is expensive, so practical tools often estimate LL by sampling a few hundred start vertices.

Worked examples

Example
Example 1: a triangle (tiny)

Take three vertices A, B and C, with every pair joined.

  1. Pairs: (A,B), (A,C), (B,C). So P=3P = 3.
  2. Every pair is joined by an edge, so every distance is 1.
  3. The sum is 1+1+1=31 + 1 + 1 = 3.
  4. The average is 3/3=13 / 3 = 1.

Interpretation: when everyone is directly connected, every route has length 1, so the average path length is the smallest it can be.

Example
Example 2: a triangle with a tail (standard)

Take the triangle A, B, C from Example 1, and add a vertex D joined only to C.

  1. Pairs: there are 4⋅3/2=64 \cdot 3 / 2 = 6 of them.
  2. Distances: AB = 1, BC = 1, AC = 1 and CD = 1, because these pairs share an edge.
  3. AD must go A to C to D, so AD = 2. BD goes B to C to D, so BD = 2.
  4. Sum: 1+1+1+1+2+2=81 + 1 + 1 + 1 + 2 + 2 = 8.
  5. Average: 8/6=4/3≈1.338 / 6 = 4/3 \approx 1.33.

Interpretation: the tail vertex D adds two pairs at distance 2, which pulls the average above 1. Adding a vertex far from the rest raises the average.

Example
Example 3: a village in a ring (realistic)

Twenty people stand in a circle, numbered 0 to 19. Each knows only the two people beside them. A message passes from one person to the next around the ring.

  1. Start with person 0. Going round the shorter way, person kk is min⁡(k, 20−k)\min(k,\ 20-k) steps away. So persons 1 to 9 are at distances 1 to 9, person 10 is at distance 10, and persons 11 to 19 are at distances 9 down to 1.
  2. Add those 19 distances. Each of the values 1 to 9 appears twice, and 10 appears once. The total is 2(1+2+⋯+9)+10=2⋅45+10=1002(1 + 2 + \cdots + 9) + 10 = 2 \cdot 45 + 10 = 100.
  3. Every person sees the same pattern, because the ring looks the same from any starting point. So the sum over all unordered pairs is 20⋅100/2=100020 \cdot 100 / 2 = 1000.
  4. The number of pairs is 20⋅19/2=19020 \cdot 19 / 2 = 190.
  5. So L=1000/190=100/19≈5.26L = 1000 / 190 = 100/19 \approx 5.26.

Interpretation: in a ring, a typical route is about a quarter of the ring's size. Doubling the number of people roughly doubles the average path length. That linear growth is what the small-world idea is compared against.

Try it: a ring of 40 people, each knowing the two beside them. From any person the distances to the other 39 are 2(1+2+⋯+19)+202(1 + 2 + \cdots + 19) + 20 in total, using the same method. Work out LL for this ring.

Answer: the total from one person is 2⋅190+20=4002 \cdot 190 + 20 = 400. The sum over all pairs is 40⋅400/2=800040 \cdot 400 / 2 = 8000, and the number of pairs is 40⋅39/2=78040 \cdot 39 / 2 = 780. So L=8000/780=400/39≈10.26L = 8000 / 780 = 400/39 \approx 10.26.

Common mistakes and tradeoffs

It is tempting to think that the average path length is the same as the diameter. In fact the diameter is the largest shortest distance in the graph, while the average path length is the mean over all pairs. In the four-vertex line the diameter is 3 and the average is about 1.67. Report both when you describe a network, because a few far-apart pairs can raise the diameter while barely moving the average.

It is tempting to think that a short average means every pair is close. In fact the average hides outliers. A network can have a short average and still contain a pair of vertices that are far apart. The diameter, or a look at the distribution of distances, shows this.

It is tempting to think that a disconnected graph has a finite average path length. In fact a pair with no route between them has infinite distance, so the plain average is infinite. Two fixes are in use. You can average only over the pairs that are connected, or you can average the inverse distances 1/d(i,j)1/d(i,j), which treats an unreachable pair as contributing zero. The second is often called global efficiency.

It is tempting to think that BFS gives the right answer when the edges have different lengths. In fact BFS counts edges. With weighted edges, such as road lengths in kilometres, each distance must be computed with Dijkstra's Algorithm, and the average is then taken over the weighted distances.

When to use it: to compare networks of the same size, or to get a single number for how well connected a network is. When not to use it: for very small graphs, where one pair can dominate; for disconnected graphs without a fix; and when you need the worst case, where the diameter is the better summary. Alternatives: the diameter for the worst case, and global efficiency when the graph may be disconnected.

History

Measuring how many intermediaries link two people is older than the graph language used for it. In 1959 and 1960 Paul Erdős and Alfréd Rényi studied random graphs, in which edges appear at random. In such graphs the typical distance between two vertices is short, growing only slowly as the graph gets larger. This gave researchers a baseline.

In 1967 Milgram's letter experiment, and the report by Jeffrey Travers and Milgram in 1969, put the question of social distance on the table. The short chains made the question of how many links separate people a live one, and the random-graph baseline gave a way to test it.

In 1998 Duncan Watts and Steven Strogatz published a paper in Nature that measured average path length and clustering together in real networks. They showed that a small amount of random rewiring in a ring of people makes the average path length drop sharply while the local clustering stays high. That combination of short paths and local triangles became known as the small-world property.

Recap

  • A graph's average path length is the mean of the shortest-route distances over all pairs of vertices.
  • It is found by running Breadth-First Search from every vertex, summing the distances and dividing by the number of pairs, n(n−1)/2n(n-1)/2.
  • A ring of nn people has an average path length that grows in proportion to nn, so it is a useful contrast to random networks.
  • The average is sensitive to disconnected pairs and hides far outliers, so it is usually reported alongside the diameter.
  • Networks that combine short average paths with high local clustering are the subject of the Watts–Strogatz Small-World Model.

Next, read Watts–Strogatz Small-World Model to see how a network can keep short paths while staying locally clustered.

Check yourself

QuestionRecall: what is the difference between the diameter of a graph and its average path length?Show answer

The diameter is the largest shortest distance between any two vertices. The average path length is the mean of the shortest distances over all pairs. The diameter describes the worst pair; the average describes a typical pair.

QuestionCompute: a graph has 5 vertices and the shortest distances over all 10 pairs add up to 18. What is its average path length?Show answer

The number of pairs is 5⋅4/2=105 \cdot 4 / 2 = 10. The average is 18/10=1.818 / 10 = 1.8.

QuestionWhy: why can the plain average path length of a disconnected graph not be used directly, and what is one way around it?Show answer

A pair with no route between its vertices has no finite shortest distance, so the sum includes an infinite term and the average is infinite. One way around it is to average only over the connected pairs; another is to average the inverse distances, so that an unreachable pair counts as zero.

QuestionApplied: a delivery company models its road network as a graph where each road segment is one edge and every segment has the same cost. It wants a single number for how far apart its depots are on average, measured in segments. Is average path length with BFS a suitable tool, and what changes if the roads have different lengths in kilometres?Show answer

With equal edges, BFS gives the distance in segments and the average path length over the depot pairs is a suitable summary. If the roads have different lengths in kilometres, the distances must be computed with weighted shortest paths, such as Dijkstra's algorithm, and the average is then taken over kilometres.

Sources