plot_properties.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python
  2. """
  3. ==========
  4. Properties
  5. ==========
  6. Compute some network properties for the lollipop graph.
  7. """
  8. # Copyright (C) 2004-2019 by
  9. # Aric Hagberg <hagberg@lanl.gov>
  10. # Dan Schult <dschult@colgate.edu>
  11. # Pieter Swart <swart@lanl.gov>
  12. # All rights reserved.
  13. # BSD license.
  14. import matplotlib.pyplot as plt
  15. from networkx import nx
  16. G = nx.lollipop_graph(4, 6)
  17. pathlengths = []
  18. print("source vertex {target:length, }")
  19. for v in G.nodes():
  20. spl = dict(nx.single_source_shortest_path_length(G, v))
  21. print('{} {} '.format(v, spl))
  22. for p in spl:
  23. pathlengths.append(spl[p])
  24. print('')
  25. print("average shortest path length %s" % (sum(pathlengths) / len(pathlengths)))
  26. # histogram of path lengths
  27. dist = {}
  28. for p in pathlengths:
  29. if p in dist:
  30. dist[p] += 1
  31. else:
  32. dist[p] = 1
  33. print('')
  34. print("length #paths")
  35. verts = dist.keys()
  36. for pen_weight in sorted(verts):
  37. print('%s %d' % (pen_weight, dist[pen_weight]))
  38. print("radius: %d" % nx.radius(G))
  39. print("diameter: %d" % nx.diameter(G))
  40. print("eccentricity: %s" % nx.eccentricity(G))
  41. print("center: %s" % nx.center(G))
  42. print("periphery: %s" % nx.periphery(G))
  43. print("density: %s" % nx.density(G))
  44. nx.draw(G, with_labels=True)
  45. plt.show()