o
    3ήcx)                     @   sd   d Z ddlZddlZddlmZ dd ZG dd dZG dd	 d	Z	G d
d dZ
G dd dZdS )z-
    Tests for ISMAGS isomorphism algorithm.
    N)isomorphismc                 C   s   t tdd | S )zq
    Helper function to facilitate comparing collections of dictionaries in
    which order does not matter.
    c                 S   s   t |  S N)	frozensetitems)m r   X/tmp/pip-target-vg8gfxp4/lib/python/networkx/algorithms/isomorphism/tests/test_ismags.py<lambda>   s    z"_matches_to_sets.<locals>.<lambda>)setmap)matchesr   r   r   _matches_to_sets   s   r   c                	   @   s   e Zd Zdeddfdeddfdeddfdeddfdeddfd	eddfgg d
fedd	g dfg g dfg g dfgZdd Zdd Zdd ZdS )TestSelfIsomorphismr   a)name      b         )r   r   r   r   r   r   r   r   r   r   )r   r   r   )r   r   )r   r   )r   r   r   r   r   )r   r   )r      r      )r      )r    	   )r   
   )r"      )r   r   )r   r   r   r   r   )r   r   c                 C      | j D ]=\}}t }|| || tj||tddd}| s'J |	 s-J t
|jdddd |jD gks@J qdS )z
        For some small, symmetric graphs, make sure that 1) they are isomorphic
        to themselves, and 2) that only the identity mapping is found.
        r   N
node_matchTsymmetryc                 S      i | ]}||qS r   r   .0nr   r   r   
<dictcomp>E       z=TestSelfIsomorphism.test_self_isomorphism.<locals>.<dictcomp>datanxGraphadd_nodes_fromadd_edges_fromisoISMAGScategorical_node_matchis_isomorphicsubgraph_is_isomorphiclistsubgraph_isomorphisms_iternodesself	node_data	edge_datagraphismagsr   r   r   test_self_isomorphism5   s   

z)TestSelfIsomorphism.test_self_isomorphismc                 C   s   t  }t |td |ddg t||}t|d}|dd |j	D gks-J t 
|ddd	d
dddd}t||}t|d}|dd |j	D gksUJ dS )zn
        This edgecase is one of the cases in which it is hard to find all
        symmetry elements.
        r   )r   r   r   r   Tc                 S   r*   r   r   r+   r   r   r   r.   S   r/   zFTestSelfIsomorphism.test_edgecase_self_isomorphism.<locals>.<dictcomp>r   r   r   r   r   r   )r   r   r   r   r   r   r   c                 S   r*   r   r   r+   r   r   r   r.   X   r/   N)r2   r3   add_pathranger5   r6   r7   r;   find_isomorphismsr=   relabel_nodes)r?   rB   rC   ismags_answerr   r   r   test_edgecase_self_isomorphismH   s   z2TestSelfIsomorphism.test_edgecase_self_isomorphismc                 C   r%   )z
        For some small, directed, symmetric graphs, make sure that 1) they are
        isomorphic to themselves, and 2) that only the identity mapping is
        found.
        r   Nr&   Tr(   c                 S   r*   r   r   r+   r   r   r   r.   k   r/   zFTestSelfIsomorphism.test_directed_self_isomorphism.<locals>.<dictcomp>r0   r>   r   r   r   test_directed_self_isomorphismZ   s   

z2TestSelfIsomorphism.test_directed_self_isomorphismN)	__name__
__module____qualname__dictrG   r1   rD   rK   rL   r   r   r   r   r      s&    
!r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestSubgraphIsomorphismc                 C   s   t  }t |td t  }t |td |dd t|tddD  t||}t|j	dddd |j
D gksAJ d S )	Nr   c                 S   s   g | ]\}}||fqS r   r   )r,   r-   r   r   r   r   
<listcomp>v   s    z<TestSubgraphIsomorphism.test_isomorphism.<locals>.<listcomp>r    Tr(   c                 S   r*   r   r   r+   r   r   r   r.   y   r/   z<TestSubgraphIsomorphism.test_isomorphism.<locals>.<dictcomp>)r2   r3   	add_cyclerG   r5   zipr6   r7   r;   r<   r=   )r?   g1g2rC   r   r   r   test_isomorphismp   s    z(TestSubgraphIsomorphism.test_isomorphismc                 C   s   t  }t |td | }|dd t||}|jdd}dddddddddddd	g}t	|t	|ks=J |jd
d}dddddddddddd	g}t	|t	|| ks`J d S )Nr   r   Tr(   r   r   r   r   r   )r   r   r   )r   r   r   F)
r2   r3   rF   rG   copyadd_edger6   r7   r<   r   r?   rU   rV   rC   r   expected_symmetricexpected_asymmetricr   r   r   test_isomorphism2|   s&   





z)TestSubgraphIsomorphism.test_isomorphism2c                 C   s   t  }t |td d|jd d< | }|dd tj||dd d}|j	dd}d	dd
dg}t
|t
|ks>J |j	dd}d
dd	dg}t
|t
|| ksWJ d S )Nr   Tr   attrc                 S      | |kS r   r   xyr   r   r   r	          z<TestSubgraphIsomorphism.test_labeled_nodes.<locals>.<lambda>r&   r(   r   r   rX   F)r2   r3   rS   rG   r=   rY   rZ   r6   r7   r<   r   r[   r   r   r   test_labeled_nodes      z*TestSubgraphIsomorphism.test_labeled_nodesc                 C   s   t  }t |td d|jd d< | }|dd tj||dd d}|j	dd	}d
dddg}t
|t
|ks>J |j	dd	}dd
ddg}t
|t
|| ksWJ d S )Nr   Tr   r_   r   c                 S   r`   r   r   ra   r   r   r   r	      rd   z<TestSubgraphIsomorphism.test_labeled_edges.<locals>.<lambda>)
edge_matchr(   r   r   rX   F)r   r   r   )r2   r3   rS   rG   edgesrY   rZ   r6   r7   r<   r   r[   r   r   r   test_labeled_edges   rf   z*TestSubgraphIsomorphism.test_labeled_edgesN)rM   rN   rO   rW   r^   re   ri   r   r   r   r   rQ   o   s
    rQ   c                   @   s   e Zd ZddgddgddgddgddgddgddgddgddgddgddgddggZd	d
