NetworkX
Contents |
Introduction
NetworkX is a Python tool which can be used to calculate graph metrics such as clustering coefficient and centrality for both weighted and non-weighted graphs.
Installation
For all the installation steps, please refer to: http://networkx.lanl.gov/install.html.
Usage
For detailed usage of NetworkX, please refer to: http://networkx.lanl.gov/index.html
Examples
A simple example using NetworkX to calculate several metrics for a undirected weighted graph is shown as follows (Installation of NetworkX Python Package required).
import networkx as nx G = nx.Graph() ''' the input of adjacency matrix is a list and each element is a tuple: [(node 1, node 2, weight of link 1-2),(node i, node j, weigh of link i-j),...,()] ''' adj = [(1, 2, 0.22), (1, 3, 0.47), (1, 4, 0.85), (1, 5, 0.99), (2, 3, 0.12), (2, 5, 0.32), (3, 4, 0.87), (4, 5, 0.66)] G.add_weighted_edges_from(adj) print G.degree(weight='weight') print nx.clustering(G, weight='weight') print nx.betweenness_centrality(G, weight='weight')
The output looks as follows, and they are in the format of Python Dictionary {node_1: degree_for_node_1, node_2: degree_for_node_2,...,node_n: degree_for_node_n}:
degree: {1: 2.5300000000000002, 2: 0.6599999999999999, 3: 1.46, 4: 2.38, 5: 1.9700000000000002} clustering: {1: 0.36500345465046613, 2: 0.2165116935975265, 3: 0.31468028188250186, 4: 0.5134952157034057, 5: 0.4153266274184304} betweenness: {1: 0.0, 2: 0.5, 3: 0.0, 4: 0.0, 5: 0.16666666666666666}
For more examples and algorithm, please refer to: http://networkx.lanl.gov/networkx_reference.pdf