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
7
8
9
10
11
12
13
14
15
17 """Return a copy of the graph nodes in a list."""
18 return G.nodes()
19
21 """Return an iterator over the graph nodes."""
22 return G.nodes_iter()
23
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
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
51 """Return a list of nodes connected to node n. """
52 return G.neighbors(n)
53
55 """Return the order of a graph = number of nodes."""
56 return G.number_of_nodes()
57
59 """Return the size of a graph = number of edges. """
60 return G.number_of_edges()
61
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:
71 return 0.0
72 else:
73 return e*2.0/float(n*(n-1))
74
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
90 """ Return True if graph is directed."""
91 return G.is_directed()
92
93
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
109 nxbase=sys.path[0]+os.sep+os.pardir
110 sys.path.insert(0,nxbase)
111 unittest.TextTestRunner().run(_test_suite())
112