
    wg                         d Z ddlmZ ddlmZ ddlZddlmZm	Z	 ddgZ
 e	d       e	d	      ej                  dd
                     Z e	d      ej                  d               Zy)z"Functions related to graph covers.    )partial)chainN)arbitrary_elementnot_implemented_formin_edge_coveris_edge_coverdirected
multigraphc                 :   t        |       dk(  r
t               S t        j                  |       dkD  rt        j                  d      |t        t        j                  d      } ||       }	 t        |j                               }d}t        |       |D ch c]  \  }}|	 c}}z
  |D ch c]  \  }}|	 c}}z
  }|D ]9  }t        | |         }|j                  ||f       |s'|j                  ||f       ; |S # t        $ r |}d}Y w xY wc c}}w c c}}w )a	  Returns the min cardinality edge cover of the graph as a set of edges.

    A smallest edge cover can be found in polynomial time by finding
    a maximum matching and extending it greedily so that all nodes
    are covered. This function follows that process. A maximum matching
    algorithm can be specified for the first step of the algorithm.
    The resulting set may return a set with one 2-tuple for each edge,
    (the usual case) or with both 2-tuples `(u, v)` and `(v, u)` for
    each edge. The latter is only done when a bipartite matching algorithm
    is specified as `matching_algorithm`.

    Parameters
    ----------
    G : NetworkX graph
        An undirected graph.

    matching_algorithm : function
        A function that returns a maximum cardinality matching for `G`.
        The function must take one input, the graph `G`, and return
        either a set of edges (with only one direction for the pair of nodes)
        or a dictionary mapping each node to its mate. If not specified,
        :func:`~networkx.algorithms.matching.max_weight_matching` is used.
        Common bipartite matching functions include
        :func:`~networkx.algorithms.bipartite.matching.hopcroft_karp_matching`
        or
        :func:`~networkx.algorithms.bipartite.matching.eppstein_matching`.

    Returns
    -------
    min_cover : set

        A set of the edges in a minimum edge cover in the form of tuples.
        It contains only one of the equivalent 2-tuples `(u, v)` and `(v, u)`
        for each edge. If a bipartite method is used to compute the matching,
        the returned set contains both the 2-tuples `(u, v)` and `(v, u)`
        for each edge of a minimum edge cover.

    Examples
    --------
    >>> G = nx.Graph([(0, 1), (0, 2), (0, 3), (1, 2), (1, 3)])
    >>> sorted(nx.min_edge_cover(G))
    [(2, 1), (3, 0)]

    Notes
    -----
    An edge cover of a graph is a set of edges such that every node of
    the graph is incident to at least one edge of the set.
    The minimum edge cover is an edge covering of smallest cardinality.

    Due to its implementation, the worst-case running time of this algorithm
    is bounded by the worst-case running time of the function
    ``matching_algorithm``.

    Minimum edge cover for `G` can also be found using the `min_edge_covering`
    function in :mod:`networkx.algorithms.bipartite.covering` which is
    simply this function with a default matching algorithm of
    :func:`~networkx.algorithms.bipartite.matching.hopcraft_karp_matching`
    r   zFGraph has a node with no edge incident on it, so no edge cover exists.T)maxcardinalityF)lensetnxnumber_of_isolatesNetworkXExceptionr   max_weight_matchingitemsAttributeErrorr   add)Gmatching_algorithmmaximum_matching	min_coverbipartite_coveruvuncovered_nodess           a/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/covering.pyr   r      s#   | 1v{u	Q!#""T
 	
 !$R%;%;DQ)!, (..01	
 !fi8da88);T$!QA;TTO 
" ad#q!fMM1a&!
" !   $	   9;Ts   ,C> D,D>DDc                 V    t        |       t        t        j                  |            k  S )a)  Decides whether a set of edges is a valid edge cover of the graph.

    Given a set of edges, whether it is an edge covering can
    be decided if we just check whether all nodes of the graph
    has an edge from the set, incident on it.

    Parameters
    ----------
    G : NetworkX graph
        An undirected bipartite graph.

    cover : set
        Set of edges to be checked.

    Returns
    -------
    bool
        Whether the set of edges is a valid edge cover of the graph.

    Examples
    --------
    >>> G = nx.Graph([(0, 1), (0, 2), (0, 3), (1, 2), (1, 3)])
    >>> cover = {(2, 1), (3, 0)}
    >>> nx.is_edge_cover(G, cover)
    True

    Notes
    -----
    An edge cover of a graph is a set of edges such that every node of
    the graph is incident to at least one edge of the set.
    )r   r   from_iterable)r   covers     r   r   r   l   s$    D q6S,,U3444    )N)__doc__	functoolsr   	itertoolsr   networkxr   networkx.utilsr   r   __all___dispatchabler   r    r"   r   <module>r+      s|    (    A_
- Z \"Z  # !Zz Z  5  ! 5r"   