test_rcm.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import networkx as nx
  2. from networkx.utils import reverse_cuthill_mckee_ordering
  3. def test_reverse_cuthill_mckee():
  4. # example graph from
  5. # http://www.boost.org/doc/libs/1_37_0/libs/graph/example/cuthill_mckee_ordering.cpp
  6. G = nx.Graph(
  7. [
  8. (0, 3),
  9. (0, 5),
  10. (1, 2),
  11. (1, 4),
  12. (1, 6),
  13. (1, 9),
  14. (2, 3),
  15. (2, 4),
  16. (3, 5),
  17. (3, 8),
  18. (4, 6),
  19. (5, 6),
  20. (5, 7),
  21. (6, 7),
  22. ]
  23. )
  24. rcm = list(reverse_cuthill_mckee_ordering(G))
  25. assert rcm in [[0, 8, 5, 7, 3, 6, 2, 4, 1, 9], [0, 8, 5, 7, 3, 6, 4, 2, 1, 9]]
  26. def test_rcm_alternate_heuristic():
  27. # example from
  28. G = nx.Graph(
  29. [
  30. (0, 0),
  31. (0, 4),
  32. (1, 1),
  33. (1, 2),
  34. (1, 5),
  35. (1, 7),
  36. (2, 2),
  37. (2, 4),
  38. (3, 3),
  39. (3, 6),
  40. (4, 4),
  41. (5, 5),
  42. (5, 7),
  43. (6, 6),
  44. (7, 7),
  45. ]
  46. )
  47. answers = [
  48. [6, 3, 5, 7, 1, 2, 4, 0],
  49. [6, 3, 7, 5, 1, 2, 4, 0],
  50. [7, 5, 1, 2, 4, 0, 6, 3],
  51. ]
  52. def smallest_degree(G):
  53. deg, node = min((d, n) for n, d in G.degree())
  54. return node
  55. rcm = list(reverse_cuthill_mckee_ordering(G, heuristic=smallest_degree))
  56. assert rcm in answers