
    VhA)                     6   d Z ddlZddlmZ ddlZddlmZ g dZ ej                  ddd      d	d
d       Z
d Z ej                  ddd      d        Zd Z ej                  ddd      d        Zd Z ej                  ddddd      dd       Zy)z
Graph isomorphism functions.
    N)Counter)NetworkXError)could_be_isomorphicfast_could_be_isomorphicfaster_could_be_isomorphicis_isomorphic   )G1G2)graphsdtc)
propertiesc          	           j                         j                         k7  ryt        |      }g g c fd}d|v rFj                   j                                j                  j                                 |       syd|v rPj                  t	        j
                                j                  t	        j
                                |       syd|v rj                  t        t        j                  j                  t	        j                                            j                  t        t        j                  j                  t	        j                                            |       syy)a  Returns False if graphs are definitely not isomorphic.
    True does NOT guarantee isomorphism.

    Parameters
    ----------
    G1, G2 : graphs
       The two graphs `G1` and `G2` must be the same type.

    properties : str, default="dct"
       Determines which properties of the graph are checked. Each character
       indicates a particular property as follows:

       - if ``"d"`` in `properties`: degree of each node
       - if ``"t"`` in `properties`: number of triangles for each node
       - if ``"c"`` in `properties`: number of maximal cliques for each node

       Unrecognized characters are ignored. The default is ``"dtc"``, which
       compares the sequence of ``(degree, num_triangles, num_cliques)`` properties
       between `G1` and `G2`. Generally, ``properties="dt"`` would be faster, and
       ``properties="d"`` faster still. See Notes for additional details on
       property selection.

    Returns
    -------
    bool
       A Boolean value representing whether `G1` could be isomorphic with `G2`
       according to the specified `properties`.

    Notes
    -----
    The triangle sequence contains the number of triangles each node is part of.
    The clique sequence contains for each node the number of maximal cliques
    involving that node.

    Some properties are faster to compute than others. And there are other
    properties we could include and don't. But of the three properties listed here,
    comparing the degree distributions is the fastest. The "triangles" property
    is slower (and also a stricter version of "could") and the "maximal cliques"
    property is slower still, but usually faster than doing a full isomorphism
    check.
    Fc                       D   cg c]   t         fdD               }} D   cg c]   t         fdD               }} t        |      t        |      k(  S c c} w c c} w )Nc              3   (   K   | ]	  }|     y wN .0pns     X/home/dcms/DCMS/lib/python3.12/site-packages/networkx/algorithms/isomorphism/isomorph.py	<genexpr>zFcould_be_isomorphic.<locals>._properties_consistent.<locals>.<genexpr>H        2A1Q42   c              3   (   K   | ]	  }|     y wr   r   r   s     r   r   zFcould_be_isomorphic.<locals>._properties_consistent.<locals>.<genexpr>I   r   r   )tuplesorted)r   	G1_ptable	G2_ptabler
   G1_propsr   G2_propss   `  r   _properties_consistentz3could_be_isomorphic.<locals>._properties_consistentF   s]    <>?qU222?	?<>?qU222?	?i F9$555 @?s
   AA#dtcT)ordersetappenddegreenx	trianglesr   	itertoolschainfrom_iterablefind_cliques)r
   r   r   properties_to_checkr#   r!   r"   s   ``   @@r   r   r      s   Z 
xxzRXXZj/RHh6 !!		$		$%'
!!R()R()%'
!!	 = =boob>Q RST	 = =boob>Q RST%'     c                 R    ddl }|j                  dt        d       t        | |      S )z
    .. deprecated:: 3.5

       `graph_could_be_isomorphic` is a deprecated alias for `could_be_isomorphic`.
       Use `could_be_isomorphic` instead.
    r   NzKgraph_could_be_isomorphic is deprecated, use `could_be_isomorphic` instead.   category
stacklevel)warningswarnDeprecationWarningr   r
   r   r8   s      r   graph_could_be_isomorphicr<   j   s0     MMU#  
 r2&&r2   c                    | j                         |j                         k7  ry| j                         }t        j                  |       }|D cg c]  \  }}|||   g }}}|j	                          |j                         }t        j                  |      }|D cg c]  \  }}|||   g }	}}|	j	                          ||	k7  ryyc c}}w c c}}w )al  Returns False if graphs are definitely not isomorphic.

    True does NOT guarantee isomorphism.

    Parameters
    ----------
    G1, G2 : graphs
       The two graphs G1 and G2 must be the same type.

    Notes
    -----
    Checks for matching degree and triangle sequences. The triangle
    sequence contains the number of triangles each node is part of.
    FT)r'   r*   r+   r,   sort)
r
   r   d1t1vr$   props1d2t2props2s
             r   r   r   {   s    " 
xxzRXXZ 
B	b	B%'(TQq"Q%j(F(
KKM	B	b	B%'(TQq"Q%j(F(
KKM  )
 )s   CCc                 R    ddl }|j                  dt        d       t        | |      S )z
    .. deprecated:: 3.5

       `fast_graph_could_be_isomorphic` is a deprecated alias for
       `fast_could_be_isomorphic`. Use `fast_could_be_isomorphic` instead.
    r   NzRfast_graph_could_be_isomorphic is deprecated, use fast_could_be_isomorphic insteadr4   r5   )r8   r9   r:   r   r;   s      r   fast_graph_could_be_isomorphicrG      s0     MM\#  
 $B++r2   c                     | j                         |j                         k7  ryt        d | j                         D              }t        d |j                         D              }||k7  ryy)a  Returns False if graphs are definitely not isomorphic.

    True does NOT guarantee isomorphism.

    Parameters
    ----------
    G1, G2 : graphs
       The two graphs G1 and G2 must be the same type.

    Notes
    -----
    Checks for matching degree sequences.
    Fc              3   &   K   | ]	  \  }}|  y wr   r   r   r   r$   s      r   r   z-faster_could_be_isomorphic.<locals>.<genexpr>        *da*   c              3   &   K   | ]	  \  }}|  y wr   r   rJ   s      r   r   z-faster_could_be_isomorphic.<locals>.<genexpr>   rK   rL   T)r'   r   r*   )r
   r   r?   rC   s       r   r   r      sX      
xxzRXXZ 
*biik*	*B	*biik*	*B	Rx r2   c                 R    ddl }|j                  dt        d       t        | |      S )z
    .. deprecated:: 3.5

       `faster_graph_could_be_isomorphic` is a deprecated alias for
       `faster_could_be_isomorphic`. Use `faster_could_be_isomorphic` instead.
    r   NzVfaster_graph_could_be_isomorphic is deprecated, use faster_could_be_isomorphic insteadr4   r5   )r8   r9   r:   r   r;   s      r    faster_graph_could_be_isomorphicrO      s0     MM`#  
 &b"--r2   
