o
    3ήc                     @   s6   d dl mZ d dlZddgZ	dddZdddZdS )	    )defaultdictNaverage_degree_connectivityk_nearest_neighborsin+outc                    s~     r9|dvrtd|dvrtd j j jd} j j jd}|| }|| }|| }	|dk}
n|dksA|dkrFtd j} j} j}	d}
t	t
}t	t
||}| v rh|||fg}|D ]I\}||	}d	u rtd
d |D }n|
rt fdd|D }nt fdd|D }|  |d7  < ||  |7  < qjfdd| D S )u  Compute the average degree connectivity of graph.

    The average degree connectivity is the average nearest neighbor degree of
    nodes with degree k. For weighted graphs, an analogous measure can
    be computed using the weighted average neighbors degree defined in
    [1]_, for a node `i`, as

    .. math::

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

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

    Parameters
    ----------
    G : NetworkX graph

    source :  "in"|"out"|"in+out" (default:"in+out")
       Directed graphs only. Use "in"- or "out"-degree for source node.

    target : "in"|"out"|"in+out" (default:"in+out"
       Directed graphs only. Use "in"- or "out"-degree for target node.

    nodes : list or iterable (optional)
        Compute neighbor connectivity for these nodes. The default is all
        nodes.

    weight : string 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 degree k with the value of average connectivity.

    Raises
    ------
    NetworkXError
        If either `source` or `target` are not one of 'in',
        'out', or 'in+out'.
        If either `source` or `target` is passed for an undirected graph.

    Examples
    --------
    >>> G = nx.path_graph(4)
    >>> G.edges[1, 2]["weight"] = 3
    >>> nx.average_degree_connectivity(G)
    {1: 2.0, 2: 1.5}
    >>> nx.average_degree_connectivity(G, weight="weight")
    {1: 2.0, 2: 1.75}

    See Also
    --------
    average_neighbor_degree

    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).
    )inoutr   z.source must be one of "in", "out", or "in+out"z.target must be one of "in", "out", or "in+out")r   r   r   r   r   zBsource and target arguments are only supported for directed graphsFNc                 s   s    | ]\}}|V  qd S )N ).0ndr   r   U/tmp/pip-target-vg8gfxp4/lib/python/networkx/algorithms/assortativity/connectivity.py	<genexpr>o   s    z.average_degree_connectivity.<locals>.<genexpr>c                 3   s,    | ]\}} |   d | V  qdS    Ngetr	   nbrr   Gr
   weightr   r   r   r      * c                 3   s,    | ]\}}  |  d | V  qdS r   r   r   r   r   r   r   t   r   )r   c                    s.   i | ]\}}| | d kr|n| |  qS )r   r   )r	   kavg)dnormr   r   
<dictcomp>y   s   . z/average_degree_connectivity.<locals>.<dictcomp>)is_directednxNetworkXError
out_degree	in_degreedegree
successorspredecessors	neighborsr   intsumitems)r   sourcetargetnodesr   	directionneighbor_funcssource_degreetarget_degreer$   reversedsumsource_nodesr   nbrdegsr   )r   r   r
   r   r   r      sJ   D


c                 C   s,   ddl }d}|j|tdd t| ||||S )zCompute the average degree connectivity of graph.

    .. deprecated 2.6

      k_nearest_neighbors function is deprecated and will be removed in v3.0.
      Use `average_degree_connectivity` instead.
    r   Nzrk_nearest_neighbors function is deprecated and will be removed in v3.0.
Use `average_degree_connectivity` instead.   )
stacklevel)warningswarnDeprecationWarningr   )r   r(   r)   r*   r   r6   msgr   r   r   r   |   s
   )r   r   NN)collectionsr   networkxr   __all__r   r   r   r   r   r   <module>   s    
t