o
    ˷e<                     @   sT   d Z ddgZddlZdddZdd Zddd	Zd
d Zdd Zdd Z	d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    Ngraphc                 c   s    | dk rt tt| d d ttd| d d  }|durEt|}|dur?|dkr2t|V  n	|dkr;t|V  t|}|dus dS dS )a  Returns a list of nonisomporphic trees

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

    create : graph or matrix (default="Graph)
      If graph is selected a list of trees will be returned,
      if matrix is selected a list of adjancency matrix will
      be returned

    Returns
    -------
    G : List of NetworkX Graphs

    M : List of Adjacency matrices

    References
    ----------

          Nr   matrix)
ValueErrorlistrange
_next_tree_layout_to_graph_layout_to_matrix_next_rooted_tree)ordercreatelayout r   ^/var/www/ideatree/venv/lib/python3.10/site-packages/networkx/generators/nonisomorphic_trees.pyr      s   *
c                 C   s   t dd t| D S )zReturns the number of nonisomorphic trees

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

    Returns
    -------
    length : Number of nonisomorphic graphs for the given order

    References
    ----------

    c                 s   s    | ]}d V  qdS )r   Nr   ).0_r   r   r   	<genexpr>F   s    z0number_of_nonisomorphic_trees.<locals>.<genexpr>)sumr   )r   r   r   r   r   6   s   c                 C   s   |du rt | d }| | dkr|d8 }| | dks|dkr dS |d }| | | | d kr<|d8 }| | | | d ks.t| }t|t |D ]}||| |  ||< qG|S )z0One iteration of the Beyer-Hedetniemi algorithm.Nr   r   )lenr	   r
   )predecessorpqresultir   r   r   r   I   s   r   c                 C   s   t | \}}t|}t|}||k}|r1||kr1t|t|kr#d}nt|t|kr1||kr1d}|r5| S t|}t| |}| | dkr^t |\}}	t|}
td|
d }||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suffixr   r   r   r   \   s&   
r   c                    s   d}d}t t D ]} | dkr|r|} nd}q
|du r#t } fddt d|D }dg fddt |t D  }||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                    s   g | ]} | d  qS )r   r   r   r   r   r   r   
<listcomp>   s    z_split_tree.<locals>.<listcomp>r   c                    s   g | ]} | qS r   r   r+   r,   r   r   r-      s    )r
   r   )r   	one_foundmr   r!   r"   r   r,   r   r      s   "r   c                    s    fddt t D }g }t t D ]5} | }|rE|d } | }||kr9|  |d } | }||ks)d || |< || |< || q|S )z\Create the adjacency matrix for the tree specified by the
    given layout (level sequence).c                    s   g | ]	}d gt   qS )r   )r   r+   r,   r   r   r-      s    z%_layout_to_matrix.<locals>.<listcomp>r   )r
   r   popappend)r   r   stackr   i_leveljj_levelr   r,   r   r      s   r   c                 C   s|   t  }g }tt| D ]/}| | }|r6|d }| | }||kr0|  |d }| | }||ks ||| || q|S )zVCreate a NetworkX Graph for the tree specified by the
    given layout(level sequence)r0   )nxGraphr
   r   r1   add_edger2   )r   Gr3   r   r4   r5   r6   r   r   r   r      s   r   )r   )N)__doc____all__networkxr7   r   r   r   r   r   r   r   r   r   r   r   <module>   s    	
'
'