diff options
| author | aric <none@none> | 2008-11-13 02:23:43 +0000 |
|---|---|---|
| committer | aric <none@none> | 2008-11-13 02:23:43 +0000 |
| commit | de380743ecd272d5cf455272ec5a000ffc2a8b3d (patch) | |
| tree | 2fba4d935601e91f3e10f9f6f7383c24ff0f158a /examples/basic | |
| parent | c106301825a7ae3906b672d11d8ca6586db75ac4 (diff) | |
| download | networkx-de380743ecd272d5cf455272ec5a000ffc2a8b3d.tar.gz | |
move examples directory out of doc directory
--HG--
extra : convert_revision : svn%3A3ed01bd8-26fb-0310-9e4c-ca1a4053419f/networkx/trunk%40961
Diffstat (limited to 'examples/basic')
| -rw-r--r-- | examples/basic/properties.py | 49 | ||||
| -rw-r--r-- | examples/basic/read_write.py | 21 |
2 files changed, 70 insertions, 0 deletions
diff --git a/examples/basic/properties.py b/examples/basic/properties.py new file mode 100644 index 00000000..0d8fa434 --- /dev/null +++ b/examples/basic/properties.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +""" +Compute some network properties for the lollipop graph. +""" +# Copyright (C) 2004 by +# Aric Hagberg <hagberg@lanl.gov> +# Dan Schult <dschult@colgate.edu> +# Pieter Swart <swart@lanl.gov> +# Distributed under the terms of the GNU Lesser General Public License +# http://www.gnu.org/copyleft/lesser.html + +from networkx import * + +G = lollipop_graph(4,6) + +pathlengths=[] + +print "source vertex {target:length, }" +for v in G.nodes(): + spl=single_source_shortest_path_length(G,v) + print v,spl + for p in spl.values(): + pathlengths.append(p) + +print +print "average shortest path length ", sum(pathlengths)/len(pathlengths) + +# histogram of path lengths +dist={} +for p in pathlengths: + if dist.has_key(p): + dist[p]+=1 + else: + dist[p]=1 + +print +print "length #paths" +verts=dist.keys() +verts.sort() +for d in verts: + print d,dist[d] + +print "radius: ",radius(G) +print "diameter: ",diameter(G) +print "eccentricity: ",eccentricity(G,with_labels=True) +print "center: ",center(G) +print "periphery: ",periphery(G) +print "density: ", density(G) + diff --git a/examples/basic/read_write.py b/examples/basic/read_write.py new file mode 100644 index 00000000..08c22970 --- /dev/null +++ b/examples/basic/read_write.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +""" +Read and write graphs. +""" +__author__ = """Aric Hagberg (hagberg@lanl.gov)""" +# Copyright (C) 2004-2006 by +# Aric Hagberg <hagberg@lanl.gov> +# Dan Schult <dschult@colgate.edu> +# Pieter Swart <swart@lanl.gov> +# Distributed under the terms of the GNU Lesser General Public License +# http://www.gnu.org/copyleft/lesser.html + +from networkx import * +import sys +G=grid_2d_graph(5,5) # 5x5 grid +write_adjlist(G,sys.stdout) # write adjacency list to screen +# write edgelist to grid.edgelist +write_edgelist(G,path="grid.edgelist",delimiter=":") +# read edgelist from grid.edgelist +H=read_edgelist(path="grid.edgelist",delimiter=":") + |
