Package networkx :: Module function
[hide private]
[frames] | no frames]

Source Code for Module networkx.function

  1  """ 
  2  Functional interface to graph properties. 
  3       
  4  """ 
  5  __author__ = """Aric Hagberg (hagberg@lanl.gov)\nPieter Swart (swart@lanl.gov)\nDan Schult(dschult@colgate.edu)""" 
  6  #    Copyright (C) 2004-2006 by  
  7  #    Aric Hagberg <hagberg@lanl.gov> 
  8  #    Dan Schult <dschult@colgate.edu> 
  9  #    Pieter Swart <swart@lanl.gov> 
 10  #    Distributed under the terms of the GNU Lesser General Public License 
 11  #    http://www.gnu.org/copyleft/lesser.html 
 12  # 
 13   
 14  # functional style helpers 
 15   
16 -def nodes(G):
17 """Return a copy of the graph nodes in a list.""" 18 return G.nodes()
19
20 -def nodes_iter(G):
21 """Return an iterator over the graph nodes.""" 22 return G.nodes_iter()
23
24 -def edges(G,nbunch=None):
25 """Return list of edges adjacent to nodes in nbunch. 26 27 Return all edges if nbunch is unspecified or nbunch=None. 28 29 For digraphs, edges=out_edges 30 31 """ 32 return G.edges(nbunch)
33
34 -def edges_iter(G,nbunch=None):
35 """Return iterator over edges adjacent to nodes in nbunch. 36 37 Return all edges if nbunch is unspecified or nbunch=None. 38 39 For digraphs, edges=out_edges 40 41 """ 42 return G.edges_iter(nbunch)
43
44 -def degree(G,nbunch=None,with_labels=False):
45 """Return degree of single node or of nbunch of nodes. 46 If nbunch is ommitted, then return degrees of *all* nodes. 47 """ 48 return G.degree(nbunch,with_labels=with_labels)
49
50 -def neighbors(G,n):
51 """Return a list of nodes connected to node n. """ 52 return G.neighbors(n)
53
54 -def number_of_nodes(G):
55 """Return the order of a graph = number of nodes.""" 56 return G.number_of_nodes()
57
58 -def number_of_edges(G):
59 """Return the size of a graph = number of edges. """ 60 return G.number_of_edges()
61
62 -def density(G):
63 """Return the density of a graph. 64 65 density = size/(order*(order-1)/2) 66 density()=0.0 for an edge-less graph and 1.0 for a complete graph. 67 """ 68 n=number_of_nodes(G) 69 e=number_of_edges(G) 70 if e==0: # includes cases n==0 and n==1 71 return 0.0 72 else: 73 return e*2.0/float(n*(n-1))
74
75 -def degree_histogram(G):
76 """Return a list of the frequency of each degree value. 77 78 The degree values are the index in the list. 79 Note: the bins are width one, hence len(list) can be large 80 (Order(number_of_edges)) 81 """ 82 degseq=G.degree() 83 dmax=max(degseq)+1 84 freq= [ 0 for d in xrange(dmax) ] 85 for d in degseq: 86 freq[d] += 1 87 return freq
88
89 -def is_directed(G):
90 """ Return True if graph is directed.""" 91 return G.is_directed()
92 93
94 -def _test_suite():
95 import doctest 96 suite = doctest.DocFileSuite('tests/function.txt', 97 package='networkx') 98 return suite
99 100 101 if __name__ == "__main__": 102 import os 103 import sys 104 import unittest 105 if sys.version_info[:2] < (2, 4): 106 print "Python version 2.4 or later required for tests (%d.%d detected)." % sys.version_info[:2] 107 sys.exit(-1) 108 # directory of networkx package (relative to this) 109 nxbase=sys.path[0]+os.sep+os.pardir 110 sys.path.insert(0,nxbase) # prepend to search path 111 unittest.TextTestRunner().run(_test_suite()) 112