In network theory, Brandes' algorithm is an algorithm for calculating the betweenness centrality of vertices in a graph. The algorithm was first published in 2001 by Ulrik Brandes. Betweenness centrality, along with other measures of centrality, is an important measure in many real-world networks, such as social networks and computer networks.

Definitions
There are several metrics for the centrality of a node, one such metric being the betweenness centrality. For a node



v


{\displaystyle v}

in a connected graph, the betweenness centrality is defined as:




C

B


(
v
)
=

∑

s
∈
V



∑

t
∈
V






σ

s
t


(
v
)


σ

s
t






{\displaystyle C_{B}(v)=\sum _{s\in V}\sum _{t\in V}{\frac {\sigma _{st}(v)}{\sigma _{st}}}}

where




σ

s
t




{\displaystyle \sigma _{st}}

is the total number of shortest paths from node



s


{\displaystyle s}

to node



t


{\displaystyle t}

, and




σ

s
t


(
v
)


{\displaystyle \sigma _{st}(v)}

is the number of these paths which pass through



v


{\displaystyle v}

. For an unweighted graph, the length of a path is considered to be the number of edges it contains.
By convention,




σ

s
t


=
1


{\displaystyle \sigma _{st}=1}

whenever



s
=
t


{\displaystyle s=t}

, since the only path is the empty path. Also,




σ

s
t


(
v
)
=
0


{\displaystyle \sigma _{st}(v)=0}

if



v


{\displaystyle v}

is either



s


{\displaystyle s}

or



t


{\displaystyle t}

, since shortest paths do not pass through their endpoints.

The quantity




δ

s
t


(
v
)
=




σ

s
t


(
v
)


σ

s
t






{\displaystyle \delta _{st}(v)={\frac {\sigma _{st}(v)}{\sigma _{st}}}}

is known as the pair dependency of



s
t


{\displaystyle st}

on



v


{\displaystyle v}

, and represents the proportion of the shortest



s


{\displaystyle s}

–



t


{\displaystyle t}

paths which travel via



v


{\displaystyle v}

. The betweenness centrality is simply the sum of the pair dependencies over all pairs.
As well as the pair dependency, it is also useful to define the (single) dependency on



v


{\displaystyle v}

, with respect to a particular vertex



s


{\displaystyle s}

:




δ

s


(
v
)
=

∑

t
∈
V



δ

s
t


(
v
)


{\displaystyle \delta _{s}(v)=\sum _{t\in V}\delta _{st}(v)}

,with which, we can obtain the concise formulation




C

B


(
v
)
=

∑

s
∈
V



δ

s


(
v
)


{\displaystyle C_{B}(v)=\sum _{s\in V}\delta _{s}(v)}

.

Algorithm
Brandes' algorithm calculates the betweenness centrality of all nodes in a graph. For every vertex



s


{\displaystyle s}

, there are two stages.

Single-source shortest path
The number of shortest paths




σ

s
v




{\displaystyle \sigma _{sv}}

between



s


{\displaystyle s}

and every vertex



v


{\displaystyle v}

is calculated using breadth-first search. The breadth-first search starts at



s


{\displaystyle s}

, and the shortest distance



d
(
v
)


{\displaystyle d(v)}

of each vertex from



s


{\displaystyle s}

is recorded, dividing the graph into discrete layers. Additionally, each vertex



v


{\displaystyle v}

keeps track of the set of vertices which in the preceding layer which point to it,



p
(
v
)


{\displaystyle p(v)}

. Described in set-builder notation, it can be written as:



p
(
v
)
=
{
u
∈
V
∣
(
u
,
v
)
∈
E
∧
d
(
u
)
+
1
=
d
(
v
)
}


{\displaystyle p(v)=\{u\in V\mid (u,v)\in E\land d(u)+1=d(v)\}}

.This lends itself to a simple iterative formula for




σ

s
v




{\displaystyle \sigma _{sv}}

:




σ

s
v


=

∑

u
∈
p
(
v
)



σ

s
u




{\displaystyle \sigma _{sv}=\sum _{u\in p(v)}\sigma _{su}}

,which essentially states that, if



v


{\displaystyle v}

is at depth



d
(
v
)


{\displaystyle d(v)}

, then any shortest path at depth



d
(
v
)
−
1


{\displaystyle d(v)-1}

extended by a single edge to



v


{\displaystyle v}

becomes a shortest path to



v


{\displaystyle v}

.

(
1
+

δ

s


(
v
)
)


{\displaystyle \delta _{s}(u)=\sum _{v\mid u\in p(v)}{\frac {\sigma _{su}}{\sigma _{sv}}}\cdot (1+\delta _{s}(v))}

,where the sum is taken over all vertices



v


{\displaystyle v}

that are one edge further away from



s


{\displaystyle s}

than



u


{\displaystyle u}

. This lemma eliminates the need to explicitly sum all of the pair dependencies. Using this formula, the single dependency of



s


{\displaystyle s}

