U
    Ñtœd”  ã                   @   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 )z¾Returns 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 )zûReturns 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 )zÅReturns 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   