+
    )i
                     b    R t ^ RIHt ^ RIHt ^ RIt^RIHt RR.t	RR lt
RR ltRR	 ltR
 tR# )zB
Cuthill-McKee ordering of graph nodes to produce sparse matrices
)deque)
itemgetterN)arbitrary_elementcuthill_mckee_orderingreverse_cuthill_mckee_orderingc              #     "   \         P                  ! V 4       F&  p\        V P                  V4      V4       Rj  xL
  K(  	  R#  L
5i)aA  Generate an ordering (permutation) of the graph nodes to make
a sparse matrix.

Uses the Cuthill-McKee heuristic (based on breadth-first search) [1]_.

Parameters
----------
G : graph
  A NetworkX graph

heuristic : function, optional
  Function to choose starting node for RCM algorithm.  If None
  a node from a pseudo-peripheral pair is used.  A user-defined function
  can be supplied that takes a graph object and returns a single node.

Returns
-------
nodes : generator
   Generator of nodes in Cuthill-McKee ordering.

Examples
--------
>>> from networkx.utils import cuthill_mckee_ordering
>>> G = nx.path_graph(4)
>>> rcm = list(cuthill_mckee_ordering(G))
>>> A = nx.adjacency_matrix(G, nodelist=rcm)

Smallest degree node as heuristic function:

>>> def smallest_degree(G):
...     return min(G, key=G.degree)
>>> rcm = list(cuthill_mckee_ordering(G, heuristic=smallest_degree))


See Also
--------
reverse_cuthill_mckee_ordering

Notes
-----
The optimal solution the bandwidth reduction is NP-complete [2]_.


References
----------
.. [1] E. Cuthill and J. McKee.
   Reducing the bandwidth of sparse symmetric matrices,
   In Proc. 24th Nat. Conf. ACM, pages 157-172, 1969.
   http://doi.acm.org/10.1145/800195.805928
.. [2]  Steven S. Skiena. 1997. The Algorithm Design Manual.
   Springer-Verlag New York, Inc., New York, NY, USA.
N)nxconnected_components connected_cuthill_mckee_orderingsubgraph)G	heuristiccs   && P/var/www/html/photoedit/myenv/lib/python3.14/site-packages/networkx/utils/rcm.pyr   r      s8     j $$Q'3AJJqM9MMM (Ms   8AAAc           	     >    \        \        \        WR7      4      4      # )a_  Generate an ordering (permutation) of the graph nodes to make
a sparse matrix.

Uses the reverse Cuthill-McKee heuristic (based on breadth-first search)
[1]_.

Parameters
----------
G : graph
  A NetworkX graph

heuristic : function, optional
  Function to choose starting node for RCM algorithm.  If None
  a node from a pseudo-peripheral pair is used.  A user-defined function
  can be supplied that takes a graph object and returns a single node.

Returns
-------
nodes : generator
   Generator of nodes in reverse Cuthill-McKee ordering.

Examples
--------
>>> from networkx.utils import reverse_cuthill_mckee_ordering
>>> G = nx.path_graph(4)
>>> rcm = list(reverse_cuthill_mckee_ordering(G))
>>> A = nx.adjacency_matrix(G, nodelist=rcm)

Smallest degree node as heuristic function:

>>> def smallest_degree(G):
...     return min(G, key=G.degree)
>>> rcm = list(reverse_cuthill_mckee_ordering(G, heuristic=smallest_degree))


See Also
--------
cuthill_mckee_ordering

Notes
-----
The optimal solution the bandwidth reduction is NP-complete [2]_.

References
----------
.. [1] E. Cuthill and J. McKee.
   Reducing the bandwidth of sparse symmetric matrices,
   In Proc. 24th Nat. Conf. ACM, pages 157-72, 1969.
   http://doi.acm.org/10.1145/800195.805928
.. [2]  Steven S. Skiena. 1997. The Algorithm Design Manual.
   Springer-Verlag New York, Inc., New York, NY, USA.
)r   )reversedlistr   )r   r   s   &&r   r   r   H   s    j D/GHII    c              #     "   Vf   \        V 4      pMV! V 4      pV0p\        V.4      pV'       d   VP                  4       pVx  \        V P	                  \        W,          4      V,
          4      \        ^4      R7      pV UUu. uF  w  rxVNK	  	  p	ppVP                  V	4       VP                  V	4       K  R # u uppi 5i)Nkey)	pseudo_peripheral_noder   popleftsorteddegreesetr   updateextend)
r   r   startvisitedqueueparentndndchildrens
   &&        r   r
   r
      s     &q)!gG5'NE
AHHS^g56JqMJ"$%"$!A"%x X  &s   /CACB</Cc                    a \        V 4      p^ pTp \        P                  ! W4      p\        VP	                  4       4      oSV8:  d    V# SpV3R lVP                  4        4       p\        V P                  V4      \        ^4      R7      w  r6K  )    c              3   >   <"   T F  w  rVS8X  g   K  Vx  K  	  R # 5iN ).0r#   distls   &  r   	<genexpr>)pseudo_peripheral_node.<locals>.<genexpr>   s     >['!DAIAA[s   
r   )	r   r   shortest_path_lengthmaxvaluesitemsminr   r   )r   ulpvsplfarthestdegr-   s   &      @r   r   r      s     	!A	
B	A
%%a+

7 H >SYY[>QXXh'Z];3r   r)   )__doc__collectionsr   operatorr   networkxr   utilsr   __all__r   r   r
   r   r*   r   r   <module>rA      s;       %#%E
F6Nr5Jp"r   