Skip to content
Related Articles

Related Articles

Closeness Centrality (Centrality Measure)
  • Last Updated : 08 Feb, 2018

In a connected graph,closeness centrality (or closeness) of a node is a measure of centrality in a network, calculated as the sum of the length of the shortest paths between the node and all other nodes in the graph. Thus the more central a node is, the closer it is to all other nodes.

Closeness was defined by Bavelas (1950) as the reciprocal of the farness,that is:

C(x)={\frac  {1}{\sum _{y}d(y,x)}}.
where  d(y,x) is the distance between vertices x and  y. When speaking of closeness centrality, people usually refer to its normalized form which represents the average length of the shortest paths instead of their sum. It is generally given by the previous formula multiplied by  N-1, where  N is the number of nodes in the graph. For large graphs this difference becomes inconsequential so the  -1 is dropped resulting in:

C(x)={\frac {N}{\sum _{y}d(y,x)}}.
This adjustment allows comparisons between nodes of graphs of different sizes.

Taking distances from or to all other nodes is irrelevant in undirected graphs, whereas it can produce totally different results in directed graphs (e.g. a website can have a high closeness centrality from outgoing link, but low closeness centrality from incoming links).

In disconnected graphs
When a graph is not strongly connected, a widespread idea is that of using the sum of reciprocal of distances, instead of the reciprocal of the sum of distances, with the convention  1/\infty=0:

 H(x)=\sum _{{y\neq x}}{\frac  {1}{d(y,x)}}.
The most natural modification of Bavelas’s definition of closeness is following the general principle proposed by Marchiori and Latora (2000) that in graphs with infinite distances the harmonic mean behaves better than the arithmetic mean. Indeed, Bavelas’s closeness can be described as the denormalized reciprocal of the arithmetic mean of distances, whereas harmonic centrality is the denormalized reciprocal of the harmonic mean of distances.

This idea was explicitly stated for undirected graphs under the name valued centrality by Dekker (2005) and under the name harmonic centrality by Rochat (2009), axiomatized by Garg (2009) and proposed once again later by Opsahl (2010). It was studied on general directed graphs by Boldi and Vigna (2014). This idea is also quite similar to market potential proposed in Harris (1954) which now often goes by the term market access.

Dangalchev (2006), in a work on network vulnerability proposes for undirected graphs a different definition:

 D(x)=\sum _{{y\neq x}}{\frac  {1}{2^{{d(y,x)}}}}.
This definition is used effectively for disconnected graphs and allows to create convenient formulae for graph operations. For example if a graph   G_{1}+G_{2} is created by linking node p of graph  G_{1} to node q of graph G_{2} then the combine closeness is:

The information centrality of Stephenson and Zelen (1989) is another closeness measure, which computes the harmonic mean of the resistance distances towards a vertex x, which is smaller if x has many paths of small resistance connecting it to other vertices.

In the classic definition of the closeness centrality, the spread of information is modeled by the use of shortest paths. This model might not be the most realistic for all types of communication scenarios. Thus, related definitions have been discussed to measure closeness, like the random walk closeness centrality introduced by Noh and Rieger (2004). It measures the speed with which randomly walking messages reach a vertex from elsewhere in the graph—a sort of random-walk version of closeness centrality. Hierarchical closeness of Tran and Kwon (2014) is an extended closeness centrality to deal still in another way with the limitation of closeness in graphs that are not strongly connected. The hierarchical closeness explicitly includes information about the range of other nodes that can be affected by the given node.

Following is the code for the calculation of the Closeness Centrality of the graph and its various nodes.

