test_harary_graph.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """Unit tests for the :mod:`networkx.generators.harary_graph` module.
  2. """
  3. import pytest
  4. import networkx as nx
  5. from networkx.algorithms.isomorphism.isomorph import is_isomorphic
  6. from networkx.generators.harary_graph import hkn_harary_graph, hnm_harary_graph
  7. class TestHararyGraph:
  8. """
  9. Suppose n nodes, m >= n-1 edges, d = 2m // n, r = 2m % n
  10. """
  11. def test_hnm_harary_graph(self):
  12. # When d is even and r = 0, the hnm_harary_graph(n,m) is
  13. # the circulant_graph(n, list(range(1,d/2+1)))
  14. for n, m in [(5, 5), (6, 12), (7, 14)]:
  15. G1 = hnm_harary_graph(n, m)
  16. d = 2 * m // n
  17. G2 = nx.circulant_graph(n, list(range(1, d // 2 + 1)))
  18. assert is_isomorphic(G1, G2)
  19. # When d is even and r > 0, the hnm_harary_graph(n,m) is
  20. # the circulant_graph(n, list(range(1,d/2+1)))
  21. # with r edges added arbitrarily
  22. for n, m in [(5, 7), (6, 13), (7, 16)]:
  23. G1 = hnm_harary_graph(n, m)
  24. d = 2 * m // n
  25. G2 = nx.circulant_graph(n, list(range(1, d // 2 + 1)))
  26. assert set(G2.edges) < set(G1.edges)
  27. assert G1.number_of_edges() == m
  28. # When d is odd and n is even and r = 0, the hnm_harary_graph(n,m)
  29. # is the circulant_graph(n, list(range(1,(d+1)/2) plus [n//2])
  30. for n, m in [(6, 9), (8, 12), (10, 15)]:
  31. G1 = hnm_harary_graph(n, m)
  32. d = 2 * m // n
  33. L = list(range(1, (d + 1) // 2))
  34. L.append(n // 2)
  35. G2 = nx.circulant_graph(n, L)
  36. assert is_isomorphic(G1, G2)
  37. # When d is odd and n is even and r > 0, the hnm_harary_graph(n,m)
  38. # is the circulant_graph(n, list(range(1,(d+1)/2) plus [n//2])
  39. # with r edges added arbitrarily
  40. for n, m in [(6, 10), (8, 13), (10, 17)]:
  41. G1 = hnm_harary_graph(n, m)
  42. d = 2 * m // n
  43. L = list(range(1, (d + 1) // 2))
  44. L.append(n // 2)
  45. G2 = nx.circulant_graph(n, L)
  46. assert set(G2.edges) < set(G1.edges)
  47. assert G1.number_of_edges() == m
  48. # When d is odd and n is odd, the hnm_harary_graph(n,m) is
  49. # the circulant_graph(n, list(range(1,(d+1)/2))
  50. # with m - n*(d-1)/2 edges added arbitrarily
  51. for n, m in [(5, 4), (7, 12), (9, 14)]:
  52. G1 = hnm_harary_graph(n, m)
  53. d = 2 * m // n
  54. L = list(range(1, (d + 1) // 2))
  55. G2 = nx.circulant_graph(n, L)
  56. assert set(G2.edges) < set(G1.edges)
  57. assert G1.number_of_edges() == m
  58. # Raise NetworkXError if n<1
  59. n = 0
  60. m = 0
  61. pytest.raises(nx.NetworkXError, hnm_harary_graph, n, m)
  62. # Raise NetworkXError if m < n-1
  63. n = 6
  64. m = 4
  65. pytest.raises(nx.NetworkXError, hnm_harary_graph, n, m)
  66. # Raise NetworkXError if m > n(n-1)/2
  67. n = 6
  68. m = 16
  69. pytest.raises(nx.NetworkXError, hnm_harary_graph, n, m)
  70. """
  71. Suppose connectivity k, number of nodes n
  72. """
  73. def test_hkn_harary_graph(self):
  74. # When k == 1, the hkn_harary_graph(k,n) is
  75. # the path_graph(n)
  76. for k, n in [(1, 6), (1, 7)]:
  77. G1 = hkn_harary_graph(k, n)
  78. G2 = nx.path_graph(n)
  79. assert is_isomorphic(G1, G2)
  80. # When k is even, the hkn_harary_graph(k,n) is
  81. # the circulant_graph(n, list(range(1,k/2+1)))
  82. for k, n in [(2, 6), (2, 7), (4, 6), (4, 7)]:
  83. G1 = hkn_harary_graph(k, n)
  84. G2 = nx.circulant_graph(n, list(range(1, k // 2 + 1)))
  85. assert is_isomorphic(G1, G2)
  86. # When k is odd and n is even, the hkn_harary_graph(k,n) is
  87. # the circulant_graph(n, list(range(1,(k+1)/2)) plus [n/2])
  88. for k, n in [(3, 6), (5, 8), (7, 10)]:
  89. G1 = hkn_harary_graph(k, n)
  90. L = list(range(1, (k + 1) // 2))
  91. L.append(n // 2)
  92. G2 = nx.circulant_graph(n, L)
  93. assert is_isomorphic(G1, G2)
  94. # When k is odd and n is odd, the hkn_harary_graph(k,n) is
  95. # the circulant_graph(n, list(range(1,(k+1)/2))) with
  96. # n//2+1 edges added between node i and node i+n//2+1
  97. for k, n in [(3, 5), (5, 9), (7, 11)]:
  98. G1 = hkn_harary_graph(k, n)
  99. G2 = nx.circulant_graph(n, list(range(1, (k + 1) // 2)))
  100. eSet1 = set(G1.edges)
  101. eSet2 = set(G2.edges)
  102. eSet3 = set()
  103. half = n // 2
  104. for i in range(0, half + 1):
  105. # add half+1 edges between i and i+half
  106. eSet3.add((i, (i + half) % n))
  107. assert eSet1 == eSet2 | eSet3
  108. # Raise NetworkXError if k<1
  109. k = 0
  110. n = 0
  111. pytest.raises(nx.NetworkXError, hkn_harary_graph, k, n)
  112. # Raise NetworkXError if n<k+1
  113. k = 6
  114. n = 6
  115. pytest.raises(nx.NetworkXError, hkn_harary_graph, k, n)