o
    &?ep                     @   sl   d Z ddgZddlZejdddddZejdd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)Zgraphs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 nonisomorphic 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 adjacency 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   h/home/www/facesmatcher.com/pyenv/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>H   s    z0number_of_nonisomorphic_trees.<locals>.<genexpr>)sumr   )r   r   r   r   r   7   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
   )Zpredecessorpqresultir   r   r   r   K   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restZleft_heightZrest_heightZvalidr   Znew_candidateZnew_leftZnew_restZnew_left_heightsuffixr   r   r   r   ^   s&   
r   c                    s   d}d}t t D ]} | dkr|r|} q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   Z	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)r'   )nxZGraphr
   r   r(   Zadd_edger)   )r   Gr*   r   r+   r,   r-   r   r   r   r      s   r   )r   )N)__doc____all__Znetworkxr.   	_dispatchr   r   r   r   r   r   r   r   r   r   r   <module>   s    	

'

'