
    Vh                     r    d Z ddlZddlZddgZ G d d      Z G d de      Z G d d      Z G d	 d
      Zy)a  
*************
VF2 Algorithm
*************

An implementation of VF2 algorithm for graph isomorphism testing.

The simplest interface to use this module is to call the
:func:`is_isomorphic <networkx.algorithms.isomorphism.is_isomorphic>`
function.

Introduction
------------

The GraphMatcher and DiGraphMatcher are responsible for matching
graphs or directed graphs in a predetermined manner.  This
usually means a check for an isomorphism, though other checks
are also possible.  For example, a subgraph of one graph
can be checked for isomorphism to a second graph.

Matching is done via syntactic feasibility. It is also possible
to check for semantic feasibility. Feasibility, then, is defined
as the logical AND of the two functions.

To include a semantic check, the (Di)GraphMatcher class should be
subclassed, and the
:meth:`semantic_feasibility <networkx.algorithms.isomorphism.GraphMatcher.semantic_feasibility>`
function should be redefined.  By default, the semantic feasibility function always
returns ``True``.  The effect of this is that semantics are not
considered in the matching of G1 and G2.

Examples
--------

Suppose G1 and G2 are isomorphic graphs. Verification is as follows:

>>> from networkx.algorithms import isomorphism
>>> G1 = nx.path_graph(4)
>>> G2 = nx.path_graph(4)
>>> GM = isomorphism.GraphMatcher(G1, G2)
>>> GM.is_isomorphic()
True

GM.mapping stores the isomorphism mapping from G1 to G2.

>>> GM.mapping
{0: 0, 1: 1, 2: 2, 3: 3}


Suppose G1 and G2 are isomorphic directed graphs.
Verification is as follows:

>>> G1 = nx.path_graph(4, create_using=nx.DiGraph)
>>> G2 = nx.path_graph(4, create_using=nx.DiGraph)
>>> DiGM = isomorphism.DiGraphMatcher(G1, G2)
>>> DiGM.is_isomorphic()
True

DiGM.mapping stores the isomorphism mapping from G1 to G2.

>>> DiGM.mapping
{0: 0, 1: 1, 2: 2, 3: 3}



Subgraph Isomorphism
--------------------
Graph theory literature can be ambiguous about the meaning of the
above statement, and we seek to clarify it now.

In the VF2 literature, a mapping ``M`` is said to be a graph-subgraph
isomorphism iff ``M`` is an isomorphism between ``G2`` and a subgraph of ``G1``.
Thus, to say that ``G1`` and ``G2`` are graph-subgraph isomorphic is to say
that a subgraph of ``G1`` is isomorphic to ``G2``.

Other literature uses the phrase 'subgraph isomorphic' as in '``G1`` does
not have a subgraph isomorphic to ``G2``'.  Another use is as an in adverb
for isomorphic.  Thus, to say that ``G1`` and ``G2`` are subgraph isomorphic
is to say that a subgraph of ``G1`` is isomorphic to ``G2``.

Finally, the term 'subgraph' can have multiple meanings. In this
context, 'subgraph' always means a 'node-induced subgraph'. Edge-induced
subgraph isomorphisms are not directly supported, but one should be
able to perform the check by making use of
:func:`line_graph <networkx.generators.line.line_graph>`. For
subgraphs which are not induced, the term 'monomorphism' is preferred
over 'isomorphism'.

Let ``G = (N, E)`` be a graph with a set of nodes ``N`` and set of edges ``E``.

If ``G' = (N', E')`` is a subgraph, then:
    ``N'`` is a subset of ``N`` and
    ``E'`` is a subset of ``E``.

If ``G' = (N', E')`` is a node-induced subgraph, then:
    ``N'`` is a subset of ``N`` and
    ``E'`` is the subset of edges in ``E`` relating nodes in ``N'``.

If ``G' = (N', E')`` is an edge-induced subgraph, then:
    ``N'`` is the subset of nodes in ``N`` related by edges in ``E'`` and
    ``E'`` is a subset of ``E``.

If ``G' = (N', E')`` is a monomorphism, then:
    ``N'`` is a subset of ``N`` and
    ``E'`` is a subset of the set of edges in ``E`` relating nodes in ``N'``.

Note that if ``G'`` is a node-induced subgraph of ``G``, then it is always a
subgraph monomorphism of ``G``, but the opposite is not always true, as a
monomorphism can have fewer edges.

