o
    h5                     @   sn   d Z ddgZddlZejd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    NT)graphsreturns_graphgraphc                 c   s    | dk rt tt| d d ttd| d d  }|durQt|}|durK|dkr2t|V  n|dkrGddl}|jdtdd t|V  t	|}|dus dS dS )	a  Generates lists of nonisomorphic trees

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

    create : one of {"graph", "matrix"} (default="graph")
       If ``"graph"`` is selected a list of ``Graph`` instances will be returned,
       if matrix is selected a list of adjacency matrices will be returned.

       .. deprecated:: 3.3

          The `create` argument is deprecated and will be removed in NetworkX
          version 3.5. In the future, `nonisomorphic_trees` will yield graph
          instances by default. To generate adjacency matrices, call
          ``nx.to_numpy_array`` on the output, e.g.::

             [nx.to_numpy_array(G) for G in nx.nonisomorphic_trees(N)]

    Yields
    ------
    list
       A list of nonisomorphic trees, in one of two formats depending on the
       value of the `create` parameter:
       - ``create="graph"``: yields a list of `networkx.Graph` instances
       - ``create="matrix"``: yields a list of list-of-lists representing adjacency matrices
          Nr   matrixr   z

The 'create=matrix' argument of nonisomorphic_trees
is deprecated and will be removed in version 3.5.
Use ``nx.to_numpy_array`` to convert graphs to adjacency matrices, e.g.::

   [nx.to_numpy_array(G) for G in nx.nonisomorphic_trees(N)])category
stacklevel)

ValueErrorlistrange
_next_tree_layout_to_graphwarningswarnDeprecationWarning_layout_to_matrix_next_rooted_tree)ordercreatelayoutr    r   [/var/www/vscode/kcb/lib/python3.10/site-packages/networkx/generators/nonisomorphic_trees.pyr      s&   *
)r   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>\   s    z0number_of_nonisomorphic_trees.<locals>.<genexpr>)sumr   )r   r   r   r   r   K   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   _   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   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   r2   r3   r   r   r4      s    )r   r   )r   	one_foundmr$   r(   r)   r   r3   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   r2   r3   r   r   r4      s    z%_layout_to_matrix.<locals>.<listcomp>r   )r   r   popappend)r   r#   stackr$   i_leveljj_levelr   r3   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)r7   )nxGraphr   r   r8   add_edger9   )r   Gr:   r$   r;   r<   r=   r   r   r   r      s   r   )r   )N)__doc____all__networkxr>   _dispatchabler   r   r   r   r%   r   r   r   r   r   r   <module>   s    	
;

'