Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / assortativity / neighbor_degree.py @ 5cef0f13

History | View | Annotate | Download (4.22 KB)

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):
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(d for n, d in nbrdeg) / float(deg)
23
        else:
24
            avg[n] = sum((G[n][nbr].get(weight, 1) * d
25
                          for nbr, d in nbrdeg)) / float(deg)
26
    return avg
27

    
28

    
29
def average_neighbor_degree(G, source='out', target='out',
30
                            nodes=None, weight=None):
31
    r"""Returns the average degree of the neighborhood of each node.
32

33
    The average neighborhood degree of a node `i` is
34

35
    .. math::
36

37
        k_{nn,i} = \frac{1}{|N(i)|} \sum_{j \in N(i)} k_j
38

39
    where `N(i)` are the neighbors of node `i` and `k_j` is
40
    the degree of node `j` which belongs to `N(i)`. For weighted 
41
    graphs, an analogous measure can be defined [1]_,
42

43
    .. math::
44

45
        k_{nn,i}^{w} = \frac{1}{s_i} \sum_{j \in N(i)} w_{ij} k_j
46

47
    where `s_i` is the weighted degree of node `i`, `w_{ij}`
48
    is the weight of the edge that links `i` and `j` and
49
    `N(i)` are the neighbors of node `i`.
50

51

52
    Parameters
53
    ----------
54
    G : NetworkX graph
55

56
    source : string ("in"|"out")
57
       Directed graphs only.
58
       Use "in"- or "out"-degree for source node.  
59

60
    target : string ("in"|"out")
61
       Directed graphs only.
62
       Use "in"- or "out"-degree for target node.
63

64
    nodes : list or iterable, optional 
65
        Compute neighbor degree for specified nodes.  The default is
66
        all nodes in the graph.
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.edges[0, 1]['weight'] = 5
81
    >>> G.edges[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
    >>> nx.add_path(G, [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__