
    wg                     r    d Z ddlZddlmZ dgZ ed       ed      ej                  d                      Zy)zProvides a function for computing the extendability of a graph which is
undirected, simple, connected and bipartite and contains at least one perfect matching.    N)not_implemented_formaximal_extendabilitydirected
multigraphc           
         t        j                  |       st        j                  d      t         j                  j	                  |       st        j                  d      t         j                  j                  |       \  }}t         j                  j                  |       }t        j                  | |      st        j                  d      ||j                         z  D cg c]	  }|||   f }}| j                  D cg c]!  \  }}||v r||f|v s
||v r
||f|vr||fn||f# }}}t        j                         }	|	j                  |        |	j                  |       t        j                  |	      st        j                  d      t        d      }
|D ]9  }|D ]2  }t        d t        j                   |	||      D              }|
|k  r|
n|}
4 ; |
S c c}w c c}}w )ux  Computes the extendability of a graph.

    The extendability of a graph is defined as the maximum $k$ for which `G`
    is $k$-extendable. Graph `G` is $k$-extendable if and only if `G` has a
    perfect matching and every set of $k$ independent edges can be extended
    to a perfect matching in `G`.

    Parameters
    ----------
    G : NetworkX Graph
        A fully-connected bipartite graph without self-loops

    Returns
    -------
    extendability : int

    Raises
    ------
    NetworkXError
       If the graph `G` is disconnected.
       If the graph `G` is not bipartite.
       If the graph `G` does not contain a perfect matching.
       If the residual graph of `G` is not strongly connected.

    Notes
    -----
    Definition:
    Let `G` be a simple, connected, undirected and bipartite graph with a perfect
    matching M and bipartition (U,V). The residual graph of `G`, denoted by $G_M$,
    is the graph obtained from G by directing the edges of M from V to U and the
    edges that do not belong to M from U to V.

    Lemma [1]_ :
    Let M be a perfect matching of `G`. `G` is $k$-extendable if and only if its residual
    graph $G_M$ is strongly connected and there are $k$ vertex-disjoint directed
    paths between every vertex of U and every vertex of V.

    Assuming that input graph `G` is undirected, simple, connected, bipartite and contains
    a perfect matching M, this function constructs the residual graph $G_M$ of G and
    returns the minimum value among the maximum vertex-disjoint directed paths between
    every vertex of U and every vertex of V in $G_M$. By combining the definitions
    and the lemma, this value represents the extendability of the graph `G`.

    Time complexity O($n^3$ $m^2$)) where $n$ is the number of vertices
    and $m$ is the number of edges.

    References
    ----------
    .. [1] "A polynomial algorithm for the extendability problem in bipartite graphs",
          J. Lakhal, L. Litzler, Information Processing Letters, 1998.
    .. [2] "On n-extendible graphs", M. D. Plummer, Discrete Mathematics, 31:201–210, 1980
          https://doi.org/10.1016/0012-365X(80)90037-0

    zGraph G is not connectedzGraph G is not bipartitez+Graph G does not contain a perfect matchingz1The residual graph of G is not strongly connectedinfc              3       K   | ]  }d   yw)   N ).0_s     p/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/bipartite/extendability.py	<genexpr>z(maximal_extendability.<locals>.<genexpr>g   s     P!APs   )nxis_connectedNetworkXError	bipartiteis_bipartitesetshopcroft_karp_matchingis_perfect_matchingkeysedgesDiGraphadd_nodes_fromadd_edges_fromis_strongly_connectedfloatsumnode_disjoint_paths)GUVmaximum_matchingnodepmxydirected_edges
residual_Gkuv	num_pathss                 r   r   r   
   s   t ??19::<<$$Q'9::<<QDAq||::1=!!!%56LMM 679I9N9N9P5P	QT4!$'
(	QB	Q
 GGAq 6q!flQAq6;KASTVWRXXN  Ja n-##J/RSS 	eA 2 	2APr'='=j!Q'OPPI]	A	22 H/ 
Rs    G?&G)__doc__networkxr   networkx.utilsr   __all___dispatchabler   r       r   <module>r5      sQ   [  ."
# Z \"\  # !\r4   