test_tournament.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """Unit tests for the :mod:`networkx.algorithms.tournament` module."""
  2. from itertools import combinations
  3. import pytest
  4. from networkx import DiGraph
  5. from networkx.algorithms.tournament import (
  6. hamiltonian_path,
  7. index_satisfying,
  8. is_reachable,
  9. is_strongly_connected,
  10. is_tournament,
  11. random_tournament,
  12. score_sequence,
  13. tournament_matrix,
  14. )
  15. def test_condition_not_satisfied():
  16. condition = lambda x: x > 0
  17. iter_in = [0]
  18. assert index_satisfying(iter_in, condition) == 1
  19. def test_empty_iterable():
  20. condition = lambda x: x > 0
  21. with pytest.raises(ValueError):
  22. index_satisfying([], condition)
  23. def test_is_tournament():
  24. G = DiGraph()
  25. G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)])
  26. assert is_tournament(G)
  27. def test_self_loops():
  28. """A tournament must have no self-loops."""
  29. G = DiGraph()
  30. G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)])
  31. G.add_edge(0, 0)
  32. assert not is_tournament(G)
  33. def test_missing_edges():
  34. """A tournament must not have any pair of nodes without at least
  35. one edge joining the pair.
  36. """
  37. G = DiGraph()
  38. G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3)])
  39. assert not is_tournament(G)
  40. def test_bidirectional_edges():
  41. """A tournament must not have any pair of nodes with greater
  42. than one edge joining the pair.
  43. """
  44. G = DiGraph()
  45. G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)])
  46. G.add_edge(1, 0)
  47. assert not is_tournament(G)
  48. def test_graph_is_tournament():
  49. for _ in range(10):
  50. G = random_tournament(5)
  51. assert is_tournament(G)
  52. def test_graph_is_tournament_seed():
  53. for _ in range(10):
  54. G = random_tournament(5, seed=1)
  55. assert is_tournament(G)
  56. def test_graph_is_tournament_one_node():
  57. G = random_tournament(1)
  58. assert is_tournament(G)
  59. def test_graph_is_tournament_zero_node():
  60. G = random_tournament(0)
  61. assert is_tournament(G)
  62. def test_hamiltonian_empty_graph():
  63. path = hamiltonian_path(DiGraph())
  64. assert len(path) == 0
  65. def test_path_is_hamiltonian():
  66. G = DiGraph()
  67. G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)])
  68. path = hamiltonian_path(G)
  69. assert len(path) == 4
  70. assert all(v in G[u] for u, v in zip(path, path[1:]))
  71. def test_hamiltonian_cycle():
  72. """Tests that :func:`networkx.tournament.hamiltonian_path`
  73. returns a Hamiltonian cycle when provided a strongly connected
  74. tournament.
  75. """
  76. G = DiGraph()
  77. G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)])
  78. path = hamiltonian_path(G)
  79. assert len(path) == 4
  80. assert all(v in G[u] for u, v in zip(path, path[1:]))
  81. assert path[0] in G[path[-1]]
  82. def test_score_sequence_edge():
  83. G = DiGraph([(0, 1)])
  84. assert score_sequence(G) == [0, 1]
  85. def test_score_sequence_triangle():
  86. G = DiGraph([(0, 1), (1, 2), (2, 0)])
  87. assert score_sequence(G) == [1, 1, 1]
  88. def test_tournament_matrix():
  89. np = pytest.importorskip("numpy")
  90. pytest.importorskip("scipy")
  91. npt = np.testing
  92. G = DiGraph([(0, 1)])
  93. m = tournament_matrix(G)
  94. npt.assert_array_equal(m.todense(), np.array([[0, 1], [-1, 0]]))
  95. def test_reachable_pair():
  96. """Tests for a reachable pair of nodes."""
  97. G = DiGraph([(0, 1), (1, 2), (2, 0)])
  98. assert is_reachable(G, 0, 2)
  99. def test_same_node_is_reachable():
  100. """Tests that a node is always reachable from it."""
  101. # G is an arbitrary tournament on ten nodes.
  102. G = DiGraph(sorted(p) for p in combinations(range(10), 2))
  103. assert all(is_reachable(G, v, v) for v in G)
  104. def test_unreachable_pair():
  105. """Tests for an unreachable pair of nodes."""
  106. G = DiGraph([(0, 1), (0, 2), (1, 2)])
  107. assert not is_reachable(G, 1, 0)
  108. def test_is_strongly_connected():
  109. """Tests for a strongly connected tournament."""
  110. G = DiGraph([(0, 1), (1, 2), (2, 0)])
  111. assert is_strongly_connected(G)
  112. def test_not_strongly_connected():
  113. """Tests for a tournament that is not strongly connected."""
  114. G = DiGraph([(0, 1), (0, 2), (1, 2)])
  115. assert not is_strongly_connected(G)