ó
ú£Õ\c           @   sq   d  Z  d Z d d g Z d d l Z d d l Z d d l Te d e d d	 „ Z	 e d e d d
 „ Z
 d „  Z d S(   sD   
Current-flow betweenness centrality measures for subsets of nodes.
s   Aric Hagberg (hagberg@lanl.gov)t*   current_flow_betweenness_centrality_subsett/   edge_current_flow_betweenness_centrality_subsetiÿÿÿÿN(   t   *t   weightt   luc            sB  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 t ˆ  t |
 ƒ ƒ ƒ } t j |  | ƒ } t j | d
 ƒ } xº t | d | d | d | ƒD]š \ } \ } } x… | D]} } | | } xj | D]b } | | } | | c d | j | | | | ƒ 7<| | c d | j | | | | ƒ 7<qdWqMWq4W| rí|
 d |
 d } n d } x, | D]$ } | | | d d |
 | | <qúWt ‡  f d †  | j ƒ  Dƒ ƒ S(   s
  Compute current-flow betweenness centrality for subsets of 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 

    sources: list of nodes
      Nodes to use as sources for current

    targets: list of nodes
      Nodes to use as sinks for current

    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ÿÿÿÿ(   t   reverse_cuthill_mckee_orderingNs3   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   t   dtypet   solverg      à?g      ð?g       @i   c         3   s%   |  ] \ } } ˆ  | | f Vq d  S(   N(    (   t   .0t   kt   v(   t   ordering(    sv   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/current_flow_betweenness_subset.pys	   <genexpr>‡   s    (   t   networkx.utilsR   t   numpyt   ImportErrort   scipyt   is_directedt   nxt   NetworkXErrort   is_connectedt   number_of_nodest   listt   dictt   zipt   ranget   relabel_nodest   fromkeyst   flow_matrix_rowt   abst   items(   t   Gt   sourcest   targetst
   normalizedR   R   R   R   t   npR   t   nt   mappingt   Ht   betweennesst   rowt   st   tt   sst   it   ttt   jt   nbR
   (    (   R   sv   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/current_flow_betweenness_subset.pyR       sF    N	

)5"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 t ˆ  t |
 ƒ ƒ ƒ } t j |  | ƒ } t j | j ƒ  d
 ƒ } | r6|
 d |
 d } n d } x› t | d | d | d | ƒD]{ \ } } x\ | D]T } | | } xA | D]9 } | | } | | c d | j | | | | ƒ 7<q…WqnW| | c | :<q[Wt ‡  f d †  | j ƒ  Dƒ ƒ S(   sê	  Compute current-flow betweenness centrality for edges using subsets 
    of 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 

    sources: list of nodes
      Nodes to use as sources for current

    targets: list of nodes
      Nodes to use as sinks for current

    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   R   g      à?c         3   s5   |  ]+ \ \ } } } ˆ  | ˆ  | f | f Vq d  S(   N(    (   R   R(   R)   R
   (   R   (    sv   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/current_flow_betweenness_subset.pys	   <genexpr>û   s   (   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   edgesR   R   R   (   R   R   R    R!   R   R   R   R   R"   R   R#   R$   R%   R&   R.   R'   t   eR*   R+   R,   R-   (    (   R   sv   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/current_flow_betweenness_subset.pyR   Š   sD    N	

1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   noseR1   R   R   (   t   moduleR1   R   R   (    (    sv   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/current_flow_betweenness_subset.pyt   setup_module   s    (   t   __doc__t
   __author__t   __all__t	   itertoolst   networkxR   t*   networkx.algorithms.centrality.flow_matrixt   Truet   floatR    R   R4   (    (    (    sv   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/current_flow_betweenness_subset.pyt   <module>   s   	
ts