References
----------
[1]   Luigi P. Cordella, Pasquale Foggia, Carlo Sansone, Mario Vento,
      "A (Sub)Graph Isomorphism Algorithm for Matching Large Graphs",
      IEEE Transactions on Pattern Analysis and Machine Intelligence,
      vol. 26,  no. 10,  pp. 1367-1372,  Oct.,  2004.
      http://ieeexplore.ieee.org/iel5/34/29305/01323804.pdf

[2]   L. P. Cordella, P. Foggia, C. Sansone, M. Vento, "An Improved
      Algorithm for Matching Large Graphs", 3rd IAPR-TC15 Workshop
      on Graph-based Representations in Pattern Recognition, Cuen,
      pp. 149-159, 2001.
      https://www.researchgate.net/publication/200034365_An_Improved_Algorithm_for_Matching_Large_Graphs

See Also
--------
:meth:`semantic_feasibility <networkx.algorithms.isomorphism.GraphMatcher.semantic_feasibility>`
:meth:`syntactic_feasibility <networkx.algorithms.isomorphism.GraphMatcher.syntactic_feasibility>`

Notes
-----

The implementation handles both directed and undirected graphs as well
as multigraphs.

In general, the subgraph isomorphism problem is NP-complete whereas the
graph isomorphism problem is most likely not NP-complete (although no
polynomial-time algorithm is known to exist).

    NGraphMatcherDiGraphMatcherc                   d    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zy)r   zvImplementation of VF2 algorithm for matching undirected graphs.

    Suitable for Graph and MultiGraph instances.
    c                 D   |j                         |j                         k7  rt        j                  d      | j                         }|s5|j                         s|j                         rt        j                  d      |r5|j                         r|j                         st        j                  d      || _        || _        t        |j                               | _        t        |j                               | _	        t        |      D ci c]  \  }}||
 c}}| _        t        j                         | _        t        | j
                        }| j                  d|z  k  r!t        j                   t#        d|z               d| _        | j'                          yc c}}w )a  Initialize GraphMatcher.

        Parameters
        ----------
        G1,G2: NetworkX Graph or MultiGraph instances.
           The two graphs to check for isomorphism or monomorphism.

        Examples
        --------
        To create a GraphMatcher which checks for syntactic feasibility:

        >>> from networkx.algorithms import isomorphism
        >>> G1 = nx.path_graph(4)
        >>> G2 = nx.path_graph(4)
        >>> GM = isomorphism.GraphMatcher(G1, G2)
        z)G1 and G2 must have the same directednessz](Multi-)GraphMatcher() not defined for directed graphs. Use (Multi-)DiGraphMatcher() instead.z_(Multi-)DiGraphMatcher() not defined for undirected graphs. Use (Multi-)GraphMatcher() instead.g      ?graphN)is_directednxNetworkXError_is_directed_matcherG1G2setnodesG1_nodesG2_nodes	enumerateG2_node_ordersysgetrecursionlimitold_recursion_limitlensetrecursionlimitinttest
initialize)selfr   r   is_directed_matcherinexpected_max_recursion_levels          [/home/dcms/DCMS/lib/python3.12/site-packages/networkx/algorithms/isomorphism/isomorphvf2.py__init__zGraphMatcher.__init__   sP   " >>r~~//""#NOO"779"(8BNN<L""8 
 (8R^^=M""6 
 BHHJBHHJ/8}=tq!ad= $'#8#8#: '*477|$##c,H&HH!!#c,H&H"IJ 	 	 >s   Fc                      y)NF r   s    r!   r   z!GraphMatcher._is_directed_matcher   s        c                 B    t        j                  | j                         y)zRestores the recursion limit.N)r   r   r   r%   s    r!   reset_recursion_limitz"GraphMatcher.reset_recursion_limit   s     	d667r&   c              #     K   | j                   }| j                  }| j                  j                  }| j                  D cg c]  }|| j
                  vs| }}| j                  D cg c]  }|| j                  vs| }}|r|rt        ||      }|D ]  }||f 
 y	 t        |t        | j                        z
  |      }	| j                  D ]  }|| j
                  vs||	f  yc c}w c c}w wz4Iterator over candidate pairs of nodes in G1 and G2.)keyN)r   r   r   __getitem__inout_1core_1inout_2core_2minr   r   )
