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:
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.
Have a look at the close_centrality.ipynb
file in this book's code bundle:
import networkx as nx import dautil as dl
fb_file = dl.data.SPANFB().load() G = nx.read_edgelist(fb_file, create_using=nx.Graph(), nodetype=int)
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