ó
 ,ľ[c           @   s  d  Z  d d l m Z d d l m Z m Z d d l m Z d d l Z	 d d l
 m Z d d d	 g Z e d
  d e d e d d   Z e d  d e d d d   Z d e d d d  Z d   Z d   Z d   Z d   Z d   Z e d e d  Z e d d  Z d S(   s    Betweenness centrality measures.i˙˙˙˙(   t   division(   t   heappusht   heappop(   t   countN(   t   py_random_statet   betweenness_centralityt   edge_betweenness_centralityt   edge_betweennessi   c         C   s  t  j |  d  } | d k r' |  } n | j |  j   |  } x | D] } | d k rs t |  |  \ }	 }
 } n t |  | |  \ }	 }
 } | rŻ t | |	 |
 | |  } qF t | |	 |
 | |  } qF Wt	 | t
 |   d | d |  j   d | d | } | S(   sn  Compute the shortest-path betweenness centrality for nodes.

    Betweenness centrality of a node $v$ is the sum of the
    fraction of all-pairs shortest paths that pass through $v$

    .. math::

       c_B(v) =\sum_{s,t \in V} \frac{\sigma(s, t|v)}{\sigma(s, t)}

    where $V$ is the set of nodes, $\sigma(s, t)$ is the number of
    shortest $(s, t)$-paths,  and $\sigma(s, t|v)$ is the number of
    those paths  passing through some  node $v$ other than $s, t$.
    If $s = t$, $\sigma(s, t) = 1$, and if $v \in {s, t}$,
    $\sigma(s, t|v) = 0$ [2]_.

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

    k : int, optional (default=None)
      If k is not None use k node samples to estimate betweenness.
      The value of k <= n where n is the number of nodes in the graph.
      Higher values give better approximation.

    normalized : bool, optional
      If True the betweenness values are normalized by `2/((n-1)(n-2))`
      for graphs, and `1/((n-1)(n-2))` for directed graphs where `n`
      is the number of nodes in G.

    weight : None or string, optional (default=None)
      If None, all edge weights are considered equal.
      Otherwise holds the name of the edge attribute used as weight.

    endpoints : bool, optional
      If True include the endpoints in the shortest path counts.

    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.
        Note that this is only used if k is not None.

    Returns
    -------
    nodes : dictionary
       Dictionary of nodes with betweenness centrality as the value.

    See Also
    --------
    edge_betweenness_centrality
    load_centrality

    Notes
    -----
    The algorithm is from Ulrik Brandes [1]_.
    See [4]_ for the original first published version and [2]_ for details on
    algorithms for variations and related metrics.

    For approximate betweenness calculations set k=#samples to use
    k nodes ("pivots") to estimate the betweenness values. For an estimate
    of the number of pivots needed see [3]_.

    For weighted graphs the edge weights must be greater than zero.
    Zero edge weights can produce an infinite number of equal length
    paths between pairs of nodes.

    References
    ----------
    .. [1] Ulrik Brandes:
       A Faster Algorithm for Betweenness Centrality.
       Journal of Mathematical Sociology 25(2):163-177, 2001.
       http://www.inf.uni-konstanz.de/algo/publications/b-fabc-01.pdf
    .. [2] Ulrik Brandes:
       On Variants of Shortest-Path Betweenness
       Centrality and their Generic Computation.
       Social Networks 30(2):136-145, 2008.
       http://www.inf.uni-konstanz.de/algo/publications/b-vspbc-08.pdf
    .. [3] Ulrik Brandes and Christian Pich:
       Centrality Estimation in Large Networks.
       International Journal of Bifurcation and Chaos 17(7):2303-2318, 2007.
       http://www.inf.uni-konstanz.de/algo/publications/bp-celn-06.pdf
    .. [4] Linton C. Freeman:
       A set of measures of centrality based on betweenness.
       Sociometry 40: 35â41, 1977
       http://moreno.ss.uci.edu/23.pdf
    g        t
   normalizedt   directedt   kt	   endpointsN(   t   dictt   fromkeyst   Nonet   samplet   nodest"   _single_source_shortest_path_basict"   _single_source_dijkstra_path_basict   _accumulate_endpointst   _accumulate_basict   _rescalet   lent   is_directed(   t   GR
   R   t   weightR   t   seedt   betweennessR   t   st   St   Pt   sigma(    (    sI   lib/python2.7/site-packages/networkx/algorithms/centrality/betweenness.pyR      s    Y	i   c         C   s  t  j |  d  } | j t  j |  j   d   | d k rF |  } n | j |  j   |  } xh | D]` } | d k r t |  |  \ } }	 }
 n t |  | |  \ } }	 }
 t	 | | |	 |
 |  } qe Wx |  D] } | | =qĐ Wt
 | t |   d | d |  j   } | S(   s  Compute betweenness centrality for edges.

    Betweenness centrality of an edge $e$ is the sum of the
    fraction of all-pairs shortest paths that pass through $e$

    .. math::

       c_B(e) =\sum_{s,t \in V} \frac{\sigma(s, t|e)}{\sigma(s, t)}

    where $V$ is the set of nodes, $\sigma(s, t)$ is the number of
    shortest $(s, t)$-paths, and $\sigma(s, t|e)$ is the number of
    those paths passing through edge $e$ [2]_.

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

    k : int, optional (default=None)
      If k is not None use k node samples to estimate betweenness.
      The value of k <= n where n is the number of nodes in the graph.
      Higher values give better approximation.

    normalized : bool, optional
      If True the betweenness values are normalized by $2/(n(n-1))$
      for graphs, and $1/(n(n-1))$ for directed graphs where $n$
      is the number of nodes in G.

    weight : None or string, optional (default=None)
      If None, all edge weights are considered equal.
      Otherwise holds the name of the edge attribute used as weight.

    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.
        Note that this is only used if k is not None.

    Returns
    -------
    edges : dictionary
       Dictionary of edges with betweenness centrality as the value.

    See Also
    --------
    betweenness_centrality
    edge_load

    Notes
    -----
    The algorithm is from Ulrik Brandes [1]_.

    For weighted graphs the edge weights must be greater than zero.
    Zero edge weights can produce an infinite number of equal length
    paths between pairs of nodes.

    References
    ----------
    .. [1]  A Faster Algorithm for Betweenness Centrality. Ulrik Brandes,
       Journal of Mathematical Sociology 25(2):163-177, 2001.
       http://www.inf.uni-konstanz.de/algo/publications/b-fabc-01.pdf
    .. [2] Ulrik Brandes: On Variants of Shortest-Path Betweenness
       Centrality and their Generic Computation.
       Social Networks 30(2):136-145, 2008.
       http://www.inf.uni-konstanz.de/algo/publications/b-vspbc-08.pdf
    g        R   R	   N(   R   R   t   updatet   edgesR   R   R   R   R   t   _accumulate_edgest
   _rescale_eR   R   (   R   R
   R   R   R   R   R   R   R   R   R   t   n(    (    sI   lib/python2.7/site-packages/networkx/algorithms/centrality/betweenness.pyR      s    D	c         C   s   t  |  | | | |  S(   N(   R   (   R   R
   R   R   R   (    (    sI   lib/python2.7/site-packages/networkx/algorithms/centrality/betweenness.pyR   â   s    c         C   s  g  } i  } x |  D] } g  | | <q Wt  j |  d  } i  } d | | <d | | <| g } xą | r| j d  } | j |  | | } | | }	 xt |  | D]h }
 |
 | k rĐ | j |
  | d | |
 <n  | |
 | d k r  | |
 c |	 7<| |
 j |  q  q  Wq_ W| | | f S(   Ng        g      đ?i    i   (   R   R   t   popt   append(   R   R   R   R   t   vR   t   Dt   Qt   Dvt   sigmavt   w(    (    sI   lib/python2.7/site-packages/networkx/algorithms/centrality/betweenness.pyR   č   s,    

		

c         C   sŮ  g  } i  } x |  D] } g  | | <q Wt  j |  d  } i  } d | | <t } t }	 i d | 6}
 t   } g  } | | d t |  | | f  x9| rË|	 |  \ } } } } | | k rĂ q n  | | c | | 7<| j |  | | | <x× |  | j   D]Ĺ \ } } | | j | d  } | | k r| |
 k sI| |
 | k  r| |
 | <| | | t |  | | f  d | | <| g | | <q˙ | |
 | k r˙ | | c | | 7<| | j |  q˙ q˙ Wq W| | | f S(   Ng        g      đ?i    i   (	   R   R   R   R   R   t   nextR&   t   itemst   get(   R   R   R   R   R   R'   R   R(   t   pushR%   t   seent   cR)   t   distt   _t   predR,   t   edgedatat   vw_dist(    (    sI   lib/python2.7/site-packages/networkx/algorithms/centrality/betweenness.pyR     s>    
		
(

c   	      C   s   t  j | d  } x| | r | j   } d | | | | } x* | | D] } | | c | | | 7<qH W| | k r |  | c | | 7<q q W|  S(   Ni    i   (   R   R   R%   (	   R   R   R   R   R   t   deltaR,   t   coeffR'   (    (    sI   lib/python2.7/site-packages/networkx/algorithms/centrality/betweenness.pyR   $  s    	c   	      C   sł   |  | c t  |  d 7<t j | d  } x | rŽ | j   } d | | | | } x* | | D] } | | c | | | 7<qb W| | k r/ |  | c | | d 7<q/ q/ W|  S(   Ni   i    (   R   R   R   R%   (	   R   R   R   R   R   R8   R,   R9   R'   (    (    sI   lib/python2.7/site-packages/networkx/algorithms/centrality/betweenness.pyR   0  s    	c   
      C   sÜ   t  j | d  } xĂ | r× | j   } d | | | | } xq | | D]e } | | | }	 | | f |  k r |  | | f c |	 7<n |  | | f c |	 7<| | c |	 7<qH W| | k r |  | c | | 7<q q W|  S(   Ni    i   (   R   R   R%   (
   R   R   R   R   R   R8   R,   R9   R'   R2   (    (    sI   lib/python2.7/site-packages/networkx/algorithms/centrality/betweenness.pyR"   =  s    	c         C   sĘ   | rd | r6 | d k  r! d  } qa d | | d } qy | d k rK d  } qy d | d | d } n | ss d } n d  } | d  k	 rĆ | d  k	 r˘ | | | } n  x! |  D] } |  | c | 9<qŠ Wn  |  S(   Ni   i   g      ŕ?(   R   (   R   R$   R   R	   R
   R   t   scaleR'   (    (    sI   lib/python2.7/site-packages/networkx/algorithms/centrality/betweenness.pyR   N  s"    			c         C   s   | r0 | d k r d  } qE d | | d } n | s? d } n d  } | d  k	 r | d  k	 rn | | | } n  x! |  D] } |  | c | 9<qu Wn  |  S(   Ni   g      ŕ?(   R   (   R   R$   R   R	   R
   R:   R'   (    (    sI   lib/python2.7/site-packages/networkx/algorithms/centrality/betweenness.pyR#   h  s    		(   t   __doc__t
   __future__R    t   heapqR   R   t	   itertoolsR   t   networkxt   nxt   networkx.utilsR   t   __all__R   t   Truet   FalseR   R   R   R   R   R   R   R"   R   R#   (    (    (    sI   lib/python2.7/site-packages/networkx/algorithms/centrality/betweenness.pyt   <module>
   s*   			m		[		#			