Guest
Sign in to keep your graphs and progress.

Practice book
Graph Theory Through Problems300 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.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
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
Select an algorithm and generate steps to begin visualization
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.
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.
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.
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 CWhen 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.
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.
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.
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.
Closeness is one of several ways to say that a vertex is central. They answer different questions.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Degree centrality | Local popularity: who has the most direct contacts. Cheap, and often a good first look. | O(V + E) |
| Closeness centrality | Reach: who can get to everyone fastest, for spreading information or placing a facility. | O(V(V + E)) |
| Harmonic centrality | Reach on graphs in pieces: it sums 1 / distance, so an unreachable vertex adds 0 instead of breaking the formula. | O(V(V + E)) |
| Betweenness centrality | Control: who lies on the most shortest paths between others, the brokers and bottlenecks. | O(VE) |
Related algorithms: Betweenness Centrality, Eigenvector Centrality, PageRank, Dijkstra's Algorithm, Breadth-First Search