ó
    †~i
  ã                   ó`   • S r SSKJr  SSKJr  SSKrSSKJr  SS/r	SS	 jr
SS
 jrSS jrS rg)zB
Cuthill-McKee ordering of graph nodes to produce sparse matrices
é    )Údeque)Ú
itemgetterNé   )Úarbitrary_elementÚcuthill_mckee_orderingÚreverse_cuthill_mckee_orderingc              #   ó�   #   • [         R                  " U 5       H&  n[        U R                  U5      U5       Sh  v•N   M(     g N	7f)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      ÚO/home/mande/repo/quber/.venv/lib/python3.13/site-packages/networkx/utils/rcm.pyr   r      s8   é € ôj ×$Ò$ QÖ'ˆÜ3°A·J±J¸q³MÀ9ÓM×MÒMò (ÙMùs   ‚8AºA»
Ac           	      ó8   • [        [        [        XS95      5      $ )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              #   ó~  #   • Uc  [        U 5      nOU" U 5      nU1n[        U/5      nU(       a†  UR                  5       nUv •  [        U R	                  [        X   5      U-
  5      [        S5      S9nU VVs/ s H  u  pxUPM	     n	nnUR                  U	5        UR                  U	5        U(       a  M…  g g s  snnf 7f)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Ó)‰á˜!“ˆØˆg€GÜ�5�'‹N€EÞ
Ø—‘“ˆØŠÜ�A—H‘HœS ¡›^¨gÑ5Ó6¼JÀq»MÑJˆÙ"$Ô%¢"™$˜!“A¡"ˆÑ%Ø�‰�xÔ Ø�‰�XÔ÷ Š%ùó &ùs   ‚A7B=Á9B7Â,B=Â5B=c                 ó  ^• [        U 5      nSnUn [        R                  " X5      n[        UR	                  5       5      mTU::  a   U$ TnU4S jUR                  5        5       n[        U R                  U5      [        S5      S9u  p6Mz  )Nr   c              3   ó<   >#   • U  H  u  pUT:X  d  M  Uv •  M     g 7f©N© )Ú.0r&   ÚdistÚls      €r   Ú	<genexpr>Ú)pseudo_peripheral_node.<locals>.<genexpr>�   s   øé € Ð>¢[™'˜!°D¸A±I—A‘A¢[ùs   ƒ“	r   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ð ˆÜ> S§Y¡Y¤[Ó>ˆÜ�Q—X‘X˜hÓ'¬Z¸«]Ñ;‰ˆñ r   r+   )Ú__doc__Úcollectionsr   Úoperatorr   Únetworkxr
   Úutilsr   Ú__all__r   r   r   r   r,   r   r   Ú<module>rC      s;   ðñõ Ý ã å %à#Ð%EÐ
F€ô6Nôr5Jôpó"r   