U
    ?hw                     @   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 |< qt|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   ]/var/www/html/venv/lib/python3.8/site-packages/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   