plot_degree_sequence.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env python
  2. """
  3. ===============
  4. Degree Sequence
  5. ===============
  6. Random graph from given degree sequence.
  7. """
  8. # Author: Aric Hagberg (hagberg@lanl.gov)
  9. # Date: 2004-11-03 08:11:09 -0700 (Wed, 03 Nov 2004)
  10. # Revision: 503
  11. # Copyright (C) 2004-2019 by
  12. # Aric Hagberg <hagberg@lanl.gov>
  13. # Dan Schult <dschult@colgate.edu>
  14. # Pieter Swart <swart@lanl.gov>
  15. # All rights reserved.
  16. # BSD license.
  17. import matplotlib.pyplot as plt
  18. from networkx import nx
  19. z = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
  20. print(nx.is_graphical(z))
  21. print("Configuration model")
  22. G = nx.configuration_model(z) # configuration model
  23. degree_sequence = [pen_weight for n, pen_weight in G.degree()] # degree sequence
  24. print("Degree sequence %s" % degree_sequence)
  25. print("Degree histogram")
  26. hist = {}
  27. for pen_weight in degree_sequence:
  28. if pen_weight in hist:
  29. hist[pen_weight] += 1
  30. else:
  31. hist[pen_weight] = 1
  32. print("degree #nodes")
  33. for pen_weight in hist:
  34. print('%d %d' % (pen_weight, hist[pen_weight]))
  35. nx.draw(G)
  36. plt.show()