ó
|£*^c           @   sí   d  Z  d d l m Z m Z d d l m Z d d l Z d d l Z d Z	 d d d g Z
 d e d e d d	 „ Z d e d d d
 „ Z d e d d d „ Z d „  Z d d „ Z d „  Z d „  Z d „  Z e d d „ Z e d d „ Z d S(   s"   
Betweenness centrality measures.
iÿÿÿÿ(   t   heappusht   heappop(   t   countNs   Aric Hagberg (hagberg@lanl.gov)t   betweenness_centralityt   edge_betweenness_centralityt   edge_betweennessc      	   C   s	  t  j |  d ƒ } | d k r' |  } n% t j | ƒ t j |  j ƒ  | ƒ } x‰ | D]� } | d k r€ t |  | ƒ \ }	 }
 } n t |  | | ƒ \ }	 }
 } | r¼ t	 | |	 |
 | | ƒ } qS t
 | |	 |
 | | ƒ } qS Wt | t |  ƒ d | d |  j ƒ  d | ƒ} | S(   s”  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
      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.

    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   kN(   t   dictt   fromkeyst   Nonet   randomt   seedt   samplet   nodest"   _single_source_shortest_path_basict"   _single_source_dijkstra_path_basict   _accumulate_endpointst   _accumulate_basict   _rescalet   lent   is_directed(   t   GR   R   t   weightt	   endpointsR   t   betweennessR   t   st   St   Pt   sigma(    (    s~   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/betweenness.pyR      s"    U		c         C   s  t  j |  d ƒ } | j t  j |  j ƒ  d ƒ ƒ | d k rF |  } n% t j | ƒ t j |  j ƒ  | ƒ } xh | D]` } | d k rŸ t	 |  | ƒ \ } }	 }
 n t
 |  | | ƒ \ } }	 }
 t | | |	 |
 | ƒ } qr 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
      If None, all edge weights are considered equal.
      Otherwise holds the name of the edge attribute used as weight.

    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   R   R   t   _accumulate_edgest
   _rescale_eR   R   (   R   R   R   R   R   R   R   R   R   R   R   t   n(    (    s~   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/betweenness.pyR   „   s"    >	c         C   s   t  |  | | | | ƒ S(   N(   R   (   R   R   R   R   R   (    (    s~   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/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(    (    s~   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/betweenness.pyR   ã   s,    

		

R   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(    (    s~   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/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    g      ð?(   R	   R
   R$   (	   R   R   R   R   R   t   deltaR+   t   coeffR&   (    (    s~   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/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    g      ð?(   R   R	   R
   R$   (	   R   R   R   R   R   R7   R+   R8   R&   (    (    s~   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/betweenness.pyR   +  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    g      ð?(   R	   R
   R$   (
   R   R   R   R   R   R7   R+   R8   R&   R1   (    (    s~   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/betweenness.pyR!   8  s    	c         C   s¤   | t  k r: | d k r! d  } qS d | d | d } n | sM d d } n d  } | d  k	 r  | d  k	 r| | | | } n  x! |  D] } |  | c | 9<qƒ Wn  |  S(   Ni   g      ð?i   g       @(   t   TrueR   (   R   R#   R   R   R   t   scaleR&   (    (    s~   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/betweenness.pyR   I  s    	c         C   s    | t  k r6 | d k r! d  } qO d | | d } n | sI d d } n d  } | d  k	 rœ | d  k	 rx | | | } n  x! |  D] } |  | c | 9<q Wn  |  S(   Ni   g      ð?g       @(   R9   R   (   R   R#   R   R   R   R:   R&   (    (    s~   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/betweenness.pyR"   \  s    	(   t   __doc__t   heapqR    R   t	   itertoolsR   t   networkxt   nxR   t
   __author__t   __all__R   R9   t   FalseR   R   R   R   R   R   R   R!   R   R"   (    (    (    s~   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/betweenness.pyt   <module>   s*   		l	X	#			