learngraphtheory.org

Guest User

Using app without sign in

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

300 Exercises with Hints, Worked Algorithms and Complete Solutions

By Mohammed Islam Hadjoudj

Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

Betweenness Centrality Calculator

Which vertices sit on the most shortest paths?

Share of shortest paths through each vertex, with Brandes' algorithm: one search per source, dependencies added back from the farthest vertex

Time: O(VE)
Space: O(V + E)
Use Case: Brokers in social networks, bottlenecks in transport and communication networks
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Betweenness Centrality

Betweenness centrality measures how often a vertex lies on the shortest paths between other vertices. A vertex with high betweenness is a broker or a bottleneck: much of the traffic between other parts of the network has to pass through it, even if it has few connections of its own. Linton Freeman defined it in 1977 for social networks, and Ulrik Brandes' algorithm of 2001 made it practical for large graphs.

How it works

For each source vertex s, a breadth-first search (Dijkstra's algorithm when edges are weighted) finds the distance to every vertex and σ(v), the number of shortest paths from s to v: each vertex adds up σ over its predecessors. Then the vertices are visited from the farthest back to s, and each passes its dependency δ to its predecessors in proportion to σ: δ(v) gains σ(v)/σ(w) × (1 + δ(w)) from every w that follows it. Adding δ(v) over all sources gives the betweenness of v. For undirected graphs each pair is counted twice, so the result is halved. The whole computation takes O(VE) time instead of looking at every pair of vertices separately.

Applications

Betweenness finds influential brokers in social and organisational networks, critical routers and links in communication networks, bottleneck stations in transport networks and key proteins in biological networks. Its edge version drives the Girvan-Newman method for community detection. It is a standard topic in network science courses, together with degree, closeness and PageRank.

Pseudocode

Brandes' algorithm computes the betweenness of every vertex without enumerating shortest paths. One BFS per source counts paths, and a backward pass over the same BFS order accumulates how much each vertex depends on the others.

Brandes(graph):
    CB[v] = 0 for every v
    for each source s:
        BFS from s, recording for every v:
            sigma[v] = number of shortest s - v paths
            pred[v]  = predecessors of v on those paths
            stack    = vertices in order of distance
        delta[v] = 0 for every v
        while stack is not empty:
            w = stack.pop()              // farthest first
            for each v in pred[w]:
                delta[v] += sigma[v] / sigma[w] * (1 + delta[w])
            if w != s: CB[w] += delta[w]
    halve every CB[v] if the graph is undirected
    return CB

delta[v] is the dependency of the source s on v: the sum, over all targets, of the fraction of shortest s-paths to that target that pass through v. The recursion works because a shortest path through v to a far target must continue through one of the successors w of v, and v carries the share sigma[v] / sigma[w] of the paths into w.

Worked example, step by step

Compute betweenness on the example this page loads, Krackhardt's kite, a ten-person network built to show that three classic centralities pick three different winners.

Example graph: Undirected edges A-B, A-C, A-D, A-F, B-D, B-E, B-G, C-D, C-F, D-E, D-F, D-G, E-G, F-G, F-H, G-H, H-I and I-J. Seven people form the kite, and H, I and J form its tail.

  1. Count paths from A. The BFS from A finds three shortest paths to G, through B, D or F, so sigma(G) = 3. H is reached by a single shortest path, A-F-H, and then I and J behind it.
  2. Accumulate from A. Walking back from the farthest vertex, I carries the path to J and H carries the paths to I and J. F collects those plus a third of the paths to G. Halved for an undirected graph, the contributions from source A are F 1.67, H 1, I 0.5, and 0.42 each for B and D.
  3. All ten sources. The same two passes run from every vertex. Sources in the tail, such as I and J, give large contributions to H, because every one of their paths into the kite must leave through H.
  4. Read the totals. H 14, F 8.33, G 8.33, I 8, D 3.67, A 0.83, B 0.83, and 0 for C, E and J. The value of H has a direct reading: each of the seven kite members has a shortest path to I and to J, and all 7 × 2 = 14 of those pass through H.

D has six neighbours, the most in the network, yet its betweenness is only 3.67, because the kite is so densely connected that most pairs have shortcuts that avoid D. F and G are closest to everyone, but H is the broker: remove it and the tail is cut off. Normalised by (n - 1)(n - 2) / 2 = 36 pairs, H scores 0.389. This is the canonical illustration that degree, closeness and betweenness measure different things.

Complexity, and where it comes from

Time: O(VE) unweighted, O(VE + V^2 log V) weighted · Space: O(V + E)

For each of the V sources, the BFS costs O(V + E) and the backward accumulation visits each predecessor link once, also O(V + E). That gives O(V (V + E)), which is O(VE) on a connected graph. For weighted graphs the BFS becomes Dijkstra, at O(E + V log V) per source. Memory is reused between sources, so only O(V + E) is needed at any time. The naive method of counting pairs and paths directly costs O(V^3) even with Floyd-Warshall, which is why Brandes 2001 made betweenness practical on networks with tens of thousands of vertices.

When to use Betweenness Centrality, and when not to

Centrality measures answer different questions. Betweenness is about control of flow along shortest paths.

AlternativePrefer it whenCost
Degree centralityLocal popularity is what matters, and the network is too large for anything costlier.O(V + E)
Closeness centralityYou want vertices that can reach everyone quickly, such as a good place for a depot or a broadcaster.O(VE)
PageRankThe graph is directed and importance should flow along links, as with web pages or citations.O(k (V + E))
Sampled betweennessThe graph has millions of vertices. Running Brandes from a random sample of sources gives an unbiased estimate.O(kE) for k sources
Edge betweennessYou need the score of edges rather than vertices, for example to find community boundaries with Girvan-Newman.O(VE)

Common pitfalls

  • Counting each pair twice. In an undirected graph every pair is seen once from each end, so the raw Brandes sums are double the standard values. Halve them, as the visualizer does, or results will not match textbooks and libraries.
  • Ignoring multiple shortest paths. When there are several shortest paths between a pair, each intermediate vertex gets only its fraction of them. Counting a vertex fully whenever it lies on any shortest path overstates the scores of dense regions.
  • Comparing raw scores across networks. Raw betweenness grows roughly with the square of the network size. Normalise by the number of pairs, (n - 1)(n - 2) / 2 for undirected graphs, before comparing networks.
  • Using BFS on weighted graphs. If edges have lengths, shortest paths are weighted and the BFS must be replaced by Dijkstra. The accumulation step is unchanged.

Frequently asked questions

What is betweenness centrality?
The betweenness of a vertex v is the sum, over all pairs of other vertices s and t, of the fraction of shortest paths from s to t that pass through v. High betweenness marks brokers and bottlenecks that much of the shortest path traffic must cross.
How is betweenness centrality calculated?
With Brandes' algorithm: for each vertex as a source, a BFS counts the shortest paths to every other vertex, and a backward pass in decreasing order of distance accumulates the dependency of the source on each vertex. Summing these dependencies over all sources gives the betweenness.
What is the time complexity of betweenness centrality?
O(VE) for unweighted graphs and O(VE + V^2 log V) for weighted ones with Brandes' algorithm. That is fast enough for graphs with hundreds of thousands of edges, and sampling sources makes it feasible on much larger ones.
What is the difference between betweenness and closeness centrality?
Closeness measures how near a vertex is to all others on average. Betweenness measures how often it lies between others. In Krackhardt's kite, F and G have the highest closeness, while H, which joins the tail to the rest, has the highest betweenness.
What is betweenness centrality used for?
Finding brokers in social and organisational networks, critical routers and links in communication networks, bottleneck stations in transport, and community boundaries, since the edges with the highest betweenness tend to connect different communities.

Related algorithms: PageRank, Girvan-Newman Communities, Breadth-First Search, Dijkstra's Algorithm

Interactive Controls

Zoom Controls

52%
Nodes: 10
Edges: 18