r   r   r   min_keynodeT1_inoutT2_inoutnode_2node_1
other_nodes
             r!   candidate_pairs_iterz!GraphMatcher.candidate_pairs_iter   s     
 ====$$00 &*\\MTT5LDMM%)\\MTT5LDMM w/F" %fn$%  C,<!<'J
 GG /D4;;."J../# NMs/   =C5C+C+C5'C0;C0?A"C5"C5c                     i | _         i | _        i | _        i | _        t	        |       | _        | j                   j                         | _        y)zReinitializes the state of the algorithm.

        This method should be redefined if using something other than GMState.
        If only subclassing GraphMatcher, a redefinition is not necessary.

        N)r.   r0   r-   r/   GMStatestatecopymappingr%   s    r!   r   zGraphMatcher.initialize  sE       T]
 {{'')r&   c                 v   | j                   j                         | j                  j                         k7  ryt        d | j                   j	                         D              }t        d | j                  j	                         D              }||k7  ry	 t        | j                               }y# t        $ r Y yw xY w)z0Returns True if G1 and G2 are isomorphic graphs.Fc              3   &   K   | ]	  \  }}|  y wNr$   .0r   ds      r!   	<genexpr>z-GraphMatcher.is_isomorphic.<locals>.<genexpr>0       3$!QA3   c              3   &   K   | ]	  \  }}|  y wrA   r$   rB   s      r!   rE   z-GraphMatcher.is_isomorphic.<locals>.<genexpr>1  rF   rG   T)r   orderr   sorteddegreenextisomorphisms_iterStopIteration)r   d1d2xs       r!   is_isomorphiczGraphMatcher.is_isomorphic$  s     77==?dggmmo- 3$''.."2333$''.."2338	T++-.A 		s   B, ,	B87B8c              #   n   K   d| _         | j                          | j                         E d{    y7 w)z.Generator over isomorphisms between G1 and G2.r   Nr   r   matchr%   s    r!   rM   zGraphMatcher.isomorphisms_iter;  s(      	::<   +535c              #     K   t        | j                        t        | j                        k(  r.| j                  j                         | _        | j                   y| j                         D ]p  \  }}| j                  ||      s| j                  ||      s,| j                  j                  | ||      }| j                         E d{    |j                          r y7 w)a%  Extends the isomorphism mapping.

        This function is called recursively to determine if a complete
        isomorphism can be found between G1 and G2.  It cleans up the class
        variables after each recursive call. If an isomorphism is found,
        we yield the mapping.

        N)r   r.   r   r=   r>   r9   syntactic_feasibilitysemantic_feasibilityr<   	__class__rU   restore)r   G1_nodeG2_nodenewstates       r!   rU   zGraphMatcher.matchB  s      t{{s477|+;;++-DL,,$($=$=$? + --gw?00'B#'::#7#7gw#O#'::<// !((*+
 0s   BC"C"0C"C 	C"c                      y)a  Returns True if adding (G1_node, G2_node) is semantically feasible.

        The semantic feasibility function should return True if it is
        acceptable to add the candidate pair (G1_node, G2_node) to the current
        partial isomorphism mapping.   The logic should focus on semantic
        information contained in the edge data or a formalized node class.

        By acceptable, we mean that the subsequent mapping can still become a
        complete isomorphism mapping.  Thus, if adding the candidate pair
        definitely makes it so that the subsequent mapping cannot become a
        complete isomorphism mapping, then this function must return False.

        The default semantic feasibility function always returns True. The
        effect is that semantics are not considered in the matching of G1
        and G2.

        The semantic checks might differ based on the what type of test is
        being performed.  A keyword description of the test is stored in
        self.test.  Here is a quick description of the currently implemented
        tests::

          test='graph'
            Indicates that the graph matcher is looking for a graph-graph
            isomorphism.

          test='subgraph'
            Indicates that the graph matcher is looking for a subgraph-graph
            isomorphism such that a subgraph of G1 is isomorphic to G2.

          test='mono'
            Indicates that the graph matcher is looking for a subgraph-graph
            monomorphism such that a subgraph of G1 is monomorphic to G2.

        Any subclass which redefines semantic_feasibility() must maintain
        the above form to keep the match() method functional. Implementations
        should consider multigraphs.
        Tr$   )r   r\   r]   s      r!   rY   z!GraphMatcher.semantic_feasibility[  s    L r&   c                 V    	 t        | j                               }y# t        $ r Y yw xY w)a  Returns `True` if a subgraph of ``G1`` is isomorphic to ``G2``.

        Examples
        --------
        When creating the `GraphMatcher`, the order of the arguments is important

        >>> G = nx.Graph([("A", "B"), ("B", "C"), ("A", "C")])
        >>> H = nx.Graph([(0, 1), (1, 2), (0, 2), (1, 3), (0, 4)])

        Check whether a subgraph of G is isomorphic to H:

        >>> isomatcher = nx.isomorphism.GraphMatcher(G, H)
        >>> isomatcher.subgraph_is_isomorphic()
        False

        Check whether a subgraph of H is isomorphic to G:

        >>> isomatcher = nx.isomorphism.GraphMatcher(H, G)
        >>> isomatcher.subgraph_is_isomorphic()
        True
        TF)rL   subgraph_isomorphisms_iterrN   r   rQ   s     r!   subgraph_is_isomorphicz#GraphMatcher.subgraph_is_isomorphic  s/    ,	T4467A 		    	((c                 V    	 t        | j                               }y# t        $ r Y yw xY w)a  Returns `True` if a subgraph of ``G1`` is monomorphic to ``G2``.

        Examples
        --------
        When creating the `GraphMatcher`, the order of the arguments is important.

        >>> G = nx.Graph([("A", "B"), ("B", "C")])
        >>> H = nx.Graph([(0, 1), (1, 2), (0, 2)])

        Check whether a subgraph of G is monomorphic to H:

        >>> isomatcher = nx.isomorphism.GraphMatcher(G, H)
        >>> isomatcher.subgraph_is_monomorphic()
        False

        Check whether a subgraph of H is monomorphic to G:

        >>> isomatcher = nx.isomorphism.GraphMatcher(H, G)
        >>> isomatcher.subgraph_is_monomorphic()
        True
        TF)rL   subgraph_monomorphisms_iterrN   rb   s     r!   subgraph_is_monomorphicz$GraphMatcher.subgraph_is_monomorphic  s/    ,	T5578A 		rd   c              #   n   K   d| _         | j                          | j                         E d{    y7 w)a  Generator over isomorphisms between a subgraph of ``G1`` and ``G2``.

        Examples
        --------
        When creating the `GraphMatcher`, the order of the arguments is important

        >>> G = nx.Graph([("A", "B"), ("B", "C"), ("A", "C")])
        >>> H = nx.Graph([(0, 1), (1, 2), (0, 2), (1, 3), (0, 4)])

        Yield isomorphic mappings between ``H`` and subgraphs of ``G``:

        >>> isomatcher = nx.isomorphism.GraphMatcher(G, H)
        >>> list(isomatcher.subgraph_isomorphisms_iter())
        []

        Yield isomorphic mappings  between ``G`` and subgraphs of ``H``:

        >>> isomatcher = nx.isomorphism.GraphMatcher(H, G)
        >>> next(isomatcher.subgraph_isomorphisms_iter())
        {0: 'A', 1: 'B', 2: 'C'}

        subgraphNrT   r%   s    r!   ra   z'GraphMatcher.subgraph_isomorphisms_iter  s(     0 	::<rV   c              #   n   K   d| _         | j                          | j                         E d{    y7 w)a  Generator over monomorphisms between a subgraph of ``G1`` and ``G2``.

        Examples
        --------
        When creating the `GraphMatcher`, the order of the arguments is important.

        >>> G = nx.Graph([("A", "B"), ("B", "C")])
        >>> H = nx.Graph([(0, 1), (1, 2), (0, 2)])

        Yield monomorphic mappings between ``H`` and subgraphs of ``G``:

        >>> isomatcher = nx.isomorphism.GraphMatcher(G, H)
        >>> list(isomatcher.subgraph_monomorphisms_iter())
        []

        Yield monomorphic mappings  between ``G`` and subgraphs of ``H``:

        >>> isomatcher = nx.isomorphism.GraphMatcher(H, G)
        >>> next(isomatcher.subgraph_monomorphisms_iter())
        {0: 'A', 1: 'B', 2: 'C'}
        monoNrT   r%   s    r!   rf   z(GraphMatcher.subgraph_monomorphisms_iter  s(     . 	::<rV   c                    | j                   dk(  r:| j                  j                  ||      | j                  j                  ||      k  r;y| j                  j                  ||      | j                  j                  ||      k7  ry| j                   dk7  r| j                  |   D ]y  }|| j                  v s| j                  |   | j                  |   vr y| j                  j                  ||      | j                  j                  | j                  |   |      k7  sy y | j                  |   D ]  }|| j
                  v s| j
                  |   | j                  |   vr y| j                   dk(  rI| j                  j                  | j
                  |   |      | j                  j                  ||      k  s y| j                  j                  | j
                  |   |      | j                  j                  ||      k7  s y | j                   dk7  rd}| j                  |   D ]%  }|| j                  v s|| j                  vs!|dz  }' d}| j                  |   D ]%  }|| j                  v s|| j
                  vs!|dz  }' | j                   dk(  r||k7  ry||k\  syd}| j                  |   D ]  }|| j                  vs|dz  } d}| j                  |   D ]  }|| j                  vs|dz  } | j                   dk(  r||k7  ryy||k\  syya|  Returns True if adding (G1_node, G2_node) is syntactically feasible.

        This function returns True if it is adding the candidate pair
        to the current partial isomorphism/monomorphism mapping is allowable.
        The addition is allowable if the inclusion of the candidate pair does
        not make it impossible for an isomorphism/monomorphism to be found.
        rk   Fr      r   T)r   r   number_of_edgesr   r.   r0   r-   r/   )r   r\   r]   neighbornum1num2s         r!   rX   z"GraphMatcher.syntactic_feasibility  s   H 99ww&&w8477;R;R<  ww&&w8DGG<S<S=   99 GGG, %t{{*{{8,DGGG4DD$00 '00X1FPQ  %% ( 	%H4;;&;;x(0@@ YY&(ww..H-w//'BC  %ww..H-w007CD  %	% 99 D GGG, ,84;;3NAID D GGG, ,84;;3NAID yyG#4<   D GGG, 4<</AID D GGG, 4<</AID yyG#4<  	   r&   N)__name__