gd
dgddgdd	gddgddgddgddgd	dgd
dgddgddggZdd ZdS )TestWikipediaExampler   ghir   jcdr   r   r   r   r   r   r   r    c                 C   sD   t  }t  }|| j || j t||}| s J d S r   )r2   r3   r5   g1edgesg2edgesr6   r7   r9   )r?   rU   rV   gmr   r   r   
test_graph   s   zTestWikipediaExample.test_graphN)rM   rN   rO   rq   rr   rt   r   r   r   r   rj      s8    rj   c                   @   s   e Zd Zdd Zdd ZdS )TestLargestCommonSubgraphc                 C   s`  t  }|g d d|jd d< t  }|g d d|jd d< d|jd d< d|jd d< tj||tdd d	}t|d
g ksIJ t|dg ksTJ t	|
 }t	ddddddddddg}||kspJ tj||tdd d	}t|d
g ksJ t|dg ksJ t	|
 }t	ddddddddddg}||ksJ d S )N)r   r   r   r   r   r   r   color)r   r   r   r   r$   rE   )r   r   r   r   r   r   r&   TFr   r   r   )r   r   r   r   )r   r   r   r   )r2   r3   r5   r=   r6   r7   r8   r;   r<   r   largest_common_subgraph)r?   graph1graph2rC   
found_mcisexpectedr   r   r   	test_mcis   s<   z#TestLargestCommonSubgraph.test_mcisc           	      C   s  t  }t |td t  }t |td |dd tj||tdd d}t|	dg ks5J t
| }t
dddd	dddd
g}||ksOJ tj||tdd d}t|	dg ksfJ t
| }t
dddddddddddddddddddd	ddddg}||ksJ t
|d}|d}dd |D }t
|}t
dddddddddddd
dddddddddddddddddddddddd
dddd
dddd	dddd	g}||ksJ ||ksJ d S )Nr   r   r   rv   r&   Tr   r   rX   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   Fc                 S   s   g | ]}d d |  D qS )c                 S   s   i | ]\}}||qS r   r   )r,   kvr   r   r   r.   3  s    zKTestLargestCommonSubgraph.test_symmetry_mcis.<locals>.<listcomp>.<dictcomp>)r   )r,   rp   r   r   r   rR   3  s    z@TestLargestCommonSubgraph.test_symmetry_mcis.<locals>.<listcomp>)r   r   r   )r2   r3   rF   rG   rZ   r6   r7   r8   r;   r<   r   rw   )	r?   rx   ry   ismags1rz   r{   ismags2found_mcis1found_mcis2r   r   r   test_symmetry_mcis  s`   


















z,TestLargestCommonSubgraph.test_symmetry_mcisN)rM   rN   rO   r|   r   r   r   r   r   ru      s    %ru   )__doc__pytestnetworkxr2   networkx.algorithmsr   r6   r   r   rQ   rj   ru   r   r   r   r   <module>   s    \L,