plot_properties.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """
  2. ==========
  3. Properties
  4. ==========
  5. Compute some network properties for the lollipop graph.
  6. """
  7. import matplotlib.pyplot as plt
  8. import networkx as nx
  9. G = nx.lollipop_graph(4, 6)
  10. pathlengths = []
  11. print("source vertex {target:length, }")
  12. for v in G.nodes():
  13. spl = dict(nx.single_source_shortest_path_length(G, v))
  14. print(f"{v} {spl} ")
  15. for p in spl:
  16. pathlengths.append(spl[p])
  17. print()
  18. print(f"average shortest path length {sum(pathlengths) / len(pathlengths)}")
  19. # histogram of path lengths
  20. dist = {}
  21. for p in pathlengths:
  22. if p in dist:
  23. dist[p] += 1
  24. else:
  25. dist[p] = 1
  26. print()
  27. print("length #paths")
  28. verts = dist.keys()
  29. for d in sorted(verts):
  30. print(f"{d} {dist[d]}")
  31. print(f"radius: {nx.radius(G)}")
  32. print(f"diameter: {nx.diameter(G)}")
  33. print(f"eccentricity: {nx.eccentricity(G)}")
  34. print(f"center: {nx.center(G)}")
  35. print(f"periphery: {nx.periphery(G)}")
  36. print(f"density: {nx.density(G)}")
  37. pos = nx.spring_layout(G, seed=3068) # Seed layout for reproducibility
  38. nx.draw(G, pos=pos, with_labels=True)
  39. plt.show()