__module____qualname____doc__r"   r   r(   r9   r   rR   rM   rU   rY   rc   rg   ra   rf   rX   r$   r&   r!   r   r      sQ    
2h8/B*>. +2&P88 8 6yr&   c                   b     e Zd ZdZ fdZd Zd Zd Zd Z fdZ	 fdZ
 fd	Z fd
Z xZS )r   zxImplementation of VF2 algorithm for matching directed graphs.

    Suitable for DiGraph and MultiDiGraph instances.
    c                 &    t         |   ||       y)a  Initialize DiGraphMatcher.

        G1 and G2 should be nx.Graph or nx.MultiGraph instances.

        Examples
        --------
        To create a GraphMatcher which checks for syntactic feasibility:

        >>> from networkx.algorithms import isomorphism
        >>> G1 = nx.DiGraph(nx.path_graph(4, create_using=nx.DiGraph()))
        >>> G2 = nx.DiGraph(nx.path_graph(4, create_using=nx.DiGraph()))
        >>> DiGM = isomorphism.DiGraphMatcher(G1, G2)
        N)superr"   )r   r   r   rZ   s      r!   r"   zDiGraphMatcher.__init__t  s     	R r&   c                      y)NTr$   r%   s    r!   r   z#DiGraphMatcher._is_directed_matcher  s    r&   c              #     K   | j                   }| j                  }| j                  j                  }| j                  D cg c]  }|| j
                  vs| }}| j                  D cg c]  }|| j                  vs| }}|r|rt        ||      }|D ]  }||f 
 y| j                  D cg c]  }|| j
                  vs| }	}| j                  D cg c]  }|| j                  vs| }
}|	r|
rt        |
|      }|	D ]  }||f 
 yt        |t        | j                        z
  |      }|D ]  }|| j
                  vs||f  yc c}w c c}w c c}w c c}w wr*   )r   r   r   r,   out_1r.   out_2r0   r1   in_1in_2r   )r   r   r   r2   r3   T1_outT2_outr6   r7   T1_inT2_ins              r!   r9   z#DiGraphMatcher.candidate_pairs_iter  sb    
 ====$$00 $(::I4T[[1H$II#'::I4T[[1H$II fW-F  %fn$% '+iiKd4t{{3JTKEK&*iiKd4t{{3JTKEK U0# )F &.() XDKK(88gF& -FT[[0$fn,-? JI LKsS   =E#EEE#'E;E?/E#.EEE#E*E.AE#E#c                     i | _         i | _        i | _        i | _        i | _        i | _        t        |       | _        | j                   j                         | _	        y)zReinitializes the state of the algorithm.

        This method should be redefined if using something other than DiGMState.
        If only subclassing GraphMatcher, a redefinition is not necessary.
        N)
r.   r0   r~   r   r|   r}   	DiGMStater<   r=   r>   r%   s    r!   r   zDiGraphMatcher.initialize  sQ      		

t_
 {{'')r&   c                    | j                   dk(  r:| j                  j                  ||      | j                  j                  ||      k  r;y| j                  j                  ||      | j                  j                  ||      k7  ry| j                   dk7  r| j                  j                  |   D ]  }|| j
                  v s| j
                  |   | j                  j                  |   vr y| j                  j                  ||      | j                  j                  | j
                  |   |      k7  s y | j                  j                  |   D ]  }|| j                  v s| j                  |   | j                  j                  |   vr y| j                   dk(  rI| j                  j                  | j                  |   |      | j                  j                  ||      k  s y| j                  j                  | j                  |   |      | j                  j                  ||      k7  s y | j                   dk7  r| j                  |   D ]y  }|| j
                  v s| j
                  |   | j                  |   vr y| j                  j                  ||      | j                  j                  || j
                  |         k7  sy y | j                  |   D ]  }|| j                  v s| j                  |   | j                  |   vr y| j                   dk(  rI| j                  j                  || j                  |         | j                  j                  ||      k  s y| j                  j                  || j                  |         | j                  j                  ||      k7  s y | j                   dk7  rd}| j                  j                  |   D ]%  }|| j                  v s|| j
                  vs!|dz  }' d}| j                  j                  |   D ]%  }|| j                  v s|| j                  vs!|dz  }' | j                   dk(  r||k7  ry||k\  syd}| j                  |   D ]%  }|| j                  v s|| j
                  vs!|dz  }' d}| j                  |   D ]%  }|| j                  v s|| j                  vs!|dz  }' | j                   dk(  r||k7  ry||k\  syd}| j                  j                  |   D ]%  }|| j                  v s|| j
                  vs!|dz  }' d}| j                  j                  |   D ]%  }|| j                  v s|| j                  vs!|dz  }' | j                   dk(  r||k7  ry||k\  syd}| j                  |   D ]%  }|| j                  v s|| j
                  vs!|dz  }' d}| j                  |   D ]%  }|| j                  v s|| j                  vs!|dz  }' | j                   dk(  r||k7  ry||k\  syd}| j                  j                  |   D ]%  }|| j                  vs|| j                  vs!|dz  }' d}| j                  j                  |   D ]%  }|| j                  vs|| j                  vs!|dz  }' | j                   dk(  r||k7  ry||k\  syd}| j                  |   D ]%  }|| j                  vs|| j                  vs!|dz  }' d}| j                  |   D ]%  }|| j                  vs|| j                  vs!|dz  }' | j                   dk(  r||k7  ryy||k\  syyrm   )r   r   ro   r   predr.   r0   r~   r   r|   r}   )r   r\   r]   predecessor	successorrq   rr   s          r!   rX   z$DiGraphMatcher.syntactic_feasibility  s1   D 99ww&&w8477;R;R<  ww&&w8DGG<S<S=   99#ww||G4 %$++-{{;/tww||G7LL$00#W00[1I7ST  %%  77<<0 	%Kdkk);;{+477<<3HH YY&(ww..K0'//WEF  %ww..K0'00gFG  %	%( 99!WWW- %	+{{9-TWWW5EE$0000$++i:PQR  %% ) 	%IDKK';;y)1AA YY&(ww..Y!7//CD  %ww..Y!700)DE  %	% 99 D#ww||G4 499,;dkk3QAID D#ww||G4 499,;dkk3QAID yyG#4<   D!WWW- 	*$++1MAID D!WWW- 	*$++1MAID yyG#4<   D#ww||G4 4::-Kt{{4RAID D#ww||G4 4::-Kt{{4RAID yyG#4<   D!WWW- 	+)4;;2NAID D!WWW- 	+)4;;2NAID yyG#4<   D#ww||G4 tyy0{$**7TAID D#ww||G4 tyy0{$**7TAID yyG#4<  
 D!WWW- 	TYY.Ydjj5PAID D!WWW- 	TYY.Ydjj5PAID yyG#4<  	   r&   c                      t         |          S )a  Returns `True` if a subgraph of ``G1`` is isomorphic to ``G2``.

        Examples
        --------
        When creating the `DiGraphMatcher`, the order of the arguments is important

        >>> G = nx.DiGraph([("A", "B"), ("B", "A"), ("B", "C"), ("C", "B")])
        >>> H = nx.DiGraph(nx.path_graph(5))

        Check whether a subgraph of G is isomorphic to H:

        >>> isomatcher = nx.isomorphism.DiGraphMatcher(G, H)
        >>> isomatcher.subgraph_is_isomorphic()
        False

        Check whether a subgraph of H is isomorphic to G:

        >>> isomatcher = nx.isomorphism.DiGraphMatcher(H, G)
        >>> isomatcher.subgraph_is_isomorphic()
        True
        )ry   rc   r   rZ   s    r!   rc   z%DiGraphMatcher.subgraph_is_isomorphic  s    , w-//r&   c                      t         |          S )a  Returns `True` if a subgraph of ``G1`` is monomorphic to ``G2``.

        Examples
        --------
        When creating the `DiGraphMatcher`, the order of the arguments is important.

        >>> G = nx.DiGraph([("A", "B"), ("C", "B"), ("D", "C")])
        >>> H = nx.DiGraph([(0, 1), (1, 2), (2, 3), (3, 2)])

        Check whether a subgraph of G is monomorphic to H:

        >>> isomatcher = nx.isomorphism.DiGraphMatcher(G, H)
        >>> isomatcher.subgraph_is_monomorphic()
        False

        Check whether a subgraph of H is isomorphic to G:

        >>> isomatcher = nx.isomorphism.DiGraphMatcher(H, G)
        >>> isomatcher.subgraph_is_monomorphic()
        True
        )ry   rg   r   s    r!   rg   z&DiGraphMatcher.subgraph_is_monomorphic  s    , w.00r&   c                      t         |          S )a  Generator over isomorphisms between a subgraph of ``G1`` and ``G2``.

        Examples
        --------
        When creating the `DiGraphMatcher`, the order of the arguments is important

        >>> G = nx.DiGraph([("B", "C"), ("C", "B"), ("C", "D"), ("D", "C")])
        >>> H = nx.DiGraph(nx.path_graph(5))

        Yield isomorphic mappings between ``H`` and subgraphs of ``G``:

        >>> isomatcher = nx.isomorphism.DiGraphMatcher(G, H)
        >>> list(isomatcher.subgraph_isomorphisms_iter())
        []

        Yield isomorphic mappings between ``G`` and subgraphs of ``H``:

        >>> isomatcher = nx.isomorphism.DiGraphMatcher(H, G)
        >>> next(isomatcher.subgraph_isomorphisms_iter())
        {0: 'B', 1: 'C', 2: 'D'}
        )ry   ra   r   s    r!   ra   z)DiGraphMatcher.subgraph_isomorphisms_iter  s    , w133r&   c                      t         |          S )a  Generator over monomorphisms between a subgraph of ``G1`` and ``G2``.

        Examples
        --------
        When creating the `DiGraphMatcher`, the order of the arguments is important.

        >>> G = nx.DiGraph([("A", "B"), ("C", "B"), ("D", "C")])
        >>> H = nx.DiGraph([(0, 1), (1, 2), (2, 3), (3, 2)])

        Yield monomorphic mappings between ``H`` and subgraphs of ``G``:

        >>> isomatcher = nx.isomorphism.DiGraphMatcher(G, H)
        >>> list(isomatcher.subgraph_monomorphisms_iter())
        []

        Yield monomorphic mappings between ``G`` and subgraphs of ``H``:

        >>> isomatcher = nx.isomorphism.DiGraphMatcher(H, G)
        >>> next(isomatcher.subgraph_monomorphisms_iter())
        {3: 'A', 2: 'B', 1: 'C', 0: 'D'}
        )ry   rf   r   s    r!   rf   z*DiGraphMatcher.subgraph_monomorphisms_iter  s    , w244r&   )rs   rt   ru   rv   r"   r   r9   r   rX   rc   rg   ra   rf   __classcell__)rZ   s   @r!   r   r   n  s>    
