Calculating social network closeness centrality

In a social network such as the Facebook SPAN data, we will have influential people. In graph terminology, these are the influential nodes. Centrality finds features of important nodes. Closeness centrality uses shortest paths between nodes as a feature, as shown in the following equation:

Calculating social network closeness centrality

In (8.3), d(u, v) is the shortest path between u, v, and n is the number of nodes. An influential node is close to other nodes and, therefore, the sum of the shortest paths is low. We can compute closeness centrality for each node separately, and for a large graph, this can be a lengthy calculation. NetworkX allows us to specify which node we are interested in, so we will calculate closeness centrality just for a few nodes.

Getting ready

Install NetworkX with the instructions from the Introduction section.

How to do it...

Have a look at the close_centrality.ipynb file in this book's code bundle:

  1. The imports are as follows:
    import networkx as nx
    import dautil as dl
  2. Create a NetworkX graph from the Facebook SPAN data as follows:
    fb_file = dl.data.SPANFB().load()
    G = nx.read_edgelist(fb_file,
                         create_using=nx.Graph(),
                         nodetype=int)
  3. Calculate the closeness centrality for node 1 and node 4037:
    print('Closeness Centrality Node 1',
          nx.closeness_centrality(G, 1))
    print('Closeness Centrality Node 4037',
          nx.closeness_centrality(G, 4037))

We get the following result for the Facebook SPAN data:

Closeness Centrality Node 1 0.2613761408505405
Closeness Centrality Node 4037 0.18400546821599453

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset