ó ŸÃÒYc@s‘dZddlmZddlmZddlZddlmZdj dgƒZ d d gZ dd „Z dd „Zdd „Zd„ZdS(sB Cuthill-McKee ordering of graph nodes to produce sparse matrices iÿÿÿÿ(tdeque(t itemgetterNi(tarbitrary_elements s%Aric Hagberg tcuthill_mckee_orderingtreverse_cuthill_mckee_orderingccsFx?tj|ƒD].}x%t|j|ƒ|ƒD] }|Vq/WqWdS(sò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) # doctest: +SKIP 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 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(tnxtconnected_componentst connected_cuthill_mckee_orderingtsubgraph(tGt heuristictctn((sh/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/utils/rcm.pyRs5cCsttt|d|ƒƒƒS(sGenerate 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) # doctest: +SKIP 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 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 (treversedtlistR(R R ((sh/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/utils/rcm.pyRMs5c csÏ|dkrt|ƒ}n ||ƒ}|h}t|gƒ}x‰|rÊ|jƒ}|Vtt|jt||ƒ|ƒƒdtdƒƒ}g|D]\}}|^q•} |j | ƒ|j | ƒqBWdS(Ntkeyi( tNonetpseudo_peripheral_nodeRtpoplefttsortedRtdegreetsetRtupdatetextend( R R tstarttvisitedtqueuetparenttndR tdtchildren((sh/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/utils/rcm.pyR…s     & cs¬t|ƒ}d}|}xtr§ttj||ƒƒ}t|jƒƒ‰ˆ|kr[Pnˆ}‡fd†|jƒDƒ}t|j |ƒdt dƒƒ\}}qW|S(Nic3s'|]\}}|ˆkr|VqdS(N((t.0R tdist(tl(sh/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/utils/rcm.pys £sRi( RtTruetdictRtshortest_path_lengthtmaxtvaluestitemstminRR(R tutlptvtspltfarthesttdeg((R!sh/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/utils/rcm.pyR—s   +(t__doc__t collectionsRtoperatorRtnetworkxRtutilsRtjoint __author__t__all__RRRRR(((sh/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/utils/rcm.pyts   : 8