
    wg'                        d Z ddlmZ ddlZg dZ ej                  d      dd       Z ej                  d      dd       Z ej                  d      dd	       Z	 ej                  d      dd
       Z
 ej                  d      dd       Z ej                  d      dd       Zej                  d        Zej                  d        Zy)z5Functions for finding and evaluating cuts in a graph.    )chainN)boundary_expansionconductancecut_sizeedge_expansionmixing_expansionnode_expansionnormalized_cut_sizevolumeweight)
edge_attrsc           
          t        j                  | |||d      }| j                         r$t        |t        j                  | |||d            }t	        d |D              S )a  Returns the size of the cut between two sets of nodes.

    A *cut* is a partition of the nodes of a graph into two sets. The
    *cut size* is the sum of the weights of the edges "between" the two
    sets of nodes.

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

    S : collection
        A collection of nodes in `G`.

    T : collection
        A collection of nodes in `G`. If not specified, this is taken to
        be the set complement of `S`.

    weight : object
        Edge attribute key to use as weight. If not specified, edges
        have weight one.

    Returns
    -------
    number
        Total weight of all edges from nodes in set `S` to nodes in
        set `T` (and, in the case of directed graphs, all edges from
        nodes in `T` to nodes in `S`).

    Examples
    --------
    In the graph with two cliques joined by a single edges, the natural
    bipartition of the graph into two blocks, one for each clique,
    yields a cut of weight one::

        >>> G = nx.barbell_graph(3, 0)
        >>> S = {0, 1, 2}
        >>> T = {3, 4, 5}
        >>> nx.cut_size(G, S, T)
        1

    Each parallel edge in a multigraph is counted when determining the
    cut size::

        >>> G = nx.MultiGraph(["ab", "ab"])
        >>> S = {"a"}
        >>> T = {"b"}
        >>> nx.cut_size(G, S, T)
        2

    Notes
    -----
    In a multigraph, the cut size is the total weight of edges including
    multiplicity.

       )datadefaultc              3   (   K   | ]
  \  }}}|  y wN ).0uvr   s       ]/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/cuts.py	<genexpr>zcut_size.<locals>.<genexpr>R   s     0,!Qv0s   )nxedge_boundaryis_directedr   sum)GSTr   edgess        r   r   r      sV    r Q161=E}}eR--aAFANO0%000    c                     | j                         r| j                  n| j                  }t        d  |||      D              S )a|  Returns the volume of a set of nodes.

    The *volume* of a set *S* is the sum of the (out-)degrees of nodes
    in *S* (taking into account parallel edges in multigraphs). [1]

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

    S : collection
        A collection of nodes in `G`.

    weight : object
        Edge attribute key to use as weight. If not specified, edges
        have weight one.

    Returns
    -------
    number
        The volume of the set of nodes represented by `S` in the graph
        `G`.

    See also
    --------
    conductance
    cut_size
    edge_expansion
    edge_boundary
    normalized_cut_size

    References
    ----------
    .. [1] David Gleich.
           *Hierarchical Directed Spectral Graph Partitioning*.
           <https://www.cs.purdue.edu/homes/dgleich/publications/Gleich%202005%20-%20hierarchical%20directed%20spectral.pdf>

    c              3   &   K   | ]	  \  }}|  y wr   r   )r   r   ds      r   r   zvolume.<locals>.<genexpr>}   s     6TQq6s   r   )r   
