
    PiQ                         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              #   z  K   | dk     rt          d          | dk    rdS | dk    rt          j        d          V  dS t          t	          | dz  dz                       t          t	          d| dz   dz                      z   }|5t          |          }| t          |          V  t          |          }|3dS dS )a  Generate nonisomorphic trees of specified `order`.

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

    Yields
    ------
    `networkx.Graph` instances
       A tree with `order` number of nodes that is not isomorphic to any other
       yielded tree.

    Raises
    ------
    ValueError
       If `order` is negative.

    Examples
    --------
    There are 11 unique (non-isomorphic) trees with 7 nodes.

    >>> n = 7
    >>> nit_list = list(nx.nonisomorphic_trees(n))
    >>> len(nit_list) == nx.number_of_nonisomorphic_trees(n) == 11
    True

    All trees yielded by the generator have the specified order.

    >>> all(len(G) == n for G in nx.nonisomorphic_trees(n))
    True

    Each tree is nonisomorphic to every other tree yielded by the generator.
    >>> seen = []
    >>> for G in nx.nonisomorphic_trees(n):
    ...     assert not any(nx.is_isomorphic(G, H) for H in seen)
    ...     seen.append(G)

    See Also
    --------
    number_of_nonisomorphic_trees
    r   order must be non-negativeN      )
ValueErrornxempty_graphlistrange
_next_tree_layout_to_graph_next_rooted_tree)orderlayouts     {/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/networkx/generators/nonisomorphic_trees.pyr   r      s      X qyy5666zzzznQ%
Q''((4a%!)9I0J0J+K+KKF

F##"6*****&v..F	 




    )r   c                 J    | dk     rt          d          t          |           S )aK  Returns the number of nonisomorphic trees of the specified `order`.

    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 trees with `order` number of nodes.

    Raises
    ------
    ValueError
       If `order` is negative.

    Examples
    --------
    >>> nx.number_of_nonisomorphic_trees(10)
    106

    See Also
    --------
    nonisomorphic_trees
    r   r	   )r   _unlabeled_trees)r   s    r   r   r   P   s*    > qyy5666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   t   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      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      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 r:   r:   r;   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$   r;   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_matrixrI      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)rC   )r   Graphr   r$   rD   add_edgerE   )r   GrF   r)   rG   r!   rH   s          r   r   r      s     	

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