U
    td                     @   s"   d Z ddlmZ G dd dZdS )z)Classes representing matchings on graphs.    )Vertexc                   @   s   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Ze	dd Z
dd Zdd Zdd Ze	dd Zejdd Ze	dd Zejdd ZdS )Matchinga|  A matching of vertices in a graph.

    A matching of an undirected graph is a set of edges such that each
    vertex is incident on at most one matched edge. When each vertex is
    incident on I{exactly} one matched edge, the matching called
    I{perfect}. This class is used in C{igraph} to represent non-perfect
    and perfect matchings in undirected graphs.

    This class is usually not instantiated directly, everything
    is taken care of by the functions that return matchings.

    Examples:

      >>> from igraph import Graph
      >>> g = Graph.Famous("noperfectmatching")
      >>> matching = g.maximum_matching()
    Nc                 C   s<   || _ d| _d| _d| _t|tr,|j| }|| _|| _dS )aa  Initializes the matching.

        @param graph: the graph the matching belongs to
        @param matching: a numeric vector where element I{i} corresponds to
          vertex I{i} of the graph. Element I{i} is -1 or if the corresponding
          vertex is unmatched, otherwise it contains the index of the vertex to
          which vertex I{i} is matched.
        @param types: the types of the vertices if the graph is bipartite.
          It must either be the name of a vertex attribute (which will be
          retrieved for all vertices) or a list. Elements in the list will be
          converted to boolean values C{True} or C{False}, and this will
          determine which part of the bipartite graph a given vertex belongs to.
        @raise ValueError: if the matching vector supplied does not describe
          a valid matching of the graph.
        Nr   )	_graph	_matching_num_matched_types
isinstancestrvstypesmatching)selfgraphr   r    r   H/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/igraph/matching.py__init__   s    

zMatching.__init__c                 C   s   | j S )N)r   r   r   r   r   __len__6   s    zMatching.__len__c                 C   s>   | j d k	r$d| jj| j| j| j f S d| jj| j| jf S d S )Nz%s(%r,%r,types=%r)z	%s(%r,%r))r   	__class____name__r   r   r   r   r   r   __repr__9   s    
zMatching.__repr__c                 C   s&   | j d k	rdt|  S dt|  S d S )Nz2Bipartite graph matching (%d matched vertex pairs)z(Graph matching (%d matched vertex pairs))r   lenr   r   r   r   __str__D   s    
zMatching.__str__c                    s,   | j j  fddt| jD }| j j| S )zReturns an edge sequence that contains the edges in the matching.

        If there are multiple edges between a pair of matched vertices, only one
        of them will be returned.
        c                    s.   g | ]&\}}|d kr||kr ||ddqS )F)Zdirectedr   ).0uvget_eidr   r   
<listcomp>Q   s    z"Matching.edges.<locals>.<listcomp>)r   r   	enumerater   es)r   Zeidxsr   r   r   edgesJ   s
    
zMatching.edgesc                 C   s   | j S )z0Returns the graph corresponding to the matching.)r   r   r   r   r   r   X   s    zMatching.graphc                 C   s   | j j| j| jdS )zReturns whether the matching is maximal.

        A matching is maximal when it is not possible to extend it any more
        with extra edges; in other words, unmatched vertices in the graph
        must be adjacent to matched vertices only.
        r   )r   Z_is_maximal_matchingr   r   r   r   r   r   
is_maximal]   s    zMatching.is_maximalc                 C   s   t |tr|j}| j| dkS )z;Returns whether the given vertex is matched to another one.r   )r   r   indexr   )r   vertexr   r   r   
is_matchedf   s    
zMatching.is_matchedc                 C   sH   t |tr.| j|j }|dk r"dS | jj| S | j| }|dk rDdS |S )a  Returns the vertex a given vertex is matched to.

        @param vertex: the vertex we are interested in; either an integer index
          or an instance of L{Vertex}.
        @return: the index of the vertex matched to the given vertex, either as
          an integer index (if I{vertex} was integer) or as an instance of
          L{Vertex}. When the vertex is unmatched, returns C{None}.
        r   N)r   r   r   r%   r   r
   )r   r&   Zmatchedr   r   r   match_ofl   s    	

zMatching.match_ofc                 C   s   | j S )zReturns the matching vector where element I{i} contains the ID of
        the vertex that vertex I{i} is matched to.

        The matching vector will contain C{-1} for unmatched vertices.
        )r   r   r   r   r   r      s    zMatching.matchingc                 C   sB   | j j|| jdstdt|| _tdd | jD d | _dS )aQ  Sets the matching vector.

        @param value: the matching vector which must contain the ID of the
          vertex matching vertex I{i} at the I{i}th position, or C{-1} if
          the vertex is unmatched.
        @raise ValueError: if the matching vector supplied does not describe
          a valid matching of the graph.
        r#   znot a valid matchingc                 s   s   | ]}|d krdV  qdS )r      Nr   )r   ir   r   r   	<genexpr>   s      z$Matching.matching.<locals>.<genexpr>   N)r   Z_is_matchingr   
ValueErrorlistr   sumr   )r   valuer   r   r   r      s    

c                 C   s   | j S )a  Returns the type vector if the graph is bipartite.

        Element I{i} of the type vector will be C{False} or C{True} depending
        on which side of the bipartite graph vertex I{i} belongs to.

        For non-bipartite graphs, this property returns C{None}.
        )r   r   r   r   r   r      s    	zMatching.typesc                 C   s2   dd |D }t || j k r(td|| _d S )Nc                 S   s   g | ]}t |qS r   )bool)r   xr   r   r   r      s     z"Matching.types.<locals>.<listcomp>ztype vector too short)r   r   Zvcountr-   r   )r   r0   r   r   r   r   r      s    )N)r   
__module____qualname____doc__r   r   r   r   r"   propertyr   r$   r'   r(   r   setterr   r   r   r   r   r      s&   

	



r   N)r5   Zigraph._igraphr   r   r   r   r   r   <module>   s   