! +-^ *D\|0010405 5r&   c                       e Zd ZdZddZd Zy)r;   aG  Internal representation of state for the GraphMatcher class.

    This class is used internally by the GraphMatcher class.  It is used
    only to store state specific data. There will be at most G2.order() of
    these objects in memory at a time, due to the depth-first search
    strategy employed by the VF2 algorithm.
    Nc                    || _         d| _        d| _        t        |j                        | _        ||i |_        i |_        i |_        i |_        ||||j                  |<   ||j                  |<   || _        || _        t        |j                        | _        ||j                  vr| j
                  |j                  |<   ||j                  vr| j
                  |j                  |<   t               }|j                  D ]=  }|j                  |j                  |   D cg c]  }||j                  vs| c}       ? |D ]*  }||j                  vs| j
                  |j                  |<   , t               }|j                  D ]=  }|j                  |j                  |   D cg c]  }||j                  vs| c}       ? |D ]*  }||j                  vs| j
                  |j                  |<   , yyyc c}w c c}w )zInitializes GMState object.

        Pass in the GraphMatcher to which this GMState belongs and the
        new node pair that will be added to the GraphMatcher's current
        isomorphism mapping.
        N)GMr\   r]   r   r.   depthr0   r-   r/   r   updater   r   )r   r   r\   r]   	new_nodesr3   rp   s          r!   r"   zGMState.__init__   s     ^
