ó
ú£Õ\c           @   s   d  Z  d Z d d d g Z d d l Z d d l Z d d l Te d e d	 d
 d d „ Z	 e d e d	 d „ Z
 e d e d	 d „ Z d „  Z d S(   s/   
Current-flow betweenness centrality measures.
s   Aric Hagberg (hagberg@lanl.gov)t#   current_flow_betweenness_centralityt/   approximate_current_flow_betweenness_centralityt(   edge_current_flow_betweenness_centralityiÿÿÿÿN(   t   *t   weightt   lug      à?i'  c            sC  d d l  m } y d d l } Wn  t k
 rB t d d ƒ ‚ n Xy$ d d l m }	 d d l m }
 Wn  t k
 r‰ t d d ƒ ‚ n X|  j ƒ  r« t	 j
 d	 d
 ƒ ‚ n  t	 j |  ƒ sÌ t	 j
 d ƒ ‚ n  i t d 6t d 6t d 6} |  j ƒ  } t | |  ƒ ƒ ‰ t	 j |  t t ˆ t | ƒ ƒ ƒ ƒ } t | d t | ƒ d | d | d d ƒ} | | | d | ƒ} t j | d ƒ } | d | d } | | d | } d } | t | j | | d | j | ƒ ƒ ƒ } | | k rt	 j
 d | | f d ƒ ‚ n  | d | } xõ t | ƒ D]ç } t j t | ƒ d ƒ \ } } | j | d | ƒ} d | | <d | | <| j | ƒ } xˆ | D]€ } | | k s| | k r£qn  xY | | D]M } | | | j | d ƒ } | | c | | j | | | | ƒ | 7<q®WqWqW| rd ‰  n
 | d ‰  t ‡  ‡ f d †  | j  ƒ  Dƒ ƒ S(   sL  Compute the approximate current-flow betweenness centrality for nodes.

    Approximates the current-flow betweenness centrality within absolute
    error of epsilon with high probability [1]_.


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

    normalized : bool, optional (default=True)
      If True the betweenness values are normalized by b=b/(n-1)(n-2) where
      n is the number of nodes in G.

    weight : string or None, optional (default='weight')
      Key for edge data used as the edge weight.
      If None, then use 1 as each edge weight.

    dtype: data type (float)
      Default data type for internal matrices.
      Set to np.float32 for lower memory consumption.

    solver: string (default='lu')
       Type of linear solver to use for computing the flow matrix.
       Options are "full" (uses most memory), "lu" (recommended), and 
       "cg" (uses least memory).

    epsilon: float
        Absolute error tolerance.
       
    kmax: int
       Maximum number of sample node pairs to use for approximation.

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

    Notes
    -----
    The running time is `O((1/\epsilon^2)m{\sqrt k} \log n)` 
    and the space required is `O(m)` for n nodes and m edges.

    If the edges have a 'weight' attribute they will be used as 
    weights in this algorithm.  Unspecified weights are set to 1.

    References
    ----------
    .. [1] Centrality Measures Based on Current Flow. 
       Ulrik Brandes and Daniel Fleischer,
       Proc. 22nd Symp. Theoretical Aspects of Computer Science (STACS '05). 
       LNCS 3404, pp. 533-544. Springer-Verlag, 2005. 
       http://www.inf.uni-konstanz.de/algo/publications/bf-cmbcf-05.pdf
    iÿÿÿÿ(   t   reverse_cuthill_mckee_orderingNs3   current_flow_betweenness_centrality requires NumPy s   http://scipy.org/(   t   sparse(   t   linalgs3   current_flow_betweenness_centrality requires SciPy s&   current_flow_betweenness_centrality() s   not defined for digraphs.s   Graph not connected.t   fullR   t   cgt   nodelistR   t   dtypet   formatt   cscg        g      ð?g       @i   i   s#   Number random pairs k>kmax (%d>%d) s   Increase kmax or epsilonc         3   s/   |  ]% \ } } ˆ | t  | ˆ  ƒ f Vq d  S(   N(   t   float(   t   .0t   kt   v(   t   factort   ordering(    so   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/current_flow_betweenness.pys	   <genexpr>‰   s    (!   t   networkx.utilsR   t   numpyt   ImportErrort   scipyR   t   scipy.sparseR   t   is_directedt   nxt   NetworkXErrort   is_connectedt   FullInverseLaplaciant   SuperLUInverseLaplaciant   CGInverseLaplaciant   number_of_nodest   listt   relabel_nodest   dictt   zipt   ranget   laplacian_sparse_matrixt   fromkeyst   intt   ceilt   logt   randomt   samplet   zerost   solvet   gett   abst   items(   t   Gt
   normalizedR   R   t   solvert   epsilont   kmaxR   t   npR   R   t
   solvernamet   nt   Ht   Lt   Ct   betweennesst   nbt   cstart   lR   t   cstar2kt   it   st   tt   bt   pR   t   nbrt   w(    (   R   R   so   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/current_flow_betweenness.pyR      sf    ?	

'.

9	
c            sO  d d l  m } y d d l } Wn  t k
 rB t d d ƒ ‚ n Xy d d l } Wn  t k
 ru t d d ƒ ‚ n X|  j ƒ  r— t j d d ƒ ‚ n  t j |  ƒ s¸ t j d	 ƒ ‚ n  |  j	 ƒ  } t
 | |  ƒ ƒ ‰  t j |  t t ˆ  t | ƒ ƒ ƒ ƒ }	 t j |	 d
 ƒ }
 x¿ t |	 d | d | d | ƒD]Ÿ \ } \ } } t t | j ƒ  d d d … t | ƒ ƒ ƒ } x\ t | ƒ D]N } |
 | c | | | | | 7<|
 | c | | d | | | | 7<q{Wq.W| rì| d | d } n d } x: t |	 ƒ D], \ } } t |
 | | d | ƒ |
 | <qÿWt ‡  f d †  |
 j ƒ  Dƒ ƒ S(   sv	  Compute current-flow betweenness centrality for nodes.

    Current-flow betweenness centrality uses an electrical current
    model for information spreading in contrast to betweenness
    centrality which uses shortest paths.

    Current-flow betweenness centrality is also known as
    random-walk betweenness centrality [2]_.

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

    normalized : bool, optional (default=True)
      If True the betweenness values are normalized by b=b/(n-1)(n-2) where
      n is the number of nodes in G.

    weight : string or None, optional (default='weight')
      Key for edge data used as the edge weight.
      If None, then use 1 as each edge weight.

    dtype: data type (float)
      Default data type for internal matrices.
      Set to np.float32 for lower memory consumption.

    solver: string (default='lu')
       Type of linear solver to use for computing the flow matrix.
       Options are "full" (uses most memory), "lu" (recommended), and 
       "cg" (uses least memory).

    Returns
    -------
    nodes : dictionary
       Dictionary of nodes with betweenness centrality as the value.
        
    See Also
    --------
    approximate_current_flow_betweenness_centrality
    betweenness_centrality
    edge_betweenness_centrality
    edge_current_flow_betweenness_centrality

    Notes
    -----
    Current-flow betweenness can be computed in `O(I(n-1)+mn \log n)`
    time [1]_, where `I(n-1)` is the time needed to compute the 
    inverse Laplacian.  For a full matrix this is `O(n^3)` but using
    sparse methods you can achieve `O(nm{\sqrt k})` where `k` is the
    Laplacian matrix condition number.  

    The space required is `O(nw) where `w` is the width of the sparse
    Laplacian matrix.  Worse case is `w=n` for `O(n^2)`.

    If the edges have a 'weight' attribute they will be used as 
    weights in this algorithm.  Unspecified weights are set to 1.

    References
    ----------
    .. [1] Centrality Measures Based on Current Flow. 
       Ulrik Brandes and Daniel Fleischer,
       Proc. 22nd Symp. Theoretical Aspects of Computer Science (STACS '05). 
       LNCS 3404, pp. 533-544. Springer-Verlag, 2005. 
       http://www.inf.uni-konstanz.de/algo/publications/bf-cmbcf-05.pdf

    .. [2] A measure of betweenness centrality based on random walks,
       M. E. J. Newman, Social Networks 27, 39-54 (2005).
    iÿÿÿÿ(   R   Ns3   current_flow_betweenness_centrality requires NumPy s   http://scipy.org/s3   current_flow_betweenness_centrality requires SciPy s&   current_flow_betweenness_centrality() s   not defined for digraphs.s   Graph not connected.g        R   R   R5   i   g      ð?g       @c         3   s%   |  ] \ } } ˆ  | | f Vq d  S(   N(    (   R   R   R   (   R   (    so   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/current_flow_betweenness.pys	   <genexpr>ô   s    (   R   R   R   R   R   R   R   R   R   R!   R"   R#   R$   R%   R&   R(   t   flow_matrix_rowt   argsortt	   enumerateR   R2   (   R3   R4   R   R   R5   R   R8   R   R:   R;   R>   t   rowRD   RE   t   posRC   R?   R   (    (   R   so   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/current_flow_betweenness.pyR    Œ   s@    F	'. 0$c            s)  d d l  m } y d d l } Wn  t k
 rB t d d ƒ ‚ n Xy d d l } Wn  t k
 ru t d d ƒ ‚ n X|  j ƒ  r— t j d d ƒ ‚ n  t j |  ƒ s¸ t j d	 ƒ ‚ n  |  j	 ƒ  } t
 | |  ƒ ƒ ‰  t j |  t t ˆ  t | ƒ ƒ ƒ ƒ }	 t j |	 j ƒ  d
 ƒ }
 | r0| d | d } n d } xÐ t |	 d | d | d | ƒD]° \ } } t t | j ƒ  d d d … t d | d ƒ ƒ ƒ } x\ t | ƒ D]N } |
 | c | d | | | | 7<|
 | c | | | | | | 7<q£W|
 | c | :<qUWt ‡  f d †  |
 j ƒ  Dƒ ƒ S(   sD	  Compute current-flow betweenness centrality for edges.

    Current-flow betweenness centrality uses an electrical current
    model for information spreading in contrast to betweenness
    centrality which uses shortest paths.

    Current-flow betweenness centrality is also known as
    random-walk betweenness centrality [2]_.

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

    normalized : bool, optional (default=True)
      If True the betweenness values are normalized by b=b/(n-1)(n-2) where
      n is the number of nodes in G.

    weight : string or None, optional (default='weight')
      Key for edge data used as the edge weight.
      If None, then use 1 as each edge weight.

    dtype: data type (float)
      Default data type for internal matrices.
      Set to np.float32 for lower memory consumption.

    solver: string (default='lu')
       Type of linear solver to use for computing the flow matrix.
       Options are "full" (uses most memory), "lu" (recommended), and 
       "cg" (uses least memory).

    Returns
    -------
    nodes : dictionary
       Dictionary of edge tuples with betweenness centrality as the value.
        
    See Also
    --------
    betweenness_centrality
    edge_betweenness_centrality
    current_flow_betweenness_centrality

    Notes
    -----
    Current-flow betweenness can be computed in `O(I(n-1)+mn \log n)`
    time [1]_, where `I(n-1)` is the time needed to compute the 
    inverse Laplacian.  For a full matrix this is `O(n^3)` but using
    sparse methods you can achieve `O(nm{\sqrt k})` where `k` is the
    Laplacian matrix condition number.  

    The space required is `O(nw) where `w` is the width of the sparse
    Laplacian matrix.  Worse case is `w=n` for `O(n^2)`.

    If the edges have a 'weight' attribute they will be used as 
    weights in this algorithm.  Unspecified weights are set to 1.

    References
    ----------
    .. [1] Centrality Measures Based on Current Flow. 
       Ulrik Brandes and Daniel Fleischer,
       Proc. 22nd Symp. Theoretical Aspects of Computer Science (STACS '05). 
       LNCS 3404, pp. 533-544. Springer-Verlag, 2005. 
       http://www.inf.uni-konstanz.de/algo/publications/bf-cmbcf-05.pdf

    .. [2] A measure of betweenness centrality based on random walks, 
       M. E. J. Newman, Social Networks 27, 39-54 (2005).
    iÿÿÿÿ(   R   Ns3   current_flow_betweenness_centrality requires NumPy s   http://scipy.org/s3   current_flow_betweenness_centrality requires SciPy s)   edge_current_flow_betweenness_centrality s   not defined for digraphs.s   Graph not connected.g        g      ð?g       @R   R   R5   i   c         3   s;   |  ]1 \ \ } } } ˆ  | ˆ  | f t  | ƒ f Vq d  S(   N(   R   (   R   RD   RE   R   (   R   (    so   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/current_flow_betweenness.pys	   <genexpr>^  s   (   R   R   R   R   R   R   R   R   R   R!   R"   R#   R$   R%   R&   R(   t   edgesRJ   RK   R2   (   R3   R4   R   R   R5   R   R8   R   R:   R;   R>   R?   RM   t   eRN   RC   (    (   R   so   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/current_flow_betweenness.pyR   ÷   s@    F	'5$(c         C   sF   d d l  m } y d d  l } d d  l } Wn | d ƒ ‚ n Xd  S(   Niÿÿÿÿ(   t   SkipTests   NumPy not available(   t   noseRQ   R   R   (   t   moduleRQ   R   R   (    (    so   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/current_flow_betweenness.pyt   setup_modulec  s    (   t   __doc__t
   __author__t   __all__R,   t   networkxR   t*   networkx.algorithms.centrality.flow_matrixt   TrueR   R   R    R   RT   (    (    (    so   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/current_flow_betweenness.pyt   <module>   s"   	
ujj