learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Algorithm Selection

Select an algorithm and generate steps to begin visualization

Harmonic Centrality Visualizer

Closeness that never divides by infinity

Adds up 1 / distance over all the other vertices: a closeness that stays defined when the graph is not connected

Time: O(V(V + E))
Space: O(V)
Use Case: Social and road networks, graphs in several pieces, accessibility
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Harmonic Centrality

Harmonic centrality measures how close a vertex is to all the others by adding up 1 / distance: a neighbour adds 1, a vertex two steps away 1/2, a vertex out of reach 0. Divided by n − 1, the score lies between 0 and 1. Closeness centrality instead divides by the sum of the distances, so a single unreachable vertex makes that sum infinite and the score useless unless it is patched. Marchiori and Latora used the harmonic form in 2000 for networks in pieces, and Boldi and Vigna showed in 2014 that it satisfies the axioms they proposed for a good centrality, where closeness does not.

How it works

The visualizer runs one shortest-path search per vertex, breadth-first search on an unweighted graph and Dijkstra's algorithm on a weighted one. On a directed graph it measures the distances to the vertex, along the edges that lead into it, the way closeness does in most libraries. Each step shows the distances as pills, the shortest-path tree to the vertex in green and the sum written out, for instance H(A) = (5×1/1 + 1/2 + ...) / 10. The panel keeps the harmonic scores beside the closeness scores, and vertices grow with their score. In the example, a hub with five neighbours and a long tail, closeness ranks F first because the far end of the tail costs A a lot, while harmonic centrality ranks A first: its five neighbours count fully and the far vertices count little.

Applications

Harmonic centrality is the closeness of choice whenever a graph may be disconnected: social networks with isolated groups, road networks cut by closures, directed graphs where many vertices cannot reach one another. Neo4j Graph Data Science, NetworkX and igraph provide it. It ranks hubs for spreading information, locates facilities and services that most places reach quickly, and compares how well connected the stations of a transport network are. Because near vertices dominate the sum, it rewards a vertex at the centre of a dense area more than one that sits between two distant parts, which betweenness centrality measures instead.

Pseudocode

One shortest-path search per vertex, then a sum of reciprocals.

Harmonic(G):
    for each vertex v:
        run BFS to v (Dijkstra if weighted)
        H(v) = sum of 1 / d(u, v) over the other vertices u
               // a vertex that cannot reach v adds 1 / infinity = 0
        H(v) = H(v) / (n - 1)
    return H

The sum Σ 1 / d is n − 1 divided by the harmonic mean of the distances, hence the name. Closeness uses the arithmetic mean instead, (n − 1) / Σ d. An infinite distance makes the arithmetic mean infinite, but adds only 0 to the harmonic sum.

Worked example, step by step

The star with a tail loaded on this page, 11 vertices.

Example graph: A is joined to B, C, D, E and F. From F a path runs F – G – H – I – J – K.

  1. A. Five neighbours at distance 1, then G at 2, H at 3, I at 4, J at 5 and K at 6. H(A) = (5 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6) / 10 = 6.45 / 10 = 0.645.
  2. B, C, D, E. One neighbour, A, four vertices at 2, then the tail at 3 to 7. Each gets 4.093 / 10 = 0.409.
  3. F. Two neighbours, A and G, five vertices at 2, then I, J and K at 3, 4 and 5. H(F) = 5.283 / 10 = 0.528.
  4. Along the tail. G 0.492, H 0.467, I 0.438, J 0.395 and K 0.302. The scores fall as the vertices move away from the crowded star.
  5. Closeness for comparison. The distances from A add up to 25 and from F to 24, so closeness gives F 10 / 24 = 0.417 above A at 10 / 25 = 0.4. The far end of the tail costs A 6, while A's five close neighbours count no more than any other vertex.

A 0.645, F 0.528, G 0.492, H 0.467, I 0.438. Harmonic centrality puts A first where closeness puts F first.

Complexity, and where it comes from

Time: O(n(n + m)) unweighted, O(n(n + m) log n) weighted · Space: O(n + m)

One breadth-first search costs O(n + m), and one runs per vertex. With weights each search is Dijkstra with a heap, O((n + m) log n). On large graphs this is too slow, so tools estimate the scores: HyperBall by Boldi and Vigna counts the vertices at each distance with small sketches, and sampling a few hundred sources gives good approximations of the ranking.

When to use Harmonic Centrality, and when not to

Distance-based centralities and when to use each.

AlternativePrefer it whenCost
ClosenessThe graph is connected and a single far vertex should weigh in fully. It divides by the sum of the distances.O(n(n + m))
Wasserman–Faust closenessA tool only offers closeness on a graph in pieces. It computes closeness inside each vertex's component and scales it by the share of vertices it reaches.O(n(n + m))
BetweennessThe question is which vertex lies on many shortest paths, a bridge or broker, rather than which is near everyone.O(nm) unweighted

Common pitfalls

  • Forgetting that some tools do not normalise. NetworkX returns the raw sum Σ 1 / d, here 6.45 for A, without dividing by n − 1. Neo4j and this page divide. Compare graphs of different sizes only with the normalised score.
  • Measuring the wrong direction. On a directed graph, distances to a vertex and from it differ. This page, like most libraries, measures the distances to the vertex along the edges into it. For reach out of a vertex, reverse the graph.
  • Using weights that mean strength. The weights are lengths: a heavier edge is a longer one. If the weights measure how strong a tie is, such as messages exchanged, invert them first, or the strongest ties count as the farthest.

Frequently asked questions

Why is it called harmonic?
The sum of 1 / d over the other vertices equals n − 1 divided by the harmonic mean of the distances. So harmonic centrality is the reciprocal of the harmonic mean distance, as closeness is the reciprocal of the arithmetic mean distance.
On a connected graph, is it the same as closeness?
No. Both reward short distances, but closeness is dominated by the far vertices and harmonic centrality by the near ones. The rankings often agree, yet this page shows a case where they disagree at the top.
What did Boldi and Vigna prove?
They proposed three axioms for a centrality: size, density and score monotonicity, which says that adding an edge into a vertex must not lower its score. Of the classic measures they tested, only harmonic centrality satisfied all three.

Related algorithms: Closeness Centrality, Betweenness Centrality, Katz Centrality, Breadth-First Search, Dijkstra's Algorithm

Built by Mohammed Islam HadjoudjWork with me

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

Zoom Controls

30%
Nodes: 11
Edges: 10