?goBIBIBJBJ 7#6!(BIIg!(BIIg #DL"DL RYYDJ bjj(&*jj

7#bjj(&*jj

7#
 I		   .0eeDkW(XRYY=VXW " 2rzz)'+zzBJJt$2
 I		   .0eeDkW(XRYY=VXW " 2rzz)'+zzBJJt$2K $72 X Xs   G7
3G7
$G<
8G<
c                    | j                   N| j                  B| j                  j                  | j                   = | j                  j                  | j                  = | j                  j
                  | j                  j                  fD ]6  }t        |j                               D ]  }||   | j                  k(  s||=  8 y)z<Deletes the GMState object and restores the class variables.N)
r\   r]   r   r.   r0   r-   r/   listkeysr   r   vectorr3   s      r!   r[   zGMState.restore_  s     <<#(@t||,t||, ww8 	%FV[[]+ %$<4::-t%	%r&   NNrs   rt   ru   rv   r"   r[   r$   r&   r!   r;   r;     s    =2~%r&   r;   c                       e Zd ZdZddZd Zy)r   aL  Internal representation of state for the DiGraphMatcher class.

    This class is used internally by the DiGraphMatcher class.  It is used
    only to store state specific data. There will be at most G2.order() of
    these objects in memory at a time, due to the depth-first search
    strategy employed by the VF2 algorithm.

    Nc                    || _         d| _        d| _        t        |j                        | _        ||*i |_        i |_        i |_        i |_        i |_	        i |_
        ||||j                  |<   ||j                  |<   || _        || _        t        |j                        | _        |j                  |j                  fD ]  }||vs| j
                  ||<    |j                  |j                  fD ]  }||vs| j
                  ||<    t               }|j                  D ]H  }|j                  |j                  j                  |      D cg c]  }||j                  vr| c}       J |D ]*  }||j                  vs| j
                  |j                  |<   , t               }|j                  D ]H  }|j                  |j                  j                  |      D cg c]  }||j                  vr| c}       J |D ]*  }||j                  vs| j
                  |j                  |<   , t               }|j                  D ]H  }|j                  |j                  j!                  |      D cg c]  }||j                  vr| c}       J |D ]*  }||j                  vs| j
                  |j                  |<   , t               }|j                  D ]H  }|j                  |j                  j!                  |      D cg c]  }||j                  vr| c}       J |D ]*  }||j                  vs| j
                  |j                  |<   , yyyc c}w c c}w c c}w c c}w )zInitializes DiGMState object.

        Pass in the DiGraphMatcher to which this DiGMState belongs and the
        new node pair that will be added to the GraphMatcher's current
        isomorphism mapping.
        N)r   r\   r]   r   r.   r   r0   r~   r   r|   r}   r   r   r   predecessorsr   
