a
    h                     @   s:   d Z ddlZddlmZ dgZededdd ZdS )z Provides 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_extendabilityZdirectedZ
multigraphc              	      s
  t | st dt j| s*t dt j| \ t j| t | s\t dfdd @ D  fdd| j	D }t 
 }||  || t |st dtd} D ]:}D ]0}td	d
 t |||D }||k r|n|}qq|S )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 matchingc                    s   g | ]}| | fqS  r   ).0node)maximum_matchingr   Y/var/www/auris/lib/python3.9/site-packages/networkx/algorithms/bipartite/extendability.py
<listcomp>R       z)maximal_extendability.<locals>.<listcomp>c                    sH   g | ]@\}}|v r ||fv s4| v r<||fvr<||fn||fqS r   r   )r   xy)UVpmr   r   r	   U   s   z1The residual graph of G is not strongly connectedZInfc                 s   s   | ]
}d V  qdS )   Nr   )r   _r   r   r   	<genexpr>g   r
   z(maximal_extendability.<locals>.<genexpr>)nxZis_connectedZNetworkXErrorZ	bipartiteZis_bipartiteZsetsZhopcroft_karp_matchingZis_perfect_matchingkeysedgesZDiGraphZadd_nodes_fromZadd_edges_fromZis_strongly_connectedfloatsumZnode_disjoint_paths)GZdirected_edgesZ
residual_GkuvZ	num_pathsr   )r   r   r   r   r   r      s.    9







)__doc__Znetworkxr   Znetworkx.utilsr   __all__r   r   r   r   r   <module>   s   