修訂. | 0a0a791ee78b826ffa4675748e0ced1a0a88d7ea |
---|---|
大小 | 1,674 bytes |
時間 | 2009-06-19 00:07:53 |
作者 | isella |
Log Message | I added the calculation of the mean betweenness and explained the relation
in between the (unnormalized) betweenness calculated by igraph and the one
calculated with networkx. |
#! /usr/bin/env python
import scipy as s
import networkx as nx
def average_shortest_path_length(G,weighted=False):
# """ Return the average shortest path length.
# Parameters
# ----------
# G : NetworkX graph
# weighted : bool, optional, default=False
# If true use edge weights on path. If False,
# use 1 as the edge distance.
# Examples
# --------
# >>> G=nx.path_graph(4)
# >>> print nx.average_shortest_path_length(G)
# 1.25
# """
if weighted:
path_length=nx.single_source_dijkstra_path_length
else:
path_length=nx.single_source_shortest_path_length
avg=0.0
for n in G:
l=path_length(G,n).values()
avg+=float(sum(l))/len(l)
return avg/len(G)
G=nx.read_edgelist("edgelist-airport")
print "The mean degree is, ", s.mean(G.degree())
print "The mean clustering coeff is, ", s.mean(nx.clustering(G))
print "The length of the graph diameter is, ", nx.diameter(G)
short=average_shortest_path_length(G)
print "the mean shortest path is, ", short
bet_val=nx.betweenness_centrality(G, normalized=False)
#NB: the betweenness is stored as a Python dictionary, now I need to use its .values() method
#to fetch the numerical values of the betweenness of each node
bet_arr=s.asarray(bet_val.values()) #I changed the numerical values of the betweenness for each vertex
#into a numpy array
print "the mean value of the betweenness is, ", bet_arr.mean()/2. #careful about the definitions here!!!
#there is a factor 2 between the (unnormalized) betweenness in networkx and in igraph
#print "bet_arr is, ", bet_arr
print "So far so good"