~ubuntu-branches/ubuntu/trusty/python-networkx/trusty-proposed

« back to all changes in this revision

Viewing changes to networkx/classes/function.py

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2009-02-28 13:36:24 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090228133624-9l5rwi1ftlzc7b0l
* Upload to unstable now that lenny is released (yay).
* Fix FTBFS with python-support 0.90.3: no longer rely on its internal
  behaviour, and xnow set tests/test.py executable right after “setup.py
  install” (Closes: #517065).
* Drop executable bits from bz2 files.
* Update Vcs-* fields: move from DPMT's svn to collab-maint's git.
* Remote DPMT from Uploaders, following Piotr Ożarowski's request.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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-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
 
12
#
 
13
 
 
14
# functional style helpers
 
15
 
 
16
__all__ = ['nodes', 'edges', 'degree', 'degree_histogram', 'neighbors',
 
17
           'number_of_nodes', 'number_of_edges', 'density',
 
18
           'nodes_iter', 'edges_iter', 'is_directed','info']
 
19
 
 
20
def nodes(G):
 
21
    """Return a copy of the graph nodes in a list."""
 
22
    return G.nodes()
 
23
 
 
24
def nodes_iter(G):
 
25
    """Return an iterator over the graph nodes."""
 
26
    return G.nodes_iter()
 
27
 
 
28
def edges(G,nbunch=None):
 
29
    """Return list of  edges adjacent to nodes in nbunch.
 
30
 
 
31
    Return all edges if nbunch is unspecified or nbunch=None.
 
32
 
 
33
    For digraphs, edges=out_edges
 
34
 
 
35
    """
 
36
    return G.edges(nbunch)
 
37
 
 
38
def edges_iter(G,nbunch=None):
 
39
    """Return iterator over  edges adjacent to nodes in nbunch.
 
40
 
 
41
    Return all edges if nbunch is unspecified or nbunch=None.
 
42
 
 
43
    For digraphs, edges=out_edges
 
44
    
 
45
    """
 
46
    return G.edges_iter(nbunch)
 
47
 
 
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.
 
51
    """
 
52
    return G.degree(nbunch,with_labels=with_labels)
 
53
 
 
54
def neighbors(G,n):
 
55
    """Return a list of nodes connected to node n. """
 
56
    return G.neighbors(n)
 
57
 
 
58
def number_of_nodes(G):
 
59
    """Return the order of a graph = number of nodes."""
 
60
    return G.number_of_nodes()
 
61
 
 
62
def number_of_edges(G):
 
63
    """Return the size of a graph = number of edges. """
 
64
    return G.number_of_edges()
 
65
    
 
66
def density(G):
 
67
    """Return the density of a graph.
 
68
    
 
69
    density = size/(order*(order-1)/2)
 
70
    density()=0.0 for an edge-less graph and 1.0 for a complete graph.
 
71
    """
 
72
    n=number_of_nodes(G)
 
73
    e=number_of_edges(G)
 
74
    if e==0: # includes cases n==0 and n==1
 
75
        return 0.0
 
76
    else:
 
77
        return e*2.0/float(n*(n-1))
 
78
 
 
79
def degree_histogram(G):
 
80
    """Return a list of the frequency of each degree value.
 
81
    
 
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))
 
85
    """
 
86
    degseq=G.degree()
 
87
    dmax=max(degseq)+1
 
88
    freq= [ 0 for d in xrange(dmax) ]
 
89
    for d in degseq:
 
90
        freq[d] += 1
 
91
    return freq
 
92
 
 
93
def is_directed(G):
 
94
    """ Return True if graph is directed."""
 
95
    return G.directed
 
96
 
 
97
def info(self, n=None):
 
98
    """Print short info summary for graph G or node n."""
 
99
    import textwrap
 
100
    width_left = 18
 
101
 
 
102
    if n is None:
 
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()
 
108
        if len(self) > 0:
 
109
            print ("Average degree:").ljust(width_left), \
 
110
                  round( sum(self.degree_iter())/float(len(self)), 4)
 
111
    else:
 
112
        try:
 
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