1 # Filename: stat_indictors.py
2
3 import networkx as nx
4
5 # generate a n = 1000, m = 3 BA scale-free network
6 G = nx.random_graphs.barabasi_albert_graph(1000, 3)
7 print G.degree(0) # return node0's degree
8 print G.degree() # return all node's degree
9 # return all node's distribution sequence, from 1 to max degree
10 print nx.degree_histogram(G)
11
12 # add module matplotlib for drawing
13 import matplotlib.pyplot as plt
14 degree = nx.degree_histogram(G)
15 # generate X scale sequence, from 1 to max degree
16 x = range(len(degree))
17 # transform the times to fequency
18 y = [z / float(sum(degree)) for z in degree]
19 # Distribution curve in double logarithmic axes
20 plt.loglog(x, y, color = "blue", linewidth = 2)
21 plt.show()
22
23 # calculate average Clustering coefficient
24 print 'average clustering coefficient is ', nx.average_clustering(G)
25 # calculate every node's clustering coefficient
26 print "every node's clustering coefficient is ", nx.clustering(G)
27
28 # calucate Diameter of G (the length of the longest shortest path)
29 print 'Diameter of G is ', nx.diameter(G)
30 # calculate all nodes' average shortest path length
31 print "all nodes' average shortest path length is", nx.average_shortest_path_length(G