a
    1b                     @   sj   d Z ddlZddlmZmZ ddlmZ ddlm	Z	 ddgZ
eded	dd
dZeddd ZdS )z# Functions related to graph covers.    N)not_implemented_forarbitrary_element)partial)chainmin_edge_coveris_edge_coverZdirectedZ
multigraphc                 C   s   t | dkrt d|du r.tt jdd}|| }zt| }W n ty\   |}Y n0 t| dd |D  dd |D  }|D ],}t| | }|	||f |	||f q|S )	a  Returns a set of edges which constitutes
    the minimum edge cover of the graph.

    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.

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

    matching_algorithm : function
        A function that returns a maximum cardinality matching in a
        given bipartite graph. The function must take one input, the
        graph ``G``, and return a dictionary mapping each node to its
        mate. If not specified,
        :func:`~networkx.algorithms.bipartite.matching.hopcroft_karp_matching`
        will be used. Other possibilities include
        :func:`~networkx.algorithms.bipartite.matching.eppstein_matching`,
        or matching algorithms in the
        :mod:`networkx.algorithms.matching` module.

    Returns
    -------
    min_cover : set

        It contains all the edges of minimum edge cover
        in form of tuples. It contains both the edges `(u, v)` and `(v, u)`
        for given nodes `u` and `v` among the edges of minimum edge cover.

    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 bipartite graph can also be found using the
    function present in :mod:`networkx.algorithms.bipartite.covering`
    r   zFGraph has a node with no edge incident on it, so no edge cover exists.NT)Zmaxcardinalityc                 S   s   h | ]\}}|qS  r   .0uvr   r   ;lib/python3.9/site-packages/networkx/algorithms/covering.py	<setcomp>K       z!min_edge_cover.<locals>.<setcomp>c                 S   s   h | ]\}}|qS r   r   r	   r   r   r   r   K   r   )
nxZnumber_of_isolatesZNetworkXExceptionr   Zmax_weight_matchingsetitemsAttributeErrorr   add)GZmatching_algorithmZmaximum_matchingZ	min_coverZuncovered_nodesr   r   r   r   r   r      s&    /
$c                 C   s   t | t t|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.

    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   Zcoverr   r   r   r   Y   s    )N)__doc__Znetworkxr   Znetworkx.utilsr   r   	functoolsr   	itertoolsr   __all__r   r   r   r   r   r   <module>   s   K