test_directed.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. """Generators - Directed Graphs
  2. ----------------------------
  3. """
  4. import pytest
  5. import networkx as nx
  6. from networkx.classes import Graph, MultiDiGraph
  7. from networkx.generators.directed import (
  8. gn_graph,
  9. gnc_graph,
  10. gnr_graph,
  11. random_k_out_graph,
  12. random_uniform_k_out_graph,
  13. scale_free_graph,
  14. )
  15. class TestGeneratorsDirected:
  16. def test_smoke_test_random_graphs(self):
  17. gn_graph(100)
  18. gnr_graph(100, 0.5)
  19. gnc_graph(100)
  20. scale_free_graph(100)
  21. gn_graph(100, seed=42)
  22. gnr_graph(100, 0.5, seed=42)
  23. gnc_graph(100, seed=42)
  24. scale_free_graph(100, seed=42)
  25. def test_create_using_keyword_arguments(self):
  26. pytest.raises(nx.NetworkXError, gn_graph, 100, create_using=Graph())
  27. pytest.raises(nx.NetworkXError, gnr_graph, 100, 0.5, create_using=Graph())
  28. pytest.raises(nx.NetworkXError, gnc_graph, 100, create_using=Graph())
  29. pytest.raises(nx.NetworkXError, scale_free_graph, 100, create_using=Graph())
  30. G = gn_graph(100, seed=1)
  31. MG = gn_graph(100, create_using=MultiDiGraph(), seed=1)
  32. assert sorted(G.edges()) == sorted(MG.edges())
  33. G = gnr_graph(100, 0.5, seed=1)
  34. MG = gnr_graph(100, 0.5, create_using=MultiDiGraph(), seed=1)
  35. assert sorted(G.edges()) == sorted(MG.edges())
  36. G = gnc_graph(100, seed=1)
  37. MG = gnc_graph(100, create_using=MultiDiGraph(), seed=1)
  38. assert sorted(G.edges()) == sorted(MG.edges())
  39. G = scale_free_graph(
  40. 100,
  41. alpha=0.3,
  42. beta=0.4,
  43. gamma=0.3,
  44. delta_in=0.3,
  45. delta_out=0.1,
  46. initial_graph=nx.cycle_graph(4, create_using=MultiDiGraph),
  47. seed=1,
  48. )
  49. pytest.raises(ValueError, scale_free_graph, 100, 0.5, 0.4, 0.3)
  50. pytest.raises(ValueError, scale_free_graph, 100, alpha=-0.3)
  51. pytest.raises(ValueError, scale_free_graph, 100, beta=-0.3)
  52. pytest.raises(ValueError, scale_free_graph, 100, gamma=-0.3)
  53. def test_parameters(self):
  54. G = nx.DiGraph()
  55. G.add_node(0)
  56. def kernel(x):
  57. return x
  58. assert nx.is_isomorphic(gn_graph(1), G)
  59. assert nx.is_isomorphic(gn_graph(1, kernel=kernel), G)
  60. assert nx.is_isomorphic(gnc_graph(1), G)
  61. assert nx.is_isomorphic(gnr_graph(1, 0.5), G)
  62. def test_scale_free_graph_create_using_with_initial_graph():
  63. G = nx.MultiGraph()
  64. with pytest.raises(
  65. ValueError,
  66. match="Cannot set both create_using and initial_graph. Set create_using=None.",
  67. ):
  68. scale_free_graph(10, create_using=nx.Graph, initial_graph=G)
  69. def test_scale_free_graph_negative_delta():
  70. with pytest.raises(ValueError, match="delta_in must be >= 0."):
  71. scale_free_graph(10, create_using=None, delta_in=-1)
  72. with pytest.raises(ValueError, match="delta_out must be >= 0."):
  73. scale_free_graph(10, create_using=None, delta_out=-1)
  74. def test_non_numeric_ordering():
  75. G = MultiDiGraph([("a", "b"), ("b", "c"), ("c", "a")])
  76. s = scale_free_graph(3, initial_graph=G)
  77. assert len(s) == 3
  78. assert len(s.edges) == 3
  79. @pytest.mark.parametrize("ig", (nx.Graph(), nx.DiGraph([(0, 1)])))
  80. def test_scale_free_graph_initial_graph_kwarg(ig):
  81. with pytest.raises(nx.NetworkXError):
  82. scale_free_graph(100, initial_graph=ig)
  83. class TestRandomKOutGraph:
  84. """Unit tests for the
  85. :func:`~networkx.generators.directed.random_k_out_graph` function.
  86. """
  87. def test_regularity(self):
  88. """Tests that the generated graph is `k`-out-regular."""
  89. n = 10
  90. k = 3
  91. alpha = 1
  92. G = random_k_out_graph(n, k, alpha)
  93. assert all(d == k for v, d in G.out_degree())
  94. G = random_k_out_graph(n, k, alpha, seed=42)
  95. assert all(d == k for v, d in G.out_degree())
  96. def test_no_self_loops(self):
  97. """Tests for forbidding self-loops."""
  98. n = 10
  99. k = 3
  100. alpha = 1
  101. G = random_k_out_graph(n, k, alpha, self_loops=False)
  102. assert nx.number_of_selfloops(G) == 0
  103. def test_negative_alpha(self):
  104. with pytest.raises(ValueError, match="alpha must be positive"):
  105. random_k_out_graph(10, 3, -1)
  106. class TestUniformRandomKOutGraph:
  107. """Unit tests for the
  108. :func:`~networkx.generators.directed.random_uniform_k_out_graph`
  109. function.
  110. """
  111. def test_regularity(self):
  112. """Tests that the generated graph is `k`-out-regular."""
  113. n = 10
  114. k = 3
  115. G = random_uniform_k_out_graph(n, k)
  116. assert all(d == k for v, d in G.out_degree())
  117. G = random_uniform_k_out_graph(n, k, seed=42)
  118. assert all(d == k for v, d in G.out_degree())
  119. def test_no_self_loops(self):
  120. """Tests for forbidding self-loops."""
  121. n = 10
  122. k = 3
  123. G = random_uniform_k_out_graph(n, k, self_loops=False)
  124. assert nx.number_of_selfloops(G) == 0
  125. assert all(d == k for v, d in G.out_degree())
  126. def test_with_replacement(self):
  127. n = 10
  128. k = 3
  129. G = random_uniform_k_out_graph(n, k, with_replacement=True)
  130. assert G.is_multigraph()
  131. assert all(d == k for v, d in G.out_degree())
  132. n = 10
  133. k = 9
  134. G = random_uniform_k_out_graph(n, k, with_replacement=False, self_loops=False)
  135. assert nx.number_of_selfloops(G) == 0
  136. assert all(d == k for v, d in G.out_degree())
  137. def test_without_replacement(self):
  138. n = 10
  139. k = 3
  140. G = random_uniform_k_out_graph(n, k, with_replacement=False)
  141. assert not G.is_multigraph()
  142. assert all(d == k for v, d in G.out_degree())