
\c           @   s   d  Z  d d l Z d d l Z d d l Z d Z d d d g Z d e d e	 d d  Z
 e d d  Z e d d	  Z d
   Z d d  Z d   Z d   Z d   Z e	 d d  Z e	 d  Z d S(   s"   
Betweenness centrality measures.
iNs   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 [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]  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
    .. [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
    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(    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/betweenness.pyR       s"    N		c   	      C   s   t  j |  d  } | j t  j |  j   d   xh |  D]` } | d k re t |  |  \ } } } n t |  | |  \ } } } t | | | | |  } q8 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(v) =\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 

    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   t   _accumulate_edgest
   _rescale_eR   R   (	   R   R   R   R   R   R   R   R   t   n(    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/betweenness.pyR   z   s    8c         C   s   t  |  | |  S(   N(   R   (   R   R   R   (    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/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(    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/betweenness.pyR      s,    

		

R   c         C   s  g  } i  } x |  D] } g  | | <q Wt  j |  d  } i  } d | | <t j } t j }	 i d | 6}
 g  } | | d | | f  x-| r|	 |  \ } } } | | k r q n  | | c | | 7<| j |  | | | <x |  | j   D] \ } } | | j | d  } | | k rt| |
 k s:| |
 | k  rt| |
 | <| | | | | f  d | | <| g | | <q | |
 | k r | | c | | 7<| | j |  q q Wq W| | | f S(   Ng        g      ?i    i   (   R   R   t   heapqt   heappusht   heappopR"   t   itemst   get(   R   R   R   R   R   R#   R   R$   t   pushR!   t   seenR%   t   distt   predR(   t   edgedatat   vw_dist(    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/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#   (    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/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   R4   R(   R5   R#   (    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/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   R4   R(   R5   R#   t   c(    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/betweenness.pyR     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#   (    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/betweenness.pyR   .  s    	c         C   s   | t  k r6 | d k r! d  } qO d | | d } n | sI d d } n d  } | d  k	 r x! |  D] } |  | c | 9<qb Wn  |  S(   Ni   g      ?g       @(   R7   R   (   R   R    R   R   R8   R#   (    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/betweenness.pyR   @  s    	(   t   __doc__R)   t   networkxt   nxR	   t
   __author__t   __all__R   R7   t   FalseR    R   R   R   R   R   R   R   R   R   (    (    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/betweenness.pyt   <module>   s&   		eL	!			