plot_rcm.py 969 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. """
  2. ===
  3. Rcm
  4. ===
  5. Cuthill-McKee ordering of matrices
  6. The reverse Cuthill-McKee algorithm gives a sparse matrix ordering that
  7. reduces the matrix bandwidth.
  8. """
  9. # Copyright (C) 2011-2019 by
  10. # Author: Aric Hagberg <aric.hagberg@gmail.com>
  11. # BSD License
  12. import networkx as nx
  13. from networkx.utils import reverse_cuthill_mckee_ordering
  14. import numpy as np
  15. # build low-bandwidth numpy matrix
  16. G = nx.grid_2d_graph(3, 3)
  17. rcm = list(reverse_cuthill_mckee_ordering(G))
  18. print("ordering", rcm)
  19. print("unordered Laplacian matrix")
  20. A = nx.laplacian_matrix(G)
  21. x, y = np.nonzero(A)
  22. #print("lower bandwidth:",(y-x).max())
  23. #print("upper bandwidth:",(x-y).max())
  24. print("bandwidth: %d" % ((y - x).max() + (x - y).max() + 1))
  25. print(A)
  26. B = nx.laplacian_matrix(G, nodelist=rcm)
  27. print("low-bandwidth Laplacian matrix")
  28. x, y = np.nonzero(B)
  29. #print("lower bandwidth:",(y-x).max())
  30. #print("upper bandwidth:",(x-y).max())
  31. print("bandwidth: %d" % ((y - x).max() + (x - y).max() + 1))
  32. print(B)