diff options
| author | Jarrod Millman <jarrod.millman@gmail.com> | 2017-07-21 08:50:40 -0700 |
|---|---|---|
| committer | Dan Schult <dschult@colgate.edu> | 2017-07-21 11:50:40 -0400 |
| commit | 1e6df4c105fa7a1e442da72ddb009bb253dc7d4e (patch) | |
| tree | d728526ccca673a762f90f174223cb558c86e02d /examples/basic/plot_properties.py | |
| parent | 7be0d8f33b46d37264ae181cb2a9fadb310fe687 (diff) | |
| download | networkx-1e6df4c105fa7a1e442da72ddb009bb253dc7d4e.tar.gz | |
Refactor examples (#2527)
* Fix unix_email and plot more examples
* Fix doctest
* Fix more examples
* Fix AntiGraph.degree generator error in examples
* Style/consistency changes and plot more examples
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() |
