
    h                     >    S r SSKrS/r\R                  SS j5       rg)z@Algorithm to select influential nodes in a graph using VoteRank.    Nvoterankc                   ^ / n0 m[        U 5      S:X  a  U$ Ub  U[        U 5      :  a  [        U 5      nU R                  5       (       a-  [        S U R                  5        5       5      [        U 5      -  nO,[        S U R	                  5        5       5      [        U 5      -  nU R                  5        H
  nSS/TU'   M     [        U5       GH  nU R                  5        H  nSTU   S'   M     U R                  5        HH  u  pFTU   S==   TU   S   -  ss'   U R                  5       (       a  M2  TU   S==   TU   S   -  ss'   MJ     U H  nSTU   S'   M     [        U R
                  U4S jS9nTU   S   S:X  a  Us  $ UR                  U5        SS/TU'   U R                  U5       H0  u  pVTU   S==   SU-  -  ss'   [        TU   S   S5      TU   S'   M2     GM     U$ )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-neighbors
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.
r   c              3   *   #    U  H	  u  pUv   M     g 7fN .0_degs      ]/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/centrality/voterank_alg.py	<genexpr>voterank.<locals>.<genexpr>@   s     9..   c              3   *   #    U  H	  u  pUv   M     g 7fr   r   r   s      r   r   r   C   s     5**r      c                    > TU    S   $ )Nr   r   )x	vote_ranks    r   <lambda>voterank.<locals>.<lambda>U   s    y|A    )key)
lenis_directedsum
out_degreedegreenodesrangeedgesmaxappend)Gnumber_of_nodesinfluential_nodes	avgDegreenr
   nbrr   s          @r   r   r      s   ` I
1v{  /CF":a&}}9!,,.99CFB	 5!((*55A>	WWY1v	!  ?#AIaLO  ggiFAaLOy~a00O==??#q!Yq\!_4!	  
 #AIaLO # 67Q<?a$$  #1v	!ggajFAcN1Y. #IcN1$5q 9IcN1 !) $. r   r   )__doc__networkxnx__all___dispatchabler   r   r   r   <module>r.      s.    F , V Vr   