
    fiN                         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dS )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   }|5t          |          }| t	          |          V  t          |          }|3dS dS )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     /var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/networkx/generators/nonisomorphic_trees.pyr   r      s       qyy%
Q''((4a%!)9I0J0J+K+KKF

F##"6*****&v..F	 




    )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!    " qyyE"""r   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   s     E1q5\\ 9 9q!!M!a%$8$8881uzzqAv&&&eqj((r   c                     | dk     r| S d}t          d|           D ]F}t          d|           D ]3}||z  dk    r(||t          |          z  t          | |z
            z  z  }4G|| 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     	1uuE1a[[ E Eq! 	E 	EA1uzz]1---a!e0D0DDD	E QUr   c                 `   |/t          |           dz
  }| |         dk    r|dz  }| |         dk    |dk    rdS |dz
  }| |         | |         dz
  k    r|dz  }| |         | |         dz
  k    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!!Avvt	AA
a.KNQ.
.
.	Q a.KNQ.
.
.+F1c&kk"" & &1q519%q		Mr   c                    t          |           \  }}t          |          }t          |          }||k    }|rQ||k    rKt          |          t          |          k    rd}n(t          |          t          |          k    r||k    rd}|r| S t          |          }t          | |          }| |         dk    rIt          |          \  }}	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 ++ t99s4yy  EE YY#d))##tE  II))Q77Q<!!,]!;!;Hh!(mmO1o122F,2M3v;;,..)r   c                 .    d}d}t          t                               D ]} |         dk    r|r|} nd}|t                     } fdt          d|          D             }dg fdt          |t                               D             z   }||fS )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
   Tc                 &    g | ]}|         d z
  S )r
    .0r&   r   s     r   
<listcomp>z_split_tree.<locals>.<listcomp>   s!    ///aF1IM///r   r   c                      g | ]
}|         S r7   r7   r8   s     r   r:   z_split_tree.<locals>.<listcomp>   s    ;;;&);;;r   )r   r!   )r   	one_foundmr&   r+   r,   s   `     r   r(   r(      s    
 IA3v;; ! !!9>> ! 	yKK////5A;;///D3;;;;U1c&kk%:%:;;;;D$<r   c                      fdt          t                               D             }g }t          t                               D ]w} |         }|rV|d         } |         }||k    r*|                                 |d         } |         }||k    *dx||         |<   ||         |<   |                    |           x|S )z\Create the adjacency matrix for the tree specified by the
    given layout (level sequence).c                 6    g | ]}d gt                    z  S )r   )r!   r8   s     r   r:   z%_layout_to_matrix.<locals>.<listcomp>   s&    <<<AqcCKK<<<r   r
   )r   r!   popappend)r   r%   stackr&   i_levelr   j_levels   `      r   _layout_to_matrixrF      s     =<<<s6{{););<<<FE3v;; 
 
) 	,b	AQiGW$$		"I ) W$$ +,+F1IaL6!9Q<QMr   c                 X   t          j                    }g }t          t          |                     D ]w}| |         }|rV|d         }| |         }||k    r*|                                 |d         }| |         }||k    *|                    ||           |                    |           x|S )zVCreate a NetworkX Graph for the tree specified by the
    given layout(level sequence)r@   )nxGraphr   r!   rA   add_edgerB   )r   GrC   r&   rD   r   rE   s          r   r   r      s     	

AE3v;; 
 
) 	b	AQiGW$$		"I ) W$$ JJq!QHr   )N)__doc____all__	functoolsr   networkxrH   _dispatchabler   r   r   r   r   r   r(   rF   r   r7   r   r   <module>rQ      s5    !"A
B           T222/ / 32/4 # # #* 4) ) ) 4
 
 
   &$ $ $N  .  (    r   