ioftools / networkxMiCe / networkxmaster / examples / basic / plot_properties.py @ 5cef0f13
History  View  Annotate  Download (1.2 KB)
1 
#!/usr/bin/env python


2 
"""

3 
==========

4 
Properties

5 
==========

6 

7 
Compute some network properties for the lollipop graph.

8 
"""

9 
# Copyright (C) 20042019 by

10 
# Aric Hagberg <hagberg@lanl.gov>

11 
# Dan Schult <dschult@colgate.edu>

12 
# Pieter Swart <swart@lanl.gov>

13 
# All rights reserved.

14 
# BSD license.

15  
16 
import matplotlib.pyplot as plt 
17 
from networkx import nx 
18  
19 
G = nx.lollipop_graph(4, 6) 
20  
21 
pathlengths = [] 
22  
23 
print("source vertex {target:length, }")

24 
for v in G.nodes(): 
25 
spl = dict(nx.single_source_shortest_path_length(G, v))

26 
print('{} {} '.format(v, spl))

27 
for p in spl: 
28 
pathlengths.append(spl[p]) 
29  
30 
print('')

31 
print("average shortest path length %s" % (sum(pathlengths) / len(pathlengths))) 
32  
33 
# histogram of path lengths

34 
dist = {} 
35 
for p in pathlengths: 
36 
if p in dist: 
37 
dist[p] += 1

38 
else:

39 
dist[p] = 1

40  
41 
print('')

42 
print("length #paths")

43 
verts = dist.keys() 
44 
for d in sorted(verts): 
45 
print('%s %d' % (d, dist[d]))

46  
47 
print("radius: %d" % nx.radius(G))

48 
print("diameter: %d" % nx.diameter(G))

49 
print("eccentricity: %s" % nx.eccentricity(G))

50 
print("center: %s" % nx.center(G))

51 
print("periphery: %s" % nx.periphery(G))

52 
print("density: %s" % nx.density(G))

53  
54 
nx.draw(G, with_labels=True)

55 
plt.show() 