
    VhH.              
       V   d Z ddlZddlmZmZmZmZ ddlm	Z	m
Z
mZ g dZ e	d       e
d       ej                  d	      d
deddddfd                     Z e	d       ej                  d	      d
dedfd              Z e	d       ej                  d	      d
dedfd              Zy)z-Current-flow betweenness centrality measures.    N)CGInverseLaplacianFullInverseLaplacianSuperLUInverseLaplacianflow_matrix_row)not_implemented_forpy_random_statereverse_cuthill_mckee_ordering)#current_flow_betweenness_centrality/approximate_current_flow_betweenness_centrality(edge_current_flow_betweenness_centralitydirected   weight)
edge_attrsTfullg      ?i'  c                    ddl }t        j                  |       st        j                  d      t        t
        t        d}	| j                         }
t        t        |             }t        j                  | t        t        |t        |
                        }t        j                  |t        |
      |      j                  d      }|j!                  |      } |	|   ||      }t        j#                  |d      }|
d	z
  |
d
z
  z  }|
|
dz
  z  |z  }d}|t%        |j'                  ||z  dz  |j)                  |
      z              z  }||kD  rd| d| d}t        j                  |d      |d|z  z  }t        |      D ]  }|j+                  t        |
      d      x\  }}}|j-                  |
|      }d||<   d||<   |j/                  |      }|D ]]  }||v r||   D ]N  }||   |   j1                  |d	      }||xx   t3        ||j5                  ||   ||   z
        z  |z        z  cc<   P _  |rd	}n|d
z  }|j7                         D ci c]  \  }}||   ||z   c}}S c c}}w )a  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 2/[(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='full')
       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.

    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    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] Ulrik Brandes and Daniel Fleischer:
       Centrality Measures Based on Current Flow.
       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
    r   NGraph not connected.)r   lucg)nodelistr   csc)dtype              ?       @      zNumber random pairs k>kmax (>z) zIncrease kmax or epsilon)numpynxis_connectedNetworkXErrorr   r   r   number_of_nodeslistr	   relabel_nodesdictziprangelaplacian_matrixasformatastypefromkeysintceillogsamplezerossolvegetfloatabsitems) G
normalizedr   r   solverepsilonkmaxseednp
solvernamenorderingHLCbetweennessnbcstarlkmsgcstar2k_stpairbpvnbrwfactors                                    g/home/dcms/DCMS/lib/python3.12/site-packages/networkx/algorithms/centrality/current_flow_betweenness.pyr   r      s~   \ ??1566$% J
 	
A2156H 	DXuQx!89:A
Aa@II%PA	A
61E*A--3'K
c'a#g	BQK"E	A	CQ.:;<<A4x,QCqb9s$>??q1uoG1X Mkk%(A..1tHHQeH$!!GGAJ 	MADyt MaDIMM&#.A%BFF1Q4!C&=,A(AG(K"LLM	MM c0;0A0A0CD1HQKV#DDDs   I(c                    t        j                  |       st        j                  d      | j                         }t	        t        |             }t        j                  | t        t        |t        |                        }t        j                  |d      }t        ||||      D ]  \  }	\  }
}t        t        |	j                         ddd   t        |                  }t        |      D ]R  }||
xx   |||   z
  |	j                  |      z  z  cc<   ||xx   ||z
  dz
  ||   z
  |	j                  |      z  z  cc<   T  |r|dz
  |dz
  z  }nd}|j                         D ci c]  \  }}||   ||z
  dz  |z   c}}S c c}}w )	a	  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 2/[(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='full')
       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   r   r   r   r:   Nr   r   r   r   )r!   r"   r#   r$   r%   r	   r&   r'   r(   r)   r-   r   argsortitemr7   )r8   r9   r   r   r:   NrA   rB   rE   rowrM   rN   posirF   r@   rP   s                    rV   r
   r
      st   V ??1566	A2156H 	DXuQx!89:A--3'K&quVT AVa3s{{}TrT*E!H56q 	AANq3q6zSXXa[88NNq1uqy3q61SXXa[@@N	AA
 #g!c'"8C8I8I8KL1HQK!a%3++LLLs   &Fc                 `   t        j                  |       st        j                  d      | j                         }t	        t        |             }t        j                  | t        t        |t        |                        }d |j                         D        }t        j                  |d      }	|r|dz
  |dz
  z  }
nd}
t        ||||      D ]  \  }}t        t        |j                         ddd   t        d	|d	z                     }t        |      D ]R  }|	|xx   |d	z   ||   z
  |j                  |      z  z  cc<   |	|xx   ||z
  ||   z
  |j                  |      z  z  cc<   T |	|xx   |
z  cc<    |	j                         D ci c]  \  \  }}}||   ||   f| c}}}S c c}}}w )
a(
  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 2/[(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 (default=float)
      Default data type for internal matrices.
      Set to np.float32 for lower memory consumption.

    solver : string (default='full')
       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.

    Raises
    ------
    NetworkXError
        The algorithm does not support DiGraphs.
        If the input graph is an instance of DiGraph class, NetworkXError
        is raised.

    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   c              3   N   K   | ]  \  }}t        t        ||f              y w)N)tuplesorted).0urR   s      rV   	<genexpr>z;edge_current_flow_betweenness_centrality.<locals>.<genexpr>J  s!     9tq!U61a&>"9s   #%r   r   r   rX   Nr   r   )r!   r"   r#   r$   r%   r	   r&   r'   r(   r)   edgesr-   r   rY   rZ   r7   )r8   r9   r   r   r:   r[   rA   rB   rf   rE   rF   r\   er]   r^   rM   rN   rP   s                     rV   r   r      s   b ??1566	A2156H 	DXuQx!89:A9qwwy9E--s+K#g!c'"#AfE&Q a3s{{}TrT*E!QUO<=q 	=ANq1us1v~!<<NNq1us1v~!<<N	= 	A" <G;L;L;NOOifq!aXa[(1+&)OOOs   F))__doc__networkxr!   *networkx.algorithms.centrality.flow_matrixr   r   r   r   networkx.utilsr   r   r	   __all___dispatchabler5   r   r
   r        rV   <module>rp      s    3    Z X& 
		vE '  !vEr Z X&t5ZM ' !ZMz Z X&t5bP ' !bPro   