U
    ?h<                     @   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k	r~t|}|dk	r6|dkrbt|V  n|dkrtt|V  t|}q6d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   Y/var/www/html/venv/lib/python3.8/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kr*t | d }| | dkr*|d8 }q|dkr6dS |d }| | | | d kr\|d8 }q>t| }t|t |D ]}||| |  ||< qr|S )z0One iteration of the Beyer-Hedetniemi algorithm.Nr   r   )lenr	   r
   )Zpredecessorpqresultir   r   r   r   I   s    

r   c                 C   s   t | \}}t|}t|}||k}|rb||krbt|t|krFd}nt|t|krb||krbd}|rj| S t|}t| |}| | dkrt |\}}	t|}
td|
d }||t| d< |S 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|r2|} q8qd}q|dkrHt } 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 ]d} | }|r|d } | }||krl|  |d } | }qJd || |< || |< || 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   sv   t  }g }tt| D ]X}| | }|rf|d }| | }||krZ|  |d }| | }q8||| || 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.   r   r   r   r   r   r   r   r   r   r   r   <module>   s   	
'
'