on a vertex



u


{\displaystyle u}

at depth



d
(
u
)


{\displaystyle d(u)}

is determined by the layer at depth



d
(
u
)
+
1


{\displaystyle d(u)+1}

. Furthermore, the order of summation is irrelevant, which allows for a bottom up approach starting at the deepest layer.
It turns out that the dependencies of



s


{\displaystyle s}

on all other vertices



u


{\displaystyle u}

can be computed in



O
(

|

E

|

)


{\displaystyle O(|E|)}

time. During the breadth-first search, the order in which vertices are visited is logged in a stack data structure. The backpropagation step then repeatedly pops off vertices, which are naturally sorted by their distance from



s


{\displaystyle s}

, descending.

(
1
+

δ

s


(
v
)
)


{\displaystyle {\frac {\sigma _{su}}{\sigma _{sv}}}\cdot (1+\delta _{s}(v))}

.Crucially, every layer propagates its dependencies completely, before moving to the layer with lower depth, due to the nature of breadth-first search. Once the propagation reaches back to



s


{\displaystyle s}

, every vertex



v


{\displaystyle v}

now contains




δ

s


(
v
)


{\displaystyle \delta _{s}(v)}

. These can simply be added to




C

B


(
v
)


{\displaystyle C_{B}(v)}

, since




C

B


(
v
)
=

∑

s
∈
V



δ

s


(
v
)


{\displaystyle C_{B}(v)=\sum _{s\in V}\delta _{s}(v)}

.After




|

V

|



{\displaystyle |V|}

iterations of single-source shortest path and backpropagation, each




C

B


(
v
)


{\displaystyle C_{B}(v)}

contains the betweenness centrality for



v


{\displaystyle v}

.

Pseudocode
The following pseudocode illustrates Brandes' algorithm on an unweighted directed graph.

algorithm Brandes(Graph) is
for each u in Graph.Vertices do
CB[u] ← 0

for each s in Graph.Vertices do
for each v in Graph.Vertices do
δ[v] ← 0 // Single dependency of s on v
prev[v] ← empty list // Immediate predecessors of v during BFS
σ[v] ← 0 // Number of shortest paths from s to v (s implied)
dist[v] ← null // No paths are known initially,
σ[s] ← 1 // except the start vertex
dist[s] ← 0

Q ← queue containing only s // Breadth-first search
S ← empty stack // Record the order in which vertices are visited

// Single-source shortest paths

while Q is not empty do
u ← Q.dequeue()
S.push(u)

for each v in Graph.Neighbours[u] do
if dist[v] = null then
dist[v] ← dist[u] + 1
Q.enqueue(v)
if dist[v] = dist[u] + 1 then
σ[v] ← σ[v] + σ[u]
prev[v].append(u)

// Backpropagation of dependencies

while S is not empty do
v ← S.pop()

for each u in prev[v] do
δ[u] ← δ[u] + σ[u] / σ[v] * (1 + δ[v])

if v ≠ s then
CB[v] ← CB[v] + δ[v] // Halved for undirected graphs

return CB

Running time
The running time of the algorithm is expressed in terms of the number of vertices




|

V

|



{\displaystyle |V|}

and the number of edges




|

E

|



{\displaystyle |E|}

.
For each vertex



s


{\displaystyle s}

, we run breadth-first search, which takes



O
(

|

V

|

+

|

E

|

)


{\displaystyle O(|V|+|E|)}

time. Since the graph is connected, the




|

E

|



{\displaystyle |E|}

component subsumes the




|

V

|



{\displaystyle |V|}

term, since the number of edges is at least




|

V

|

−
1


{\displaystyle |V|-1}

.
In the backpropagation stage, every vertex is popped off the stack, and its predecessors are iterated over. However, since each predecessor entry corresponds to an edge in the graph, this stage is also bounded by



O
(

|

E

|

)


{\displaystyle O(|E|)}

.
The overall running time of the algorithm is therefore



O
(

|

V

|


|

E

|

)


{\displaystyle O(|V||E|)}

, an improvement on the



O
(

|

V


|


3


)


{\displaystyle O(|V|^{3})}

time bounds achieved by prior algorithms. In addition, Brandes' algorithm improves on the space complexity of naive algorithms, which typically require



O
(

|

V


|


2


)


{\displaystyle O(|V|^{2})}

space. Brandes' algorithm only stores at most




|

E

|



{\displaystyle |E|}

predecessors, along with data for each vertex, making its extra space complexity



O
(

|

V

|

+

|

E

|

)


{\displaystyle O(|V|+|E|)}

Variants
The algorithm can be generalised to weighted graphs by using Dijkstra's algorithm instead of breadth-first search. When operating on undirected graphs, the betweenness centrality may be divided by 2, to avoid double counting each path's reversed counterpart. Variants also exist to calculate different measures of centrality, including betweenness with paths at most length



k


{\displaystyle k}

, edge betweenness, load betweenness, and stress betweenness.

References