o
    3ήc(                     @   sh   d Z ddlZddlmZ ddlmZmZ ddgZeddde	d	fd
dZ
eddde	d	fddZdS )zBCurrent-flow betweenness centrality measures for subsets of nodes.    N)flow_matrix_row)not_implemented_forreverse_cuthill_mckee_ordering*current_flow_betweenness_centrality_subset/edge_current_flow_betweenness_centrality_subsetdirectedTluc                    sJ  ddl }ddlm} t| std|  }	t||  tt	 t
|	}
t| |
}t|d}t||||dD ]>\}\}}|D ]5}|
| }|D ],}|
| }||  d||| ||   7  < ||  d||| ||   7  < qKqCq;|r|	d |	d	  }nd	}|D ]}|| | dd
|	   ||< q fdd| D S )a-
  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=None)
      Key for edge data used as the edge weight.
      If None, then use 1 as each edge weight.
      The weight reflects the capacity or the strength of the
      edge.

    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.
       https://doi.org/10.1007/978-3-540-31856-9_44

    .. [2] A measure of betweenness centrality based on random walks,
       M. E. J. Newman, Social Networks 27, 39-54 (2005).
    r   N)r   Graph not connected.        weightdtypesolver      ?      ?       @   c                    s   i | ]	\}} | |qS  r   ).0kvorderingr   e/tmp/pip-target-vg8gfxp4/lib/python/networkx/algorithms/centrality/current_flow_betweenness_subset.py
<dictcomp>v   s    z>current_flow_betweenness_centrality_subset.<locals>.<dictcomp>)numpynetworkx.utilsr   nxis_connectedNetworkXErrornumber_of_nodeslistdictziprangerelabel_nodesfromkeysr   absitems)Gsourcestargets
normalizedr   r   r   npr   nmappingHbetweennessrowstssittjnbr   r   r   r   r      s0   P

&(c                    s  ddl }t| std|  }tt|  tt t	|}	t
| |	}
dd |
 D }t|d}|rA|d |d  }nd}t|
|||d	D ]1\}}|D ]"}|	| }|D ]}|	| }||  d
||| ||   7  < qYqQ||  |  < qK fdd| D S )a
  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=None)
      Key for edge data used as the edge weight.
      If None, then use 1 as each edge weight.
      The weight reflects the capacity or the strength of the
      edge.

    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 : dict
       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.
       https://doi.org/10.1007/978-3-540-31856-9_44

    .. [2] A measure of betweenness centrality based on random walks,
       M. E. J. Newman, Social Networks 27, 39-54 (2005).
    r   Nr	   c                 s   s$    | ]\}}t t||fV  qd S )N)tuplesorted)r   ur   r   r   r   	<genexpr>   s   " zBedge_current_flow_betweenness_centrality_subset.<locals>.<genexpr>r
   r   r   r   r   c                    s&   i | ]\\}}} |  | f|qS r   r   )r   r3   r4   r   r   r   r   r      s   & zCedge_current_flow_betweenness_centrality_subset.<locals>.<dictcomp>)r   r   r   r   r    r!   r   r"   r#   r$   r%   edgesr&   r   r'   r(   )r)   r*   r+   r,   r   r   r   r-   r.   r/   r0   r>   r1   r9   r2   er5   r6   r7   r8   r   r   r   r   y   s*   P

()__doc__networkxr   *networkx.algorithms.centrality.flow_matrixr   r   r   r   __all__floatr   r   r   r   r   r   <module>   s    l