K i ZdZddlmZddlmZddlZddlmZddgZ d d Z d d Z d d Z d Z y)zB Cuthill-McKee ordering of graph nodes to produce sparse matrices )deque) itemgetterN)arbitrary_elementcuthill_mckee_orderingreverse_cuthill_mckee_orderingc#Ktj|D]%}t|j||Ed{'y7w)aGenerate 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)nxconnected_components connected_cuthill_mckee_orderingsubgraph)G heuristiccs X/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/utils/rcm.pyrrs@j $ $Q 'N3AJJqM9MMMNMs7AAAc@ttt||S)aGenerate 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)reversedlistr)rrs rrrHsj D/YGH IIc#jK| t|}n||}|h}t|g}|r|j}|t|j t |||z t d}|Dcgc]\}}| } }}|j| |j| |ryycc}}ww)Nkey) pseudo_peripheral_noderpopleftsorteddegreesetrupdateextend) rrstartvisitedqueueparentndndchildrens rr r s&q)! gG 5'NE  AHHS6^g56JqM J"$%$!QA%%x  X &sA5B37 B-'B3+B3ct|}d}|} tj||}t|j |kr |S}fd|j D}t |j|td\}}{)Nrc34K|]\}}|k(s |ywN).0r&distls r z)pseudo_peripheral_node..s>'!TDAIA>s rr) rr shortest_path_lengthmaxvaluesitemsminrr)rulpvsplfarthestdegr/s @rrrs !A B A %%a+   7  H>SYY[>QXXh'Z];3 rr+)__doc__ collectionsroperatorrnetworkxr utilsr__all__rrr rr,rrrBs;% #%E F6Nr5Jp" r