average_neighbor_degree#
- average_neighbor_degree(G, source='out', target='out', nodes=None, weight=None)[source]#
Returns the average degree of the neighborhood of each node.
In an undirected graph, the neighborhood
N(i)of nodeicontains the nodes that are connected toiby an edge.For directed graphs,
N(i)is defined according to the parametersource:if source is ‘in’, then
N(i)consists of predecessors of nodei.if source is ‘out’, then
N(i)consists of successors of nodei.if source is ‘in+out’, then
N(i)is both predecessors and successors.
The average neighborhood degree of a node
iis\[k_{nn,i} = \frac{1}{|N(i)|} \sum_{j \in N(i)} k_j\]where
N(i)are the neighbors of nodeiandk_jis the degree of nodejwhich belongs toN(i). For weighted graphs, an analogous measure can be defined [1],\[k_{nn,i}^{w} = \frac{1}{s_i} \sum_{j \in N(i)} w_{ij} k_j\]where
s_iis the weighted degree of nodei,w_{ij}is the weight of the edge that linksiandjandN(i)are the neighbors of nodei.- Parameters:
- GNetworkX graph
- sourcestring (“in”|”out”|”in+out”), optional (default=”out”)
Directed graphs only. Use “in”- or “out”-neighbors of source node.
- targetstring (“in”|”out”|”in+out”), optional (default=”out”)
Directed graphs only. Use “in”- or “out”-degree for target node.
- nodeslist or iterable, optional (default=G.nodes)
Compute neighbor degree only for specified nodes.
- weightstring or None, optional (default=None)
The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1.
- Returns:
- d: dict
A dictionary keyed by node to the average degree of its neighbors.
- Raises:
- NetworkXError
If either
sourceortargetare not one of ‘in’, ‘out’, or ‘in+out’. If eithersourceortargetis passed for an undirected graph.
See also
References
[1]A. Barrat, M. Barthélemy, R. Pastor-Satorras, and A. Vespignani, “The architecture of complex weighted networks”. PNAS 101 (11): 3747–3752 (2004).
Examples
>>> G = nx.path_graph(4) >>> G.edges[0, 1]["weight"] = 5 >>> G.edges[2, 3]["weight"] = 3
>>> nx.average_neighbor_degree(G) {0: 2.0, 1: 1.5, 2: 1.5, 3: 2.0} >>> nx.average_neighbor_degree(G, weight="weight") {0: 2.0, 1: 1.1666666666666667, 2: 1.25, 3: 2.0}
>>> G = nx.DiGraph() >>> nx.add_path(G, [0, 1, 2, 3]) >>> nx.average_neighbor_degree(G, source="in", target="in") {0: 0.0, 1: 0.0, 2: 1.0, 3: 1.0}
>>> nx.average_neighbor_degree(G, source="out", target="out") {0: 1.0, 1: 1.0, 2: 0.0, 3: 0.0}