successors)	r   r   r\   r]   r   r   r3   r   r   s	            r!   r"   zDiGMState.__init__y  sV     ^
?goBIBIBGBGBHBH 7#6!(BIIg!(BIIg #DL"DL RYYDJ 77BHH- 1&(&*jjF7O1 77BHH- 1&(&*jjF7O1 I		    ,.55+=+=d+C'&bii7 $ " /rww&$(JJBGGDM/
 I		    ,.55+=+=d+C'&bii7 $ " /rww&$(JJBGGDM/
 I		    *,)9)9$)?%$BII5 " " 0rxx'%)ZZBHHTN0
 I		    *,)9)9$)?%$BII5 " " 0rxx'%)ZZBHHTN0W $76s   M
M
-M
=M
c                    | j                   N| j                  B| j                  j                  | j                   = | j                  j                  | j                  = | j                  j
                  | j                  j                  | j                  j                  | j                  j                  fD ]6  }t        |j                               D ]  }||   | j                  k(  s||=  8 y)z>Deletes the DiGMState object and restores the class variables.N)r\   r]   r   r.   r0   r~   r   r|   r}   r   r   r   r   s      r!   r[   zDiGMState.restore  s    
 <<#(@t||,t||, ww||TWW\\477==$''--P 	%FV[[]+ %$<4::-t%	%r&   r   r   r$   r&   r!   r   r   o  s    e0N%r&   r   )	rv   r   networkxr	   __all__r   r   r;   r   r$   r&   r!   <module>r      sT   Kd  +
,Q Qhf5\ f5RU% U%p% %r&   