Generate an ordering (permutation) of the graph nodes to make a sparse matrix.
Uses the Cuthill-McKee heuristic (based on breadth-first search) [R332].
Parameters: | G : graph
heuristic : function, optional
|
---|---|
Returns: | nodes : generator
|
See also
Notes
The optimal solution the the bandwidth reduction is NP-complete [R333].
References
[R332] | (1, 2) 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 |
[R333] | (1, 2) Steven S. Skiena. 1997. The Algorithm Design Manual. Springer-Verlag New York, Inc., New York, NY, USA. |
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):
... node,deg = sorted(G.degree().items(), key = lambda x:x[1])[0]
... return node
>>> rcm = list(cuthill_mckee_ordering(G, heuristic=smallest_degree))