def closeness_centrality(G, u=None, distance=None, normalized=True):
    r"""Compute closeness centrality for nodes.
    Closeness centrality [1]_ of a node `u` is the reciprocal of the
    sum of the shortest path distances from `u` to all `n-1` other nodes.
    Since the sum of distances depends on the number of nodes in the
    graph, closeness is normalized by the sum of minimum possible
    distances `n-1`.
    .. math::
        C(u) = \frac{n - 1}{\sum_{v=1}^{n-1} d(v, u)},
    where `d(v, u)` is the shortest-path distance between `v` and `u`,
    and `n` is the number of nodes in the graph.
    Notice that higher values of closeness indicate higher centrality.
    G : graph
      A NetworkX graph
    u : node, optional
      Return only the value for node u
    distance : edge attribute key, optional (default=None)
      Use the specified edge attribute as the edge distance in shortest 
      path calculations
    normalized : bool, optional
      If True (default) normalize by the number of nodes in the connected
      part of the graph.
    nodes : dictionary
      Dictionary of nodes with closeness centrality as the value.
    See Also
    betweenness_centrality, load_centrality, eigenvector_centrality,
    The closeness centrality is normalized to `(n-1)/(|G|-1)` where
    `n` is the number of nodes in the connected part of graph
    containing the node.  If the graph is not completely connected,
    this algorithm computes the closeness centrality for each
    connected part separately.
    If the 'distance' keyword is set to an edge attribute key then the
    shortest-path length will be computed using Dijkstra's algorithm with
    that edge attribute as the edge weight.
    if distance is not None:
        # use Dijkstra's algorithm with specified attribute as edge weight 
        path_length = functools.partial(nx.single_source_dijkstra_path_length,
        path_length = nx.single_source_shortest_path_length
    if u is None:
        nodes = G.nodes()
        nodes = [u]
    closeness_centrality = {}
    for n in nodes:
        sp = path_length(G,n)
        totsp = sum(sp.values())
        if totsp > 0.0 and len(G) > 1:
            closeness_centrality[n] = (len(sp)-1.0) / totsp
            # normalize to number of nodes-1 in connected part
            if normalized:
                s = (len(sp)-1.0) / ( len(G) - 1 )
                closeness_centrality[n] *= s
            closeness_centrality[n] = 0.0
    if u is not None:
        return closeness_centrality[u]
        return closeness_centrality

The above function is invoked using the networkx library and once the library is installed, you can eventually use it and the following code has to be written in python for the implementation of the closeness centrality of a node.

import networkx as nx

The output of the above code is:

{0: 0.6971830985915493, 1: 0.7122302158273381, 2: 0.6875, 3: 0.7021276595744681
4: 0.7443609022556391, 5: 0.7388059701492538, 6: 0.7071428571428572, 7: 0.7388059701492538,
 8: 0.7388059701492538, 9: 0.7226277372262774, 10: 0.6923076923076923, 11: 0.7021276595744681,
 12: 0.7388059701492538, 13: 0.6923076923076923, 14: 0.7279411764705882, 15: 0.6644295302013423,
 16: 0.7333333333333333, 17: 0.7388059701492538, 18: 0.7122302158273381, 19: 0.678082191780822,
 20: 0.6923076923076923, 21: 0.717391304347826, 22: 0.6923076923076923, 23: 0.7071428571428572
24: 0.7333333333333333, 25: 0.717391304347826, 26: 0.7734375, 27: 0.7071428571428572,
 28: 0.7557251908396947, 29: 0.7279411764705882, 30: 0.717391304347826, 31: 0.7122302158273381,
 32: 0.7122302158273381, 33: 0.7226277372262774, 34: 0.6827586206896552, 35: 0.7021276595744681
36: 0.7122302158273381, 37: 0.673469387755102, 38: 0.75, 39: 0.7071428571428572,
 40: 0.7333333333333333, 41: 0.717391304347826, 42: 0.7071428571428572, 43: 0.6875
44: 0.7615384615384615, 45: 0.7226277372262774, 46: 0.7857142857142857, 47: 0.7388059701492538,
 48: 0.7333333333333333, 49: 0.717391304347826, 50: 0.7021276595744681, 51: 0.7071428571428572,
 52: 0.717391304347826, 53: 0.7333333333333333, 54: 0.717391304347826, 55: 0.7388059701492538,
 56: 0.7021276595744681, 57: 0.7557251908396947, 58: 0.6971830985915493, 59: 0.7795275590551181,
 60: 0.7122302158273381, 61: 0.7388059701492538, 62: 0.717391304347826, 63: 0.6923076923076923,
 64: 0.7071428571428572, 65: 0.7021276595744681, 66: 0.7021276595744681, 67: 0.7021276595744681,
 68: 0.7388059701492538, 69: 0.7333333333333333, 70: 0.6923076923076923, 71: 0.75
72: 0.7557251908396947, 73: 0.7443609022556391, 74: 0.7388059701492538, 75: 0.7388059701492538,
 76: 0.6346153846153846, 77: 0.7071428571428572, 78: 0.7226277372262774, 79: 0.7674418604651163,
 80: 0.7071428571428572, 81: 0.7279411764705882, 82: 0.678082191780822, 83: 0.7333333333333333,
 84: 0.7443609022556391, 85: 0.6923076923076923, 86: 0.7122302158273381, 87: 0.7333333333333333,
 88: 0.7279411764705882, 89: 0.7122302158273381, 90: 0.7122302158273381, 91: 0.7557251908396947,
 92: 0.7388059701492538, 93: 0.7021276595744681, 94: 0.6513157894736842, 95: 0.673469387755102,
 96: 0.7122302158273381, 97: 0.717391304347826, 98: 0.7021276595744681, 99: 0.7388059701492538}

The above result is a dictionary depicting the value of closeness centrality of each node. The above is an extension of my article series on the centrality measures. Keep networking!!!

You can read more about the same at

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :