
    ިsg5                         d Z ddgZddlZ ej                  dd      dd       Z ej                  d      d	        Zdd
Zd Zd Z	d Z
d Zy)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_graphc              #   L  K   | dk  rt         t        t        | dz  dz               t        t        d| dz   dz              z   }|]t        |      }|L|dk(  rt	        |       n.|dk(  r)ddl}|j                  dt        d       t        |       t        |      }|\yyw)	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
          Ngraph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   s       Z/var/www/html/venv/lib/python3.12/site-packages/networkx/generators/nonisomorphic_trees.pyr   r      s     > qy%
Q'(4a%!)9I0J+KKF

F# &v..8#W 0   
 (//&v.F+ 
s   BB$"B$)r   c                 8    t        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              3       K   | ]  }d   yw)r	   N ).0_s     r   	<genexpr>z0number_of_nonisomorphic_trees.<locals>.<genexpr>\   s     5Qq5s   )sumr   )r   s    r   r   r   K   s    " 5-e4555    c                    |$t        |       dz
  }| |   dk(  r|dz  }| |   dk(  r|dk(  ry|dz
  }| |   | |   dz
  k7  r|dz  }| |   | |   dz
  k7  r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!Av	AA
a.KNQ.
.	Q a.KNQ.
.+F1c&k" &1q519%q	&Mr#   c                    t        |       \  }}t        |      }t        |      }||k\  }|r=||k(  r8t        |      t        |      kD  rd}nt        |      t        |      k(  r||kD  rd}|r| S t        |      }t        | |      }| |   dkD  r7t        |      \  }}	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   r   s     Y'JD$ d)Kd)K;&E+ t9s4y E Y#d)#tE I))Q7Q<!!,]!;Hh!(mO1o12F,2M3v;,.)r#   c                 &   d}d}t        t        |             D ]  }| |   dk(  s|r|} nd} |t        |       }t        d|      D cg c]
  }| |   dz
   }}dgt        |t        |             D cg c]  }| |   	 c}z   }||fS c c}w c c}w )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	   Tr   )r   r%   )r   	one_foundmr*   r/   r0   s         r   r,   r,      s    
 IA3v; !!9> 	! 	yK#(A;/aF1IM/D/3U1c&k%:;&);;D$< 0;s   
B	4Bc                 P   t        t        |             D cg c]  }dgt        |       z   }}g }t        t        |             D ]Y  }| |   }|r?|d   }| |   }||k\  r |j                          |d   }| |   }||k\  r dx||   |<   ||   |<   |j                  |       [ |S c c}w )z\Create the adjacency matrix for the tree specified by the
    given layout (level sequence).r   r	   )r   r%   popappend)r   r*   r)   stacki_leveljj_levels          r   r   r      s     */s6{);<AqcCK<F<E3v; 
)b	AQiGW$		"I ) W$ +,+F1IaL6!9Q<Q
 M =s   B#c                    t        j                         }g }t        t        |             D ][  }| |   }|rA|d   }| |   }||k\  r |j	                          |d   }| |   }||k\  r |j                  ||       |j                  |       ] |S )zVCreate a NetworkX Graph for the tree specified by the
    given layout(level sequence)r=   )nxGraphr   r%   r>   add_edger?   )r   Gr@   r*   rA   rB   rC   s          r   r   r      s     	
AE3v; 
)b	AQiGW$		"I ) W$ JJq!Q
 Hr#   )r
   )N)__doc____all__networkxrE   _dispatchabler   r   r   r   r,   r   r   r   r#   r   <module>rM      sy    !"A
B  T28/ 38/v 6 6&&$N.(r#   