Node Centrality in SAN

- Degree Centrality

In [2]:
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline

g = nx.Graph()
g.add_edge(1,2)
g.add_edge(1,3)
g.add_edge(1,4)
g.add_edge(2,3)
g.add_edge(3,4)
g.add_edge(4,5)
g.add_edge(4,6)
g.add_edge(5,6)
g.add_edge(5,7)
g.add_edge(5,8)
g.add_edge(6,7)
g.add_edge(6,8)
g.add_edge(7,8)
g.add_edge(7,9)
nx.draw(g, node_size=500, with_labels=True, font_size=16)
plt.show()
In [3]:
degree_centrality = nx.degree_centrality(g)
In [4]:
degree_centrality 
Out[4]:
{1: 0.375,
 2: 0.25,
 3: 0.375,
 4: 0.5,
 5: 0.5,
 6: 0.5,
 7: 0.5,
 8: 0.375,
 9: 0.125}
In [5]:
type(degree_centrality)
Out[5]:
dict
In [6]:
print degree_centrality.items()
[(1, 0.375), (2, 0.25), (3, 0.375), (4, 0.5), (5, 0.5), (6, 0.5), (7, 0.5), (8, 0.375), (9, 0.125)]
In [7]:
import operator

def sort_map(map):
    sortedList = map.items()
    sortedList.sort(key=operator.itemgetter(1), reverse=True)
    return sortedList
In [8]:
sorted_degree_centrality = sort_map(degree_centrality)
In [9]:
sorted_degree_centrality
Out[9]:
[(4, 0.5),
 (5, 0.5),
 (6, 0.5),
 (7, 0.5),
 (1, 0.375),
 (3, 0.375),
 (8, 0.375),
 (2, 0.25),
 (9, 0.125)]

- Closeness Centrality

In [10]:
closeness_centrality = nx.closeness_centrality(g)
In [11]:
closeness_centrality
Out[11]:
{1: 0.47058823529411764,
 2: 0.34782608695652173,
 3: 0.47058823529411764,
 4: 0.6153846153846154,
 5: 0.6153846153846154,
 6: 0.6153846153846154,
 7: 0.5,
 8: 0.47058823529411764,
 9: 0.34782608695652173}
In [12]:
sorted_closeness_centrality = sort_map(closeness_centrality)
In [13]:
sorted_closeness_centrality 
Out[13]:
[(4, 0.6153846153846154),
 (5, 0.6153846153846154),
 (6, 0.6153846153846154),
 (7, 0.5),
 (1, 0.47058823529411764),
 (3, 0.47058823529411764),
 (8, 0.47058823529411764),
 (2, 0.34782608695652173),
 (9, 0.34782608695652173)]

- Betweenness Centrality

In [14]:
bet_centrality = nx.betweenness_centrality(g)
In [15]:
bet_centrality
Out[15]:
{1: 0.10714285714285714,
 2: 0.0,
 3: 0.10714285714285714,
 4: 0.5357142857142857,
 5: 0.21428571428571427,
 6: 0.21428571428571427,
 7: 0.25,
 8: 0.0,
 9: 0.0}
In [16]:
sorted_bet_centrality =sort_map(bet_centrality)
In [17]:
sorted_bet_centrality
Out[17]:
[(4, 0.5357142857142857),
 (7, 0.25),
 (5, 0.21428571428571427),
 (6, 0.21428571428571427),
 (1, 0.10714285714285714),
 (3, 0.10714285714285714),
 (2, 0.0),
 (8, 0.0),
 (9, 0.0)]

- Eigenvector Centrality

In [18]:
eigenvector_centrality = nx.eigenvector_centrality(g)
In [19]:
eigenvector_centrality
Out[19]:
{1: 0.19575179821589203,
 2: 0.11168619729756277,
 3: 0.19575179821589203,
 4: 0.3787497778502688,
 5: 0.4680845766467968,
 6: 0.4680845766467968,
 7: 0.4099777873648636,
 8: 0.3840189757284675,
 9: 0.11695539517576153}
In [20]:
sorted_eigenvector_centrality= sort_map(eigenvector_centrality)
In [21]:
sorted_eigenvector_centrality 
Out[21]:
[(5, 0.4680845766467968),
 (6, 0.4680845766467968),
 (7, 0.4099777873648636),
 (8, 0.3840189757284675),
 (4, 0.3787497778502688),
 (1, 0.19575179821589203),
 (3, 0.19575179821589203),
 (9, 0.11695539517576153),
 (2, 0.11168619729756277)]

- Build a table with four centralities

In [22]:
rounded_degree_centrality = {k: round(v, 3) for k, v in degree_centrality.items()}
In [23]:
rounded_closeness_centrality = {k: round(v, 3) for k, v in closeness_centrality.items()}
In [24]:
rounded_bet_centrality = {k: round(v, 3) for k, v in bet_centrality.items()}
In [25]:
rounded_eigenvector_centrality = {k: round(v, 3) for k, v in eigenvector_centrality.items()}
In [26]:
table = [[node, rounded_degree_centrality[node], 
          rounded_closeness_centrality[node], 
          rounded_bet_centrality[node], 
          rounded_eigenvector_centrality[node]] for node in g.nodes()]
In [27]:
table
Out[27]:
[[1, 0.375, 0.471, 0.107, 0.196],
 [2, 0.25, 0.348, 0.0, 0.112],
 [3, 0.375, 0.471, 0.107, 0.196],
 [4, 0.5, 0.615, 0.536, 0.379],
 [5, 0.5, 0.615, 0.214, 0.468],
 [6, 0.5, 0.615, 0.214, 0.468],
 [7, 0.5, 0.5, 0.25, 0.41],
 [8, 0.375, 0.471, 0.0, 0.384],
 [9, 0.125, 0.348, 0.0, 0.117]]