diff options
Diffstat (limited to 'examples/basic/plot_properties.py')
| -rw-r--r-- | examples/basic/plot_properties.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/examples/basic/plot_properties.py b/examples/basic/plot_properties.py new file mode 100644 index 00000000..30e055df --- /dev/null +++ b/examples/basic/plot_properties.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +""" +========== +Properties +========== + +Compute some network properties for the lollipop graph. +""" +# Copyright (C) 2004-2017 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 d in sorted(verts): + print('%s %d' % (d, dist[d])) + +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() |