out_degreedegreer   )r   r   r   r(   s       r   r   r   U   s4    N ]]_Q\\!((F6VAf5666r"   c                     |t        |       t        |      z
  }t        | |||      }t        | ||      }t        | ||      }|d|z  d|z  z   z  S )a  Returns the normalized size of the cut between two sets of nodes.

    The *normalized cut size* is the cut size times the sum of the
    reciprocal sizes of the volumes of the two sets. [1]

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

    S : collection
        A collection of nodes in `G`.

    T : collection
        A collection of nodes in `G`.

    weight : object
        Edge attribute key to use as weight. If not specified, edges
        have weight one.

    Returns
    -------
    number
        The normalized cut size between the two sets `S` and `T`.

    Notes
    -----
    In a multigraph, the cut size is the total weight of edges including
    multiplicity.

    See also
    --------
    conductance
    cut_size
    edge_expansion
    volume

    References
    ----------
    .. [1] David Gleich.
           *Hierarchical Directed Spectral Graph Partitioning*.
           <https://www.cs.purdue.edu/homes/dgleich/publications/Gleich%202005%20-%20hierarchical%20directed%20spectral.pdf>

    r    r   r&   r   )setr   r   r   r   r    r   num_cut_edgesvolume_Svolume_Ts          r   r
   r
      s_    Z 	yFSVOQQv6Ma6*Ha6*HQ\a(l;<<r"   c                     |t        |       t        |      z
  }t        | |||      }t        | ||      }t        | ||      }|t        ||      z  S )ap  Returns the conductance of two sets of nodes.

    The *conductance* is the quotient of the cut size and the smaller of
    the volumes of the two sets. [1]

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

    S : collection
        A collection of nodes in `G`.

    T : collection
        A collection of nodes in `G`.

    weight : object
        Edge attribute key to use as weight. If not specified, edges
        have weight one.

    Returns
    -------
    number
        The conductance between the two sets `S` and `T`.

    See also
    --------
    cut_size
    edge_expansion
    normalized_cut_size
    volume

    References
    ----------
    .. [1] David Gleich.
           *Hierarchical Directed Spectral Graph Partitioning*.
           <https://www.cs.purdue.edu/homes/dgleich/publications/Gleich%202005%20-%20hierarchical%20directed%20spectral.pdf>

    r&   )r+   r   r   minr,   s          r   r   r      sY    P 	yFSVOQ1V4Ma6*Ha6*H3x222r"   c                     |t        |       t        |      z
  }t        | |||      }|t        t        |      t        |            z  S )a  Returns the edge expansion between two node sets.

    The *edge expansion* is the quotient of the cut size and the smaller
    of the cardinalities of the two sets. [1]

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

    S : collection
        A collection of nodes in `G`.

    T : collection
        A collection of nodes in `G`.

    weight : object
        Edge attribute key to use as weight. If not specified, edges
        have weight one.

    Returns
    -------
    number
        The edge expansion between the two sets `S` and `T`.

    See also
    --------
    boundary_expansion
    mixing_expansion
    node_expansion

    References
    ----------
    .. [1] Fan Chung.
           *Spectral Graph Theory*.
           (CBMS Regional Conference Series in Mathematics, No. 92),
           American Mathematical Society, 1997, ISBN 0-8218-0315-8
           <http://www.math.ucsd.edu/~fan/research/revised.html>

    r*   )r+   r   r1   len)r   r   r    r   r-   s        r   r   r      sE    R 	yFSVOQQv6M3s1vs1v...r"   c                 P    t        | |||      }| j                         }|d|z  z  S )us  Returns the mixing expansion between two node sets.

    The *mixing expansion* is the quotient of the cut size and twice the
    number of edges in the graph. [1]

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

    S : collection
        A collection of nodes in `G`.

    T : collection
        A collection of nodes in `G`.

    weight : object
        Edge attribute key to use as weight. If not specified, edges
        have weight one.

    Returns
    -------
    number
        The mixing expansion between the two sets `S` and `T`.

    See also
    --------
    boundary_expansion
    edge_expansion
    node_expansion

    References
    ----------
    .. [1] Vadhan, Salil P.
           "Pseudorandomness."
           *Foundations and Trends
           in Theoretical Computer Science* 7.1–3 (2011): 1–336.
           <https://doi.org/10.1561/0400000010>

    r*      )r   number_of_edges)r   r   r    r   r-   num_total_edgess         r   r   r     s3    R QQv6M'')OA/00r"   c                      t        t        j                   fd|D                    }t        |      t        |      z  S )u  Returns the node expansion of the set `S`.

    The *node expansion* is the quotient of the size of the node
    boundary of *S* and the cardinality of *S*. [1]

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

    S : collection
        A collection of nodes in `G`.

    Returns
    -------
    number
        The node expansion of the set `S`.

    See also
    --------
    boundary_expansion
    edge_expansion
    mixing_expansion

    References
    ----------
    .. [1] Vadhan, Salil P.
           "Pseudorandomness."
           *Foundations and Trends
           in Theoretical Computer Science* 7.1–3 (2011): 1–336.
           <https://doi.org/10.1561/0400000010>

    c              3   @   K   | ]  }j                  |        y wr   )	neighbors)r   r   r   s     r   r   z!node_expansion.<locals>.<genexpr>f  s     *Ea1;;q>*Es   )r+   r   from_iterabler3   )r   r   neighborhoods   `  r   r	   r	   D  s5    D u***E1*EEFL|s1v%%r"   c                 X    t        t        j                  | |            t        |      z  S )u  Returns the boundary expansion of the set `S`.

    The *boundary expansion* is the quotient of the size
    of the node boundary and the cardinality of *S*. [1]

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

    S : collection
        A collection of nodes in `G`.

    Returns
    -------
    number
        The boundary expansion of the set `S`.

    See also
    --------
    edge_expansion
    mixing_expansion
    node_expansion

    References
    ----------
    .. [1] Vadhan, Salil P.
           "Pseudorandomness."
           *Foundations and Trends in Theoretical Computer Science*
           7.1–3 (2011): 1–336.
           <https://doi.org/10.1561/0400000010>

    )r3   r   node_boundary)r   r   s     r   r   r   l  s&    D r1%&Q//r"   )NNr   )__doc__	itertoolsr   networkxr   __all___dispatchabler   r   r
   r   r   r   r	   r   r   r"   r   <module>rD      s   ;  	 X&;1 ';1| X&'7 ''7T X&1= '1=h X&,3 ',3^ X&+/ '+/\ X&*1 '*1^ "& "&N !0 !0r"   