
\c           @   s   d  Z  d d l Z d d l Z d d g Z d e f d     YZ d e f d     YZ d e f d     YZ d	 e f d
     YZ	 d S(   s  
*************
VF2 Algorithm
*************

An implementation of VF2 algorithm for graph ismorphism testing.

The simplest interface to use this module is to call networkx.is_isomorphic().

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 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
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 ambiguious 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 nx.line_graph(). For
subgraphs which are not induced, the term 'monomorphism' is preferred
over 'isomorphism'. Currently, it is not possible to check for
monomorphisms.

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
    E' is a subset of E

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

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

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.
      http://amalfi.dis.unina.it/graph/db/papers/vf-algorithm.pdf

See Also
--------
syntactic_feasibliity(), semantic_feasibility()

Notes
-----
Modified to handle undirected graphs.
Modified to handle multiple edges.


In general, this problem is NP-Complete.



iNt   GraphMatchert   DiGraphMatcherc           B   sq   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 RS(   sv   Implementation of VF2 algorithm for matching undirected graphs.

    Suitable for Graph and MultiGraph instances.
    c         C   s   | |  _  | |  _ t | j    |  _ t | j    |  _ t j   |  _ t	 |  j  } |  j d | k  r t j
 t d |   n  d |  _ |  j   d S(   s  Initialize GraphMatcher.

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

        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)
        g      ?t   graphN(   t   G1t   G2t   sett   nodest   G1_nodest   G2_nodest   syst   getrecursionlimitt   old_recursion_limitt   lent   setrecursionlimitt   intt   testt
   initialize(   t   selfR   R   t   expected_max_recursion_level(    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyt   __init__   s    			c         C   s   t  j |  j  d S(   s   Restores the recursion limit.N(   R	   R   R   (   R   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyt   reset_recursion_limit   s    c         c   s   |  j  } |  j } g  | D]* } | |  j k r | |  j k r | ^ q } g  | D]* } | |  j k rP | |  j k rP | ^ qP } | r | r xl | D] } | t |  f Vq WnJ t | t |  j   } x. |  j D]# } | |  j k r | | f Vq q Wd S(   s4   Iterator over candidate pairs of nodes in G1 and G2.N(	   R   R   t   inout_1t   core_1t   inout_2t   core_2t   minR   R   (   R   R   R   t   nodet   T1_inoutt   T2_inoutt
   other_node(    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyt   candidate_pairs_iter   s    		77c         C   sI   i  |  _  i  |  _ i  |  _ i  |  _ t |   |  _ |  j  j   |  _ d S(   s   Reinitializes 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   R   R   R   t   GMStatet   statet   copyt   mapping(   R   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR      s    					c         C   s   |  j  j   |  j j   k r" t St |  j  j   j    } t |  j j   j    } | | k rh t Sy t |  j    } t	 SWn t
 k
 r t SXd S(   s0   Returns True if G1 and G2 are isomorphic graphs.N(   R   t   orderR   t   Falset   sortedt   degreet   valuest   nextt   isomorphisms_itert   Truet   StopIteration(   R   t   d1t   d2t   x(    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyt   is_isomorphic  s      c         c   s3   d |  _  |  j   x |  j   D] } | Vq  Wd S(   s.   Generator over isomorphisms between G1 and G2.R   N(   R   R   t   match(   R   R"   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR)     s    	
c         c   s   t  |  j  t  |  j  k r; |  j j   |  _ |  j Vn x |  j   D]t \ } } |  j | |  rH |  j | |  r |  j j	 |  | |  } x |  j
   D] } | Vq W| j   q qH qH Wd S(   s%  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"   R   t   syntactic_feasibilityt   semantic_feasibilityR    t	   __class__R0   t   restore(   R   t   G1_nodet   G2_nodet   newstateR"   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR0   "  s    		c         C   s   t  S(   s(  Returns True if adding (G1_node, G2_node) is symantically 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.

        Any subclass which redefines semantic_feasibility() must maintain
        the above form to keep the match() method functional. Implementations
        should consider multigraphs.
        (   R*   (   R   R5   R6   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR2   <  s    "c         C   s3   y t  |  j    } t SWn t k
 r. t SXd S(   s5   Returns True if a subgraph of G1 is isomorphic to G2.N(   R(   t   subgraph_isomorphisms_iterR*   R+   R$   (   R   R.   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyt   subgraph_is_isomorphic`  s
    c         c   s3   d |  _  |  j   x |  j   D] } | Vq  Wd S(   s<   Generator over isomorphisms between a subgraph of G1 and G2.t   subgraphN(   R   R   R0   (   R   R"   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR8   j  s    	
c         C   s  |  j  j | |  |  j j | |  k r. t Sxz |  j  | D]k } | |  j k r< |  j | |  j | k ro t S|  j  j | |  |  j j |  j | |  k r t Sq< q< Wxz |  j | D]k } | |  j k r |  j | |  j  | k r t S|  j  j |  j | |  |  j j | |  k r$t Sq q Wd } x@ |  j  | D]1 } | |  j k r<| |  j k r<| d 7} q<q<Wd } x@ |  j | D]1 } | |  j k r| |  j k r| d 7} qqW|  j d k r| | k st Sn | | k st Sd } x1 |  j  | D]" } | |  j k r | d 7} q q Wd } x1 |  j | D]" } | |  j k r:| d 7} q:q:W|  j d k r| | k st Sn | | k st St	 S(   sc  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 mapping is allowable.  The addition
        is allowable if the inclusion of the candidate pair does not make it
        impossible for an isomorphism to be found.
        i    i   R   (
   R   t   number_of_edgesR   R$   R   R   R   R   R   R*   (   R   R5   R6   t   neighbort   num1t   num2(    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR1   t  sR    $*11
(   t   __name__t
   __module__t   __doc__R   R   R   R   R/   R)   R0   R2   R9   R8   R1   (    (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR       s   	#							$	
	
c           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   sx   Implementation of VF2 algorithm for matching directed graphs.

    Suitable for DiGraph and MultiDiGraph instances.
    c         C   s   t  t |   j | |  d S(   s  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(   t   superR   R   (   R   R   R   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR     s    c   
      c   s  |  j  } |  j } g  | D]* } | |  j k r | |  j k r | ^ q } g  | D]* } | |  j k rP | |  j k rP | ^ qP } | r | r t |  } x| D] } | | f Vq Wn g  | D]* } | |  j k r | |  j k r | ^ q } g  | D]* } | |  j k r | |  j k r | ^ q }	 | r\|	 r\t |	  } xc | D] } | | f VqDWnG t | t	 |  j   } x+ | D]# } | |  j k r|| | f Vq|q|Wd S(   s4   Iterator over candidate pairs of nodes in G1 and G2.N(
   R   R   t   out_1R   t   out_2R   R   t   in_1t   in_2R   (
   R   R   R   R   t   T1_outt   T2_outt   node_2t   node_1t   T1_int   T2_in(    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR     s$    		7777c         C   s[   i  |  _  i  |  _ i  |  _ i  |  _ i  |  _ i  |  _ t |   |  _ |  j  j   |  _	 d S(   s   Reinitializes 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   R   RE   RF   RC   RD   t	   DiGMStateR    R!   R"   (   R   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR   "  s    						c         C   s  |  j  j | |  |  j j | |  k r. t Sx |  j  j | D]n } | |  j k r? |  j | |  j j | k ru t S|  j  j | |  |  j j |  j | |  k r t Sq? q? Wx |  j j | D]n } | |  j k r |  j | |  j  j | k r t S|  j  j |  j | |  |  j j | |  k r0t Sq q Wxz |  j  | D]k } | |  j k rB|  j | |  j | k rut S|  j  j | |  |  j j | |  j |  k rt SqBqBWxz |  j | D]k } | |  j k r|  j | |  j  | k rt S|  j  j | |  j |  |  j j | |  k r*t SqqWd } xC |  j  j | D]1 } | |  j k rE| |  j k rE| d 7} qEqEWd } xC |  j j | D]1 } | |  j k r| |  j k r| d 7} qqW|  j	 d k r| | k st Sn | | k st Sd } x@ |  j  | D]1 } | |  j k r| |  j k r| d 7} qqWd } x@ |  j | D]1 } | |  j k rU| |  j k rU| d 7} qUqUW|  j	 d k r| | k st Sn | | k st Sd } xC |  j  j | D]1 } | |  j
 k r| |  j k r| d 7} qqWd } xC |  j j | D]1 } | |  j k r| |  j k r| d 7} qqW|  j	 d k rv| | k st Sn | | k st Sd } x@ |  j  | D]1 } | |  j
 k r| |  j k r| d 7} qqWd } x@ |  j | D]1 } | |  j k r| |  j k r| d 7} qqW|  j	 d k r:| | k sJt Sn | | k sJt Sd } xC |  j  j | D]1 } | |  j k ra| |  j
 k ra| d 7} qaqaWd } xC |  j j | D]1 } | |  j k r| |  j k r| d 7} qqW|  j	 d k r| | k st Sn | | k st Sd } x@ |  j  | D]1 } | |  j k r(| |  j
 k r(| d 7} q(q(Wd } x@ |  j | D]1 } | |  j k rq| |  j k rq| d 7} qqqqW|  j	 d k r| | k st Sn | | k st St S(   sc  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 mapping is allowable.  The addition
        is allowable if the inclusion of the candidate pair does not make it
        impossible for an isomorphism to be found.
        i    i   R   (   R   R;   R   R$   t   predR   R   RE   RF   R   RC   RD   R*   (   R   R5   R6   t   predecessort	   successorR=   R>   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR1   D  s    &*1111	(   R?   R@   RA   R   R   R   R1   (    (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR     s
   		.	"R   c           B   s&   e  Z d  Z d d d  Z d   Z RS(   sG  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.
    c         C   s  | |  _  d |  _ d |  _ t | j  |  _ | d k sE | d k rl i  | _ i  | _ i  | _ i  | _	 n  | d k	 r| d k	 r| | j | <| | j | <| |  _ | |  _ t | j  |  _ | | j k r |  j | j | <n  | | j	 k r|  j | j	 | <n  t
 g   } xG | j D]< } | j g  | j | D] } | | j k r6| ^ q6 qWx0 | D]( } | | j k rc|  j | j | <qcqcWt
 g   } xG | j D]< } | j g  | j | D] } | | j k r| ^ q qWx3 | D]( } | | j	 k r|  j | j	 | <qqWn  d S(   s   Initializes 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(   t   GMt   NoneR5   R6   R   R   t   depthR   R   R   R   t   updateR   R   (   R   RQ   R5   R6   t	   new_nodesR   R<   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR     s>    								::c         C   s   |  j  d k	 rA |  j d k	 rA |  j j |  j  =|  j j |  j =n  xZ |  j j |  j j f D]@ } x7 t | j	    D]# } | | |  j
 k rs | | =qs qs WqZ Wd S(   s<   Deletes the GMState object and restores the class variables.N(   R5   RR   R6   RQ   R   R   R   R   t   listt   keysRS   (   R   t   vectorR   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR4   N  s    N(   R?   R@   RA   RR   R   R4   (    (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR     s   ;RM   c           B   s&   e  Z d  Z d d d  Z d   Z RS(   sL  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.

    c   	      C   s  | |  _  d |  _ d |  _ t | j  |  _ | d k sE | d k r~ i  | _ i  | _ i  | _ i  | _	 i  | _
 i  | _ n  | d k	 r| d k	 r| | j | <| | j | <| |  _ | |  _ t | j  |  _ x6 | j | j
 f D]" } | | k r |  j | | <q q Wx6 | j	 | j f D]" } | | k r |  j | | <q q Wt g   } xL | j D]A } | j g  | j j |  D] } | | j k r{| ^ q{ q\Wx0 | D]( } | | j k r|  j | j | <qqWt g   } xL | j D]A } | j g  | j j |  D] } | | j k r	| ^ q	 qWx0 | D]( } | | j	 k r6|  j | j	 | <q6q6Wt g   } xL | j D]A } | j g  | j j |  D] } | | j k r| ^ q qxWx0 | D]( } | | j
 k r|  j | j
 | <qqWt g   } xL | j D]A } | j g  | j j |  D] } | | j k r%| ^ q% qWx3 | D]( } | | j k rR|  j | j | <qRqRWn  d S(   s   Initializes 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(   RQ   RR   R5   R6   R   R   RS   R   RE   RF   RC   RD   R   RT   R   t   predecessorsR   t
   successors(	   R   RQ   R5   R6   RX   RU   R   RO   RP   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR   g  s^    										????c         C   s   |  j  d k	 rA |  j d k	 rA |  j j |  j  =|  j j |  j =n  xl |  j j |  j j |  j j |  j j	 f D]@ } x7 t
 | j    D]# } | | |  j k r | | =q q Wql Wd S(   s>   Deletes the DiGMState object and restores the class variables.N(   R5   RR   R6   RQ   R   R   RE   RF   RC   RD   RV   RW   RS   (   R   RX   R   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyR4     s    1N(   R?   R@   RA   RR   R   R4   (    (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyRM   ^  s   O(
   RA   R	   t   networkxt   nxt   __all__t   objectR    R   R   RM   (    (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/isomorphism/isomorphvf2.pyt   <module>   s   	 L /S