
    shN                         S r SS/rSSKJr  SSKr\R                  " SSS9S 5       r\R                  " SS	9S
 5       r\" S5      S 5       r	\" S5      S 5       r
SS jrS rS rS rS rg)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              #      #    U S:  a  [         e[        [        U S-  S-   5      5      [        [        SU S-   S-  5      5      -   nUb,  [        U5      nUb  [	        U5      v   [        U5      nUb  M+  gg7f)zGenerates lists of nonisomorphic trees

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

Yields
------
list of `networkx.Graph` instances
   A list of nonisomorphic trees
      N)
ValueErrorlistrange
_next_tree_layout_to_graph_next_rooted_tree)orderlayouts     {/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/networkx/generators/nonisomorphic_trees.pyr   r      sx      qy%
Q'(4a%!)9I0J+KKF

F#"6**&v.F	 
s   A/A53A5)r   c                 0    U S:  a  [         e[        U 5      $ )a1  Returns the number of nonisomorphic trees

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 graphs for the given order
r	   )r   _unlabeled_trees)r   s    r   r   r   ,   s    " qyE""    c                     Sn[        U S-   5       H  nU[        U5      [        X-
  5      -  -  nM!     U S-  S:X  a  U[        U S-  5      -  n[        U 5      US-  -
  $ )z4Implements OEIS A000055 (number of unlabeled trees).r   r
   r	   r   _rooted_trees)nvalueks      r   r   r   B   sh     E1q5\q!M!%$888 1uzqAv&&eqj((r   c                     U S:  a  U $ Sn[        SU 5       H>  n[        SU 5       H+  nX#-  S:X  d  M  X[        U5      -  [        X-
  5      -  -  nM-     M@     XS-
  -  $ )z;Implements OEIS A000081 (number of unlabeled rooted trees).r	   r   r
   r   )r   r   jds       r   r   r   N   sk     	1uE1a[q!Auz]1--ae0DDD   Ur   c                    Uc%  [        U 5      S-
  nX   S:X  a  US-  nX   S:X  a  M  US:X  a  gUS-
  nX   X   S-
  :w  a  US-  nX   X   S-
  :w  a  M  [        U 5      n[        U[        U5      5       H  nX4U-
  U-      X4'   M     U$ )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
.KNQ.
.	Q .KNQ.
.+F1c&k"q519%	 #Mr   c                    [        U 5      u  p[        U5      n[        U5      nXC:  nU(       a?  XC:X  a:  [        U5      [        U5      :  a  SnO[        U5      [        U5      :X  a  X:  a  SnU(       a  U $ [        U5      n[        X5      nX   S:  a5  [        U5      u  p[        U5      n
[	        SU
S-   5      nX[        U5      * S& U$ )zCOne 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   o   s     Y'JD d)Kd)K&E+ t9s4y E Y#d)#E I))7<!!,]!;H!(mO1o12F,23v;,.)r   c                 >   SnSn[        [        U 5      5       H  nX   S:X  d  M  U(       a  Un  OSnM     Uc  [        U 5      n[        SU5       Vs/ s H
  o0U   S-
  PM     nnS/[        U[        U 5      5       Vs/ s H  o0U   PM	     sn-   nXE4$ s  snf s  snf )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;/;a1IM;D/3U1c&k%:;%:)%:;;D< 0;s   B?Bc                 \   [        [        U 5      5       Vs/ s H  nS/[        U 5      -  PM     nn/ n[        [        U 5      5       H[  nX   nU(       a<  US   nX   nXd:  a   UR                  5         US   nX   nXd:  a  M   S=X!   U'   X%   U'   UR                  U5        M]     U$ s  snf )zXCreate 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_matrixr?      s     */s6{);<);AqcCK);F<E3v;)b	AiG$		"I ) $ +,+FIaL69Q<Q   M =s   B)c                     [         R                  " 5       n/ n[        [        U 5      5       H^  nX   nU(       a?  US   nX   nXd:  a   UR	                  5         US   nX   nXd:  a  M   UR                  X55        UR                  U5        M`     U$ )zRCreate a NetworkX Graph for the tree specified by the
given layout(level sequence)r9   )nxGraphr   r!   r:   add_edger;   )r   Gr<   r&   r=   r   r>   s          r   r   r      s     	
AE3v;)b	AiG$		"I ) $ JJqQ   Hr   )N)__doc____all__	functoolsr   networkxrA   _dispatchabler   r   r   r   r   r   r(   r?   r    r   r   <module>rK      s    !"A
B   T2/ 3/4 # #* 4) ) 4
 
&$N.(r   