
    VhN                         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              #      K   | dk  rt         t        t        | dz  dz               t        t        d| dz   dz              z   }|)t        |      }|t	        |       t        |      }|(yyw)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     W/home/dcms/DCMS/lib/python3.12/site-packages/networkx/generators/nonisomorphic_trees.pyr   r      sy      qy%
Q'(4a%!)9I0J+KKF

F#"6**&v.F	 
s   A+A0.A0)r   c                 .    | dk  rt         t        |       S )a]  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                     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   B   so     E1q5\ 9q!M!a%$8889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   N   s     	1uE1a[ Eq! 	EA1uz]1--a!e0DDD	EE 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   o   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_matrixr?      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)r9   )nxGraphr   r!   r:   add_edger;   )r   Gr<   r&   r=   r   r>   s          r   r   r      s     	
AE3v; 
)b	AQiGW$		"I ) W$ 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   