edge_match
node_match)r   preserve_edge_attrspreserve_node_attrsc                 d   | j                         r5|j                         r%t        j                  j                  j                  }nP| j                         s5|j                         s%t        j                  j                  j
                  }nt        d       || |||      }|j                         S )a2  Returns True if the graphs G1 and G2 are isomorphic and False otherwise.

    Parameters
    ----------
    G1, G2: graphs
        The two graphs G1 and G2 must be the same type.

    node_match : callable
        A function that returns True if node n1 in G1 and n2 in G2 should
        be considered equal during the isomorphism test.
        If node_match is not specified then node attributes are not considered.

        The function will be called like

           node_match(G1.nodes[n1], G2.nodes[n2]).

        That is, the function will receive the node attribute dictionaries
        for n1 and n2 as inputs.

    edge_match : callable
        A function that returns True if the edge attribute dictionary
        for the pair of nodes (u1, v1) in G1 and (u2, v2) in G2 should
        be considered equal during the isomorphism test.  If edge_match is
        not specified then edge attributes are not considered.

        The function will be called like

           edge_match(G1[u1][v1], G2[u2][v2]).

        That is, the function will receive the edge attribute dictionaries
        of the edges under consideration.

    Notes
    -----
    Uses the vf2 algorithm [1]_.

    Examples
    --------
    >>> import networkx.algorithms.isomorphism as iso

    For digraphs G1 and G2, using 'weight' edge attribute (default: 1)

    >>> G1 = nx.DiGraph()
    >>> G2 = nx.DiGraph()
    >>> nx.add_path(G1, [1, 2, 3, 4], weight=1)
    >>> nx.add_path(G2, [10, 20, 30, 40], weight=2)
    >>> em = iso.numerical_edge_match("weight", 1)
    >>> nx.is_isomorphic(G1, G2)  # no weights considered
    True
    >>> nx.is_isomorphic(G1, G2, edge_match=em)  # match weights
    False

    For multidigraphs G1 and G2, using 'fill' node attribute (default: '')

    >>> G1 = nx.MultiDiGraph()
    >>> G2 = nx.MultiDiGraph()
    >>> G1.add_nodes_from([1, 2, 3], fill="red")
    >>> G2.add_nodes_from([10, 20, 30, 40], fill="red")
    >>> nx.add_path(G1, [1, 2, 3, 4], weight=3, linewidth=2.5)
    >>> nx.add_path(G2, [10, 20, 30, 40], weight=3)
    >>> nm = iso.categorical_node_match("fill", "red")
    >>> nx.is_isomorphic(G1, G2, node_match=nm)
    True

    For multidigraphs G1 and G2, using 'weight' edge attribute (default: 7)

    >>> G1.add_edge(1, 2, weight=7)
    1
    >>> G2.add_edge(10, 20)
    1
    >>> em = iso.numerical_multiedge_match("weight", 7, rtol=1e-6)
    >>> nx.is_isomorphic(G1, G2, edge_match=em)
    True

    For multigraphs G1 and G2, using 'weight' and 'linewidth' edge attributes
    with default values 7 and 2.5. Also using 'fill' node attribute with
    default value 'red'.

    >>> em = iso.numerical_multiedge_match(["weight", "linewidth"], [7, 2.5])
    >>> nm = iso.categorical_node_match("fill", "red")
    >>> nx.is_isomorphic(G1, G2, edge_match=em, node_match=nm)
    True

    See Also
    --------
    numerical_node_match, numerical_edge_match, numerical_multiedge_match
    categorical_node_match, categorical_edge_match, categorical_multiedge_match

    References
    ----------
    .. [1]  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
    z*Graphs G1 and G2 are not of the same type.)rQ   rP   )is_directedr+   
algorithmsisomorphismDiGraphMatcherGraphMatcherr   r   )r
   r   rQ   rP   GMgms         r   r   r      s    L 
~~BNN,]]&&55nn)9]]&&33HII	Bzj	ABr2   )NN)__doc__r-   collectionsr   networkxr+   networkx.exceptionr   __all___dispatchabler   r<   r   rG   r   rO   r   r   r2   r   <module>rb      s       , +,.3 S -Sl'" +," -"J," +, -:." 1$$
j
jr2   