plot_erdos_renyi.py 841 B

123456789101112131415161718192021222324252627282930313233343536
  1. """
  2. ===========
  3. Erdos Renyi
  4. ===========
  5. Create an G{n,m} random graph with n nodes and m edges
  6. and report some properties.
  7. This graph is sometimes called the Erdős-Rényi graph
  8. but is different from G{n,p} or binomial_graph which is also
  9. sometimes called the Erdős-Rényi graph.
  10. """
  11. import matplotlib.pyplot as plt
  12. import networkx as nx
  13. n = 10 # 10 nodes
  14. m = 20 # 20 edges
  15. seed = 20160 # seed random number generators for reproducibility
  16. # Use seed for reproducibility
  17. G = nx.gnm_random_graph(n, m, seed=seed)
  18. # some properties
  19. print("node degree clustering")
  20. for v in nx.nodes(G):
  21. print(f"{v} {nx.degree(G, v)} {nx.clustering(G, v)}")
  22. print()
  23. print("the adjacency list")
  24. for line in nx.generate_adjlist(G):
  25. print(line)
  26. pos = nx.spring_layout(G, seed=seed) # Seed for reproducible layout
  27. nx.draw(G, pos=pos)
  28. plt.show()