
    ܛ7iQ                         d Z ddgZddlmZ ddlZ ej                  dd      d        Z ej                  d	      d
        Z ed      d        Z	 ed      d        Z
ddZd Zd Zd Zd Zy)a  
Implementation of the Wright, Richmond, Odlyzko and McKay (WROM)
algorithm for the enumeration of all non-isomorphic free trees of a
given order.  Rooted trees are represented by level sequences, i.e.,
lists in which the i-th element specifies the distance of vertex i to
the root.

nonisomorphic_treesnumber_of_nonisomorphic_trees    )	lru_cacheNT)graphsreturns_graphc              #   4  K   | dk  rt        d      | dk(  ry| dk(  rt        j                  d       yt        t	        | dz  dz               t        t	        d| dz   dz              z   }|)t        |      }|t        |       t        |      }|(yyw)a  Generate nonisomorphic trees of specified `order`.

    Parameters
    ----------
    order : int
       order of the desired tree(s)

    Yields
    ------
    `networkx.Graph` instances
       A tree with `order` number of nodes that is not isomorphic to any other
       yielded tree.

    Raises
    ------
    ValueError
       If `order` is negative.

    Examples
    --------
    There are 11 unique (non-isomorphic) trees with 7 nodes.

    >>> n = 7
    >>> nit_list = list(nx.nonisomorphic_trees(n))
    >>> len(nit_list) == nx.number_of_nonisomorphic_trees(n) == 11
    True

    All trees yielded by the generator have the specified order.

    >>> all(len(G) == n for G in nx.nonisomorphic_trees(n))
    True

    Each tree is nonisomorphic to every other tree yielded by the generator.
    >>> seen = []
    >>> for G in nx.nonisomorphic_trees(n):
    ...     assert not any(nx.is_isomorphic(G, H) for H in seen)
    ...     seen.append(G)

    See Also
    --------
    number_of_nonisomorphic_trees
    r   order must be non-negativeN      )
ValueErrornxempty_graphlistrange
_next_tree_layout_to_graph_next_rooted_tree)orderlayouts     d/home/rose/Desktop/poly/venv/lib/python3.12/site-packages/networkx/generators/nonisomorphic_trees.pyr   r      s     X qy566zznnQ%
Q'(4a%!)9I0J+KKF

F#"6**&v.F	 
s   BBB)r   c                 8    | dk  rt        d      t        |       S )aK  Returns the number of nonisomorphic trees of the specified `order`.

    Based on an algorithm by Alois P. Heinz in
    `OEIS entry A000055 <https://oeis.org/A000055>`_. Complexity is ``O(n ** 3)``.

    Parameters
    ----------
    order : int
       Order of the desired tree(s).

    Returns
    -------
    int
       Number of nonisomorphic trees with `order` number of nodes.

    Raises
    ------
    ValueError
       If `order` is negative.

    Examples
    --------
    >>> nx.number_of_nonisomorphic_trees(10)
    106

    See Also
    --------
    nonisomorphic_trees
    r   r	   )r   _unlabeled_trees)r   s    r   r   r   P   s"    > qy566E""    c                     d}t        | dz         D ]  }|t        |      t        | |z
        z  z  }! | dz  dk(  r|t        | dz        z  }t        |       |dz  z
  S )z4Implements OEIS A000055 (number of unlabeled trees).r   r
   r   r   _rooted_trees)nvalueks      r   r   r   t   sj     E1q5\q!M!a%$888 1uzqAv&&eqj((r   c                     | dk  r| S d}t        d|       D ]<  }t        d|       D ]+  }||z  dk(  s||t        |      z  t        | |z
        z  z  }- > || dz
  z  S )z;Implements OEIS A000081 (number of unlabeled rooted trees).r   r   r
   r   )r   r   jds       r   r   r      ss     	1uE1a[q!A1uz]1--a!e0DDD   QUr   c                    |$t        |       dz
  }| |   dk(  r|dz  }| |   dk(  r|dk(  ry|dz
  }| |   | |   dz
  k7  r|dz  }| |   | |   dz
  k7  rt        |       }t        |t        |            D ]  }|||z
  |z      ||<    |S )z0One iteration of the Beyer-Hedetniemi algorithm.Nr
   r   )lenr   r   )predecessorpqresultis        r   r   r      s     	yq !n!FA !n!Av	AA
a.KNQ.
.	Q a.KNQ.
.+F1c&k"1q519%q	 #Mr   c                    t        |       \  }}t        |      }t        |      }||k\  }|r=||k(  r8t        |      t        |      kD  rd}nt        |      t        |      k(  r||kD  rd}|r| S t        |      }t        | |      }| |   dkD  r7t        |      \  }}	t        |      }
t	        d|
dz         }||t        |       d |S )zGOne iteration of the Wright, Richmond, Odlyzko and McKay
    algorithm.Fr   r
   N)_split_treemaxr$   r   r   )	candidateleftrestleft_heightrest_heightvalidr&   new_candidatenew_leftnew_restnew_left_heightsuffixs               r   r   r      s     Y'JD$ d)Kd)K;&E+ t9s4y E Y#d)#tE I))Q7Q<!!,]!;Hh!(mO1o12F,2M3v;,.)r   c                 &   d}d}t        t        |             D ]  }| |   dk(  s|r|} nd} |t        |       }t        d|      D cg c]
  }| |   dz
   }}dgt        |t        |             D cg c]  }| |   	 c}z   }||fS c c}w c c}w )zReturns a tuple of two layouts, one containing the left
    subtree of the root vertex, and one containing the original tree
    with the left subtree removed.FNr
   Tr   )r   r$   )r   	one_foundmr)   r.   r/   s         r   r+   r+      s    
 IA3v;!9> 	   	yK#(A;/;aF1IM;D/3U1c&k%:;%:&)%:;;D$< 0;s   
B	4Bc                 P   t        t        |             D cg c]  }dgt        |       z   }}g }t        t        |             D ]Y  }| |   }|r?|d   }| |   }||k\  r |j                          |d   }| |   }||k\  r dx||   |<   ||   |<   |j                  |       [ |S c c}w )z\Create the adjacency matrix for the tree specified by the
    given layout (level sequence).r   r
   )r   r$   popappend)r   r)   r(   stacki_levelr!   j_levels          r   _layout_to_matrixrB      s     */s6{);<);AqcCK);F<E3v;)b	AQiGW$		"I ) W$ +,+F1IaL6!9Q<Q   M =s   B#c                    t        j                         }g }t        t        |             D ][  }| |   }|rA|d   }| |   }||k\  r |j	                          |d   }| |   }||k\  r |j                  ||       |j                  |       ] |S )zVCreate a NetworkX Graph for the tree specified by the
    given layout(level sequence)r<   )r   Graphr   r$   r=   add_edger>   )r   Gr?   r)   r@   r!   rA   s          r   r   r      s     	
AE3v;)b	AQiGW$		"I ) W$ JJq!Q   Hr   )N)__doc____all__	functoolsr   networkxr   _dispatchabler   r   r   r   r   r   r+   rB   r    r   r   <module>rM      s    !"A
B   T2;/ 3;/|  #  #F 4) ) 4
 
&$N.(r   