2
# Copyright (C) 2011 by
3
# Jordi Torrents <jtorrents@milnou.net>
4
# Aric Hagberg <hagberg@lanl.gov>
8
__author__ = """\n""".join(['Jordi Torrents <jtorrents@milnou.net>',
9
'Aric Hagberg (hagberg@lanl.gov)'])
10
__all__ = ["average_neighbor_degree"]
13
def _average_nbr_deg(G, source_degree, target_degree, nodes=None, weight=None):
14
# average degree of neighbors
16
for n,deg in source_degree(nodes,weight=weight).items():
17
# normalize but not by zero degree
20
nbrdeg = target_degree(G[n])
22
avg[n] = sum(nbrdeg.values())/float(deg)
24
avg[n] = sum((G[n][nbr].get(weight,1)*d
25
for nbr,d in nbrdeg.items()))/float(deg)
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.
32
The average degree of a node `i` is
36
k_{nn,i} = \frac{1}{|N(i)|} \sum_{j \in N(i)} k_j
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]_,
44
k_{nn,i}^{w} = \frac{1}{s_i} \sum_{j \in N(i)} w_{ij} k_j
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`.
55
source : string ("in"|"out")
57
Use "in"- or "out"-degree for source node.
59
target : string ("in"|"out")
61
Use "in"- or "out"-degree for target node.
63
nodes : list or iterable, optional
64
Compute neighbor degree for specified nodes. The default is
65
all nodes in the graph.
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.
75
A dictionary keyed by node with average neighbors degree value.
79
>>> G=nx.path_graph(4)
80
>>> G.edge[0][1]['weight'] = 5
81
>>> G.edge[2][3]['weight'] = 3
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}
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}
93
>>> nx.average_neighbor_degree(G, source='out', target='out')
94
{0: 1.0, 1: 1.0, 2: 0.0, 3: 0.0}
98
For directed graphs you can also specify in-degree or out-degree
99
by passing keyword arguments.
103
average_degree_connectivity
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).
111
source_degree = G.degree
112
target_degree = G.degree
114
direction = {'out':G.out_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)
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__
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__