2
Functional interface to graph properties.
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
14
# functional style helpers
17
"""Return a copy of the graph nodes in a list."""
21
"""Return an iterator over the graph nodes."""
24
def edges(G,nbunch=None):
25
"""Return list of edges adjacent to nodes in nbunch.
27
Return all edges if nbunch is unspecified or nbunch=None.
29
For digraphs, edges=out_edges
32
return G.edges(nbunch)
34
def edges_iter(G,nbunch=None):
35
"""Return iterator over edges adjacent to nodes in nbunch.
37
Return all edges if nbunch is unspecified or nbunch=None.
39
For digraphs, edges=out_edges
42
return G.edges_iter(nbunch)
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.
48
return G.degree(nbunch,with_labels=with_labels)
51
"""Return a list of nodes connected to node n. """
54
def number_of_nodes(G):
55
"""Return the order of a graph = number of nodes."""
56
return G.number_of_nodes()
58
def number_of_edges(G):
59
"""Return the size of a graph = number of edges. """
60
return G.number_of_edges()
63
"""Return the density of a graph.
65
density = size/(order*(order-1)/2)
66
density()=0.0 for an edge-less graph and 1.0 for a complete graph.
70
if e==0: # includes cases n==0 and n==1
73
return e*2.0/float(n*(n-1))
75
def degree_histogram(G):
76
"""Return a list of the frequency of each degree value.
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))
84
freq= [ 0 for d in xrange(dmax) ]
90
""" Return True if graph is directed."""
91
return G.is_directed()
96
suite = doctest.DocFileSuite('tests/function.txt',
101
if __name__ == "__main__":
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]
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())