12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- #!/usr/bin/env python
- """
- ==========
- Properties
- ==========
- Compute some network properties for the lollipop graph.
- """
- # Copyright (C) 2004-2019 by
- # Aric Hagberg <hagberg@lanl.gov>
- # Dan Schult <dschult@colgate.edu>
- # Pieter Swart <swart@lanl.gov>
- # All rights reserved.
- # BSD license.
- import matplotlib.pyplot as plt
- from networkx import nx
- G = nx.lollipop_graph(4, 6)
- pathlengths = []
- print("source vertex {target:length, }")
- for v in G.nodes():
- spl = dict(nx.single_source_shortest_path_length(G, v))
- print('{} {} '.format(v, spl))
- for p in spl:
- pathlengths.append(spl[p])
- print('')
- print("average shortest path length %s" % (sum(pathlengths) / len(pathlengths)))
- # histogram of path lengths
- dist = {}
- for p in pathlengths:
- if p in dist:
- dist[p] += 1
- else:
- dist[p] = 1
- print('')
- print("length #paths")
- verts = dist.keys()
- for pen_weight in sorted(verts):
- print('%s %d' % (pen_weight, dist[pen_weight]))
- print("radius: %d" % nx.radius(G))
- print("diameter: %d" % nx.diameter(G))
- print("eccentricity: %s" % nx.eccentricity(G))
- print("center: %s" % nx.center(G))
- print("periphery: %s" % nx.periphery(G))
- print("density: %s" % nx.density(G))
- nx.draw(G, with_labels=True)
- plt.show()
|