U
    A·fw  ã                   @   s   d Z dgZddd„ZdS )z@Algorithm to select influential nodes in a graph using VoteRank.ÚvoterankNc                    sº  g }i ‰ t | ƒdkr|S |dks,|t | ƒkr4t | ƒ}|  ¡ r\tdd„ |  ¡ D ƒƒt | ƒ }ntdd„ |  ¡ D ƒƒt | ƒ }|  ¡ D ]}ddgˆ |< q‚t|ƒD ]}|  ¡ D ]}dˆ | d< qª|  ¡ D ]H\}}ˆ | d  ˆ | d 7  < |  ¡ sÄˆ | d  ˆ | d 7  < qÄ|D ]}dˆ | d< qt| j‡ fdd„d	}ˆ | d dkrV|  S | 	|¡ ddgˆ |< |  |¡D ]<\}}ˆ | d  d| 8  < tˆ | d dƒˆ | d< qvqœ|S )
a´  Select a list of influential nodes in a graph using VoteRank algorithm

    VoteRank [1]_ computes a ranking of the nodes in a graph G based on a
    voting scheme. With VoteRank, all nodes vote for each of its in-neighbours
    and the node with the highest votes is elected iteratively. The voting
    ability of out-neighbors of elected nodes is decreased in subsequent turns.

    Parameters
    ----------
    G : graph
        A NetworkX graph.

    number_of_nodes : integer, optional
        Number of ranked nodes to extract (default all nodes).

    Returns
    -------
    voterank : list
        Ordered list of computed seeds.
        Only nodes with positive number of votes are returned.

    Examples
    --------
    >>> G = nx.Graph([(0, 1), (0, 2), (0, 3), (1, 4)])
    >>> nx.voterank(G)
    [0, 1]

    The algorithm can be used both for undirected and directed graphs.
    However, the directed version is different in two ways:
    (i) nodes only vote for their in-neighbors and
    (ii) only the voting ability of elected node and its out-neighbors are updated:

    >>> G = nx.DiGraph([(0, 1), (2, 1), (2, 3), (3, 4)])
    >>> nx.voterank(G)
    [2, 3]

    Notes
    -----
    Each edge is treated independently in case of multigraphs.

    References
    ----------
    .. [1] Zhang, J.-X. et al. (2016).
        Identifying a set of influential spreaders in complex networks.
        Sci. Rep. 6, 27823; doi: 10.1038/srep27823.
    é    Nc                 s   s   | ]\}}|V  qd S ©N© ©Ú.0Ú_Údegr   r   úO/tmp/pip-unpacked-wheel-0wvcsx6h/networkx/algorithms/centrality/voterank_alg.pyÚ	<genexpr>=   s     zvoterank.<locals>.<genexpr>c                 s   s   | ]\}}|V  qd S r   r   r   r   r   r	   r
   @   s     é   c                    s   ˆ |  d S )Nr   r   )Úx©Z	vote_rankr   r	   Ú<lambda>R   ó    zvoterank.<locals>.<lambda>)Úkey)
ÚlenZis_directedÚsumZ
out_degreeZdegreeZnodesÚrangeÚedgesÚmaxÚappend)ÚGZnumber_of_nodesZinfluential_nodesZ	avgDegreeÚnr   Znbrr   r   r	   r      s:    / 
 )N)Ú__doc__Ú__all__r   r   r   r   r	   Ú<module>   s   