plot_erdos_renyi.py 931 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. """
  4. ===========
  5. Erdos Renyi
  6. ===========
  7. Create an G{n,m} random graph with n nodes and m edges
  8. and report some properties.
  9. This graph is sometimes called the Erdős-Rényi graph
  10. but is different from G{n,p} or binomial_graph which is also
  11. sometimes called the Erdős-Rényi graph.
  12. """
  13. # Author: Aric Hagberg (hagberg@lanl.gov)
  14. # Copyright (C) 2004-2019 by
  15. # Aric Hagberg <hagberg@lanl.gov>
  16. # Dan Schult <dschult@colgate.edu>
  17. # Pieter Swart <swart@lanl.gov>
  18. # All rights reserved.
  19. # BSD license.
  20. import matplotlib.pyplot as plt
  21. from networkx import nx
  22. n = 10 # 10 nodes
  23. m = 20 # 20 edges
  24. G = nx.gnm_random_graph(n, m)
  25. # some properties
  26. print("node degree clustering")
  27. for v in nx.nodes(G):
  28. print('%s %d %f' % (v, nx.degree(G, v), nx.clustering(G, v)))
  29. # print the adjacency list
  30. for line in nx.generate_adjlist(G):
  31. print(line)
  32. nx.draw(G)
  33. plt.show()