Short answer: Brandes’ algorithm computes betweenness centrality. For every source it builds a shortest-path DAG, counts the number of shortest paths σ through each vertex, and walks that DAG backward to add dependencies. One source costs a BFS, or a Dijkstra run when weights are nonnegative. All sources cost O(VE) on an unweighted graph and O(VE + V² log V) with a binary heap on nonnegative weights. The centrality of v is the sum, over pairs, of the fraction of shortest paths between the pair that pass through v.
What the problem actually asks
The definition sums, over all ordered pairs s, t different from v, the fraction σ_st(v) / σ_st. Computing each pair separately is cubic or worse and recomputes the same shortest-path DAGs. Brandes’ observation is that the fractions at a source can be accumulated from the leaves of the shortest-path DAG inward, in linear time once the DAG and the path counts exist.
The dependency δ_s(v) is the sum, over all targets, of the fraction of shortest s-paths that go through v. It satisfies a one-line recurrence along the DAG: a vertex sends its own dependency, plus one, down each incoming DAG edge in proportion to the path counts.
What the algorithm guarantees
After every source has been processed, the accumulated dependencies equal the betweenness scores, up to the convention of counting unordered pairs once, which is a factor of two on undirected graphs and must be stated. Unweighted time is O(VE) because each of V breadth-first searches is O(V+E) and the accumulation is linear in the DAG. Nonnegative weights replace each BFS with Dijkstra. Extra memory is one distance array, one path-count array, and a stack of vertices in order of discovery, reused per source.
Negative weights are outside the algorithm, because the inner search would no longer be Dijkstra or BFS. Brandes does not approximate. Sampling random sources is a later estimator and is not this procedure. Endpoints are usually excluded from a path’s credit. Including them is a different score.
The working steps
- Set centrality[v] = 0 for every vertex.
- For each source s, run BFS or Dijkstra. Let σ[s] = 1 and σ[v] = 0 elsewhere. Along a relaxation that finds an equal shortest distance, add the parent’s σ into the child’s σ. Along a strictly better distance, reset σ[child] to σ[parent].
- Push vertices onto a stack in the order they are closed, so the stack pops from the farthest vertices back toward s.
- Set δ[v] = 0. Pop v. For each predecessor u on the shortest-path DAG, add δ[u] += (σ[u]/σ[v]) · (1+δ[v]). Then, if v is not s, add δ[v] to centrality[v].
- On an undirected graph, halve the final scores if the definition counts unordered pairs.
The accumulation assumes the inner search enumerated every shortest path’s predecessor edges. A single parent pointer, as in a shortest-path tree, drops alternate shortest paths and undercounts σ. The algorithm is exact betweenness, not closeness centrality and not PageRank.
Approaches that look simpler and fail
| Approach | Why it fails when only one shortest path is counted |
|---|---|
| Keep a single parent pointer | A vertex with two shortest paths from the source contributes a fraction on each. One parent treats the fraction as 1 or 0. |
| Accumulate in arbitrary order | The recurrence needs the child’s δ to be finished before the parent is updated. The stack of closing order provides that. |
| Run it with negative weights under Dijkstra | The shortest-path DAG is wrong as soon as Dijkstra is wrong. There is no betweenness number to accumulate. |
| Quote O(VE) for a weighted heap implementation | Each source pays a Dijkstra bound. The unweighted O(VE) figure is the BFS version. |
A single-source shortest-path tree is Dijkstra or BFS. PageRank is a different centrality, based on a damped walk rather than on fractions of shortest paths. Sampling sources estimates Brandes and does not replace the exact sum.
A working example
This Python fragment runs one search per source, keeps every predecessor of equal distance, and accumulates dependency from the far end of a stack. On the undirected path 0—1—2 the halved score of the middle vertex is 1.
from collections import deque
adj = {0: [1], 1: [0, 2], 2: [1]}
centrality = {v: 0.0 for v in adj}
for source in adj:
stack, pred = [], {v: [] for v in adj}
sigma = {v: 0.0 for v in adj}
dist = {v: -1 for v in adj}
sigma[source] = 1
dist[source] = 0
queue = deque([source])
while queue:
u = queue.popleft()
stack.append(u)
for v in adj[u]:
if dist[v] < 0:
dist[v] = dist[u] + 1
queue.append(v)
if dist[v] == dist[u] + 1:
sigma[v] += sigma[u]
pred[v].append(u)
delta = {v: 0.0 for v in adj}
while stack:
v = stack.pop()
for u in pred[v]:
delta[u] += (sigma[u] / sigma[v]) * (1 + delta[v])
if v != source:
centrality[v] += delta[v]
for v in centrality:
centrality[v] /= 2
assert centrality[1] == 1 and centrality[0] == 0
print(int(centrality[1]))
Where it is used
NetworkX and transport-planning tools compute betweenness with Brandes’ accumulation: one search from each vertex, then a dependency pass. The score says which junctions lie on many shortest paths. PageRank scores a different question, the stationary visit rate.
When you can drop this algorithm
Drop exact Brandes when V is large enough that V shortest-path searches will not finish. A sampled-source estimator is then a different algorithm and should be labeled as one. Keep Brandes when the graph is moderate and the score required is exact betweenness.
Why this stays published
We publish this so a search for Brandes’ algorithm returns the backward dependency (σ[u]/σ[v]) · (1+δ[v]) and the stack that makes the walk legal. Brandes’ algorithm computes betweenness by summing, over every source, a backward pass over the shortest-path DAG, in O(VE) time on an unweighted graph.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
