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-2008 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
16
__all__ = ['nodes', 'edges', 'degree', 'degree_histogram', 'neighbors',
17
'number_of_nodes', 'number_of_edges', 'density',
18
'nodes_iter', 'edges_iter', 'is_directed','info']
21
"""Return a copy of the graph nodes in a list."""
25
"""Return an iterator over the graph nodes."""
28
def edges(G,nbunch=None):
29
"""Return list of edges adjacent to nodes in nbunch.
31
Return all edges if nbunch is unspecified or nbunch=None.
33
For digraphs, edges=out_edges
36
return G.edges(nbunch)
38
def edges_iter(G,nbunch=None):
39
"""Return iterator over edges adjacent to nodes in nbunch.
41
Return all edges if nbunch is unspecified or nbunch=None.
43
For digraphs, edges=out_edges
46
return G.edges_iter(nbunch)
48
def degree(G,nbunch=None,with_labels=False):
49
"""Return degree of single node or of nbunch of nodes.
50
If nbunch is ommitted, then return degrees of *all* nodes.
52
return G.degree(nbunch,with_labels=with_labels)
55
"""Return a list of nodes connected to node n. """
58
def number_of_nodes(G):
59
"""Return the order of a graph = number of nodes."""
60
return G.number_of_nodes()
62
def number_of_edges(G):
63
"""Return the size of a graph = number of edges. """
64
return G.number_of_edges()
67
"""Return the density of a graph.
69
density = size/(order*(order-1)/2)
70
density()=0.0 for an edge-less graph and 1.0 for a complete graph.
74
if e==0: # includes cases n==0 and n==1
77
return e*2.0/float(n*(n-1))
79
def degree_histogram(G):
80
"""Return a list of the frequency of each degree value.
82
The degree values are the index in the list.
83
Note: the bins are width one, hence len(list) can be large
84
(Order(number_of_edges))
88
freq= [ 0 for d in xrange(dmax) ]
94
""" Return True if graph is directed."""
97
def info(self, n=None):
98
"""Print short info summary for graph G or node n."""
103
print ("Name:").ljust(width_left), self.name
104
type_name = [type(self).__name__]
105
print ("Type:").ljust(width_left), ",".join(type_name)
106
print ("Number of nodes:").ljust(width_left), self.number_of_nodes()
107
print ("Number of edges:").ljust(width_left), self.number_of_edges()
109
print ("Average degree:").ljust(width_left), \
110
round( sum(self.degree_iter())/float(len(self)), 4)
113
list_neighbors = self.neighbors(n)
114
except (KeyError, TypeError):
115
raise NetworkXError, "node %s not in graph"%(n,)
116
print "\nNode", n, "has the following properties:"
117
print ("Degree:").ljust(width_left), len(list_neighbors)
118
str_neighbors = str(list_neighbors)
119
str_neighbors = str_neighbors[1:len(str_neighbors)-1]
120
wrapped_neighbors = textwrap.wrap(str_neighbors, 50)
121
print ("Neighbors:").ljust(width_left), wrapped_neighbors[0]
122
for i in wrapped_neighbors[1:]:
123
print "".ljust(width_left), i