~ubuntu-branches/ubuntu/utopic/python-networkx/utopic

« back to all changes in this revision

Viewing changes to networkx/algorithms/assortativity/neighbor_degree.py

  • Committer: Package Import Robot
  • Author(s): Sandro Tosi
  • Date: 2012-08-11 12:41:30 UTC
  • mfrom: (1.4.1) (5.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20120811124130-whr6uso7fncyg8bi
Tags: 1.7-1
* New upstream release
* debian/patches/changeset_*
  - removed, included upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#-*- coding: utf-8 -*-
 
2
#    Copyright (C) 2011 by 
 
3
#    Jordi Torrents <jtorrents@milnou.net>
 
4
#    Aric Hagberg <hagberg@lanl.gov>
 
5
#    All rights reserved.
 
6
#    BSD license.
 
7
import networkx as nx
 
8
__author__ = """\n""".join(['Jordi Torrents <jtorrents@milnou.net>',
 
9
                            'Aric Hagberg (hagberg@lanl.gov)'])
 
10
__all__ = ["average_neighbor_degree"]
 
11
 
 
12
 
 
13
def _average_nbr_deg(G, source_degree, target_degree, nodes=None, weight=None):
 
14
    # average degree of neighbors
 
15
    avg = {}
 
16
    for n,deg in source_degree(nodes,weight=weight).items():
 
17
        # normalize but not by zero degree
 
18
        if deg == 0:
 
19
            deg = 1
 
20
        nbrdeg = target_degree(G[n])
 
21
        if weight is None:
 
22
            avg[n] = sum(nbrdeg.values())/float(deg)
 
23
        else:
 
24
            avg[n] = sum((G[n][nbr].get(weight,1)*d 
 
25
                          for nbr,d in nbrdeg.items()))/float(deg)
 
26
    return avg
 
27
 
 
28
def average_neighbor_degree(G, source='out', target='out',
 
29
                            nodes=None, weight=None):
 
30
    r"""Returns the average degree of the neighborhood of each node.
 
31
 
 
32
    The average degree of a node `i` is
 
33
 
 
34
    .. math::
 
35
 
 
36
        k_{nn,i} = \frac{1}{|N(i)|} \sum_{j \in N(i)} k_j
 
37
 
 
38
    where `N(i)` are the neighbors of node `i` and `k_j` is
 
39
    the degree of node `j` which belongs to `N(i)`. For weighted 
 
40
    graphs, an analogous measure can be defined [1]_,
 
41
 
 
42
    .. math::
 
43
 
 
44
        k_{nn,i}^{w} = \frac{1}{s_i} \sum_{j \in N(i)} w_{ij} k_j
 
45
 
 
46
    where `s_i` is the weighted degree of node `i`, `w_{ij}`
 
47
    is the weight of the edge that links `i` and `j` and
 
48
    `N(i)` are the neighbors of node `i`.
 
49
 
 
50
 
 
51
    Parameters
 
52
    ----------
 
53
    G : NetworkX graph
 
54
 
 
55
    source : string ("in"|"out")
 
56
       Directed graphs only.
 
57
       Use "in"- or "out"-degree for source node.  
 
58
 
 
59
    target : string ("in"|"out")
 
60
       Directed graphs only.
 
61
       Use "in"- or "out"-degree for target node.
 
62
 
 
63
    nodes : list or iterable, optional 
 
64
        Compute neighbor degree for specified nodes.  The default is
 
65
        all nodes in the graph.
 
66
 
 
67
 
 
68
    weight : string or None, optional (default=None)
 
69
       The edge attribute that holds the numerical value used as a weight.
 
70
       If None, then each edge has weight 1.
 
71
 
 
72
    Returns
 
73
    -------
 
74
    d: dict
 
75
       A dictionary keyed by node with average neighbors degree value.
 
76
 
 
77
    Examples
 
78
    --------
 
79
    >>> G=nx.path_graph(4)
 
80
    >>> G.edge[0][1]['weight'] = 5
 
81
    >>> G.edge[2][3]['weight'] = 3
 
82
 
 
83
    >>> nx.average_neighbor_degree(G)
 
84
    {0: 2.0, 1: 1.5, 2: 1.5, 3: 2.0}
 
85
    >>> nx.average_neighbor_degree(G, weight='weight')
 
86
    {0: 2.0, 1: 1.1666666666666667, 2: 1.25, 3: 2.0}
 
87
 
 
88
    >>> G=nx.DiGraph()
 
89
    >>> G.add_path([0,1,2,3])
 
90
    >>> nx.average_neighbor_degree(G, source='in', target='in')
 
91
    {0: 1.0, 1: 1.0, 2: 1.0, 3: 0.0}
 
92
 
 
93
    >>> nx.average_neighbor_degree(G, source='out', target='out')
 
94
    {0: 1.0, 1: 1.0, 2: 0.0, 3: 0.0}
 
95
 
 
96
    Notes
 
97
    -----
 
98
    For directed graphs you can also specify in-degree or out-degree 
 
99
    by passing keyword arguments.
 
100
 
 
101
    See Also
 
102
    --------
 
103
    average_degree_connectivity 
 
104
    
 
105
    References
 
106
    ----------    
 
107
    .. [1] A. Barrat, M. Barthélemy, R. Pastor-Satorras, and A. Vespignani, 
 
108
       "The architecture of complex weighted networks". 
 
109
       PNAS 101 (11): 3747–3752 (2004).
 
110
    """
 
111
    source_degree = G.degree
 
112
    target_degree = G.degree
 
113
    if G.is_directed():
 
114
        direction = {'out':G.out_degree,
 
115
                     'in':G.in_degree}
 
116
        source_degree = direction[source]
 
117
        target_degree = direction[target]
 
118
    return _average_nbr_deg(G, source_degree, target_degree, 
 
119
                            nodes=nodes, weight=weight)
 
120
 
 
121
# obsolete
 
122
# def average_neighbor_in_degree(G, nodes=None, weight=None):
 
123
#     if not G.is_directed():
 
124
#         raise nx.NetworkXError("Not defined for undirected graphs.")
 
125
#     return _average_nbr_deg(G, G.in_degree, G.in_degree, nodes, weight)
 
126
# average_neighbor_in_degree.__doc__=average_neighbor_degree.__doc__
 
127
 
 
128
# def average_neighbor_out_degree(G, nodes=None, weight=None):
 
129
#     if not G.is_directed():
 
130
#         raise nx.NetworkXError("Not defined for undirected graphs.")
 
131
#     return _average_nbr_deg(G, G.out_degree, G.out_degree, nodes, weight)
 
132
# average_neighbor_out_degree.__doc__=average_neighbor_degree.__doc__
 
133