learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

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

Closeness Centrality Visualizer

Who is closest to everyone

One shortest-path search from every vertex: a vertex scores high when its distances to all the others add up to little

Time: O(V(V + E))
Space: O(V)
Use Case: Information spread, accessibility, social networks
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Closeness Centrality

Closeness centrality measures how near a vertex is to all the other vertices of a graph. It takes the shortest-path distance from the vertex to every other vertex, adds them up and turns the sum into a score: the smaller the total distance, the closer the vertex is to everyone and the higher its closeness. For a connected graph the score is (n - 1) divided by the sum of the distances, the inverse of the average distance, so a vertex adjacent to every other vertex scores exactly 1. Alex Bavelas introduced the idea in 1950 to study communication in small groups, and it is one of the three classic centrality measures, next to degree and betweenness.

How it works

The algorithm runs one shortest-path search from each vertex: breadth-first search when the edges are unweighted, Dijkstra's algorithm when they have lengths. From the distances it counts the vertices reached, r, and their total distance, S. The closeness is then (r / S) × (r / (n - 1)). The first factor is the inverse of the average distance to the vertices reached, the second scales it down by the share of the graph that is reached (the Wasserman and Faust correction, also used by NetworkX), so a vertex in a small separate piece does not look central. On a directed graph the distances are measured to the vertex, along the edges that lead into it. With n searches the total time is O(V(V + E)) without weights. The visualizer draws the distances from the vertex being scored, the shortest paths it found and every vertex sized by its score so far.

Applications

Closeness finds the vertices from which information spreads fastest: the person in a team who passes news on best, the airport or warehouse with the shortest average trip to everywhere else, the server with the lowest average latency. In epidemiology it points to people who can reach a whole community quickly. In road and transport networks it ranks locations by accessibility, and in biology it highlights proteins that are close to many others in an interaction network. Because it uses every distance, it complements degree (local) and betweenness (brokerage): on Krackhardt's kite, the example loaded here, the three measures crown three different vertices.

Pseudocode

Closeness needs the distance from every vertex to every other one, so it is one single-source shortest-path search per vertex. The score then needs only two numbers per vertex: how many vertices it reached and their total distance.

Closeness(G):
    n = number of vertices
    for each vertex v:
        d = BFS(G, v)            // Dijkstra(G, v) if weighted
        R = vertices u ≠ v with d[u] < ∞
        S = sum of d[u] over u in R
        if S == 0:
            C[v] = 0
        else:
            C[v] = (|R| / S) × (|R| / (n - 1))
    return C

When v reaches every vertex, the formula is simply (n - 1) / S, Bavelas's original closeness. The second factor, |R| / (n - 1), is the Wasserman and Faust correction for graphs in pieces, and it is what NetworkX computes by default. Harmonic centrality, the sum of 1 / d[u], is the other common fix: an unreachable vertex simply adds 0.

Worked example, step by step

Run it on Krackhardt's kite, the ten-person network that David Krackhardt drew in 1990 to show that different centralities pick different people.

Example graph: A to G form a dense kite: D is linked to six others (A, B, C, E, F, G), F and G to five each. H is linked to F and G, I to H, and J ends the tail. The graph is connected, so every closeness is (n - 1) / S = 9 / S.

  1. D, the best connected. D is one step from six vertices, two from H, three from I and four from J: S = 6 + 2 + 3 + 4 = 15, closeness 9 / 15 = 0.6.
  2. F and G, the bridges to the tail. F is one step from A, C, D, G and H, two steps from B, E and I, and three from J: S = 5 + 6 + 3 = 14, closeness 9 / 14 = 0.643. G mirrors F and scores the same.
  3. H, the broker. H has only three neighbours (F, G, I), but every other vertex is two steps away: S = 3 + 12 = 15, the same sum as D, so H also scores 0.6.
  4. J, the end of the tail. J reaches I in one step but needs four steps to reach any of A, B, C, D and E: S = 29, the lowest closeness, 0.31.

Closeness crowns F and G (0.643), degree crowns D (six neighbours), and betweenness crowns H, which lies on every shortest path into the tail. Three measures, three answers: the right one depends on whether you care about local popularity, overall reach or control of the flow.

Complexity, and where it comes from

Time: O(V(V + E)) · Space: O(V)

One breadth-first search costs O(V + E) and there are V of them, so the total is O(V(V + E)), which is O(V³) on a dense graph. With weights each search is Dijkstra's algorithm with a binary heap, O((V + E) log V), so O(V(V + E) log V) overall. A search needs O(V) memory for its distances and queue, and the scores add O(V). For huge networks exact closeness is too slow, so libraries sample a few hundred source vertices and estimate the rest (Eppstein and Wang), or compute only the top k vertices with pruned searches.

When to use Closeness Centrality, and when not to

Closeness is one of several ways to say that a vertex is central. They answer different questions.

AlternativePrefer it whenCost
Degree centralityLocal popularity: who has the most direct contacts. Cheap, and often a good first look.O(V + E)
Closeness centralityReach: who can get to everyone fastest, for spreading information or placing a facility.O(V(V + E))
Harmonic centralityReach on graphs in pieces: it sums 1 / distance, so an unreachable vertex adds 0 instead of breaking the formula.O(V(V + E))
Betweenness centralityControl: who lies on the most shortest paths between others, the brokers and bottlenecks.O(VE)

Common pitfalls

  • Disconnected graphs. The textbook formula (n - 1) / S breaks when some vertex is unreachable: S is infinite, or, if unreachable vertices are skipped, a vertex in a tiny piece gets a perfect score. Use the Wasserman and Faust correction, as here, or harmonic centrality.
  • Direction matters. On a directed graph, closeness from a vertex (outgoing distances) and to a vertex (incoming distances) are different numbers. NetworkX uses incoming distances, and so does this visualizer. Reverse the graph to get the other one.
  • Weights are lengths, not strengths. Closeness reads edge weights as distances. In a social network where a weight counts the messages two people exchanged, a strong tie must become a short edge, for example weight 1 / messages, or the ranking comes out backwards.
  • Small differences. Closeness values tend to sit in a narrow band, because most vertices of a small-world network are only a few steps from everyone. Compare rankings, not tiny gaps in the third decimal.

Frequently asked questions

What is the formula for closeness centrality?
For a connected graph with n vertices, C(v) = (n - 1) / Σ d(v, u), where the sum runs over all other vertices u and d is the shortest-path distance. It is the inverse of the average distance from v. When v reaches only r of the other vertices, the corrected version is (r / S) × (r / (n - 1)).
Can closeness centrality be greater than 1?
No. Every other vertex is at least one step away, so the sum of distances is at least n - 1 and the score at most 1. It equals 1 exactly when the vertex is adjacent to all others, like the centre of a star.
What is the difference between closeness and betweenness centrality?
Closeness measures how short the paths from a vertex to everyone are. Betweenness measures how many shortest paths between other vertices pass through it. A vertex at the edge of a dense cluster can have high closeness and low betweenness, and a bridge between two groups can be the reverse.
How do I compute closeness centrality in Python?
With NetworkX: nx.closeness_centrality(G), or nx.closeness_centrality(G, distance='weight') for weighted graphs. It applies the Wasserman and Faust correction by default, which matches this visualizer, and nx.harmonic_centrality(G) gives the harmonic variant.

Related algorithms: Betweenness Centrality, Eigenvector Centrality, PageRank, Dijkstra's Algorithm, Breadth-First Search

An example graph: you can change it, or start blank.

Zoom Controls

30%
Nodes: 10
Edges: 18