test_chains.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. """Unit tests for the chain decomposition functions."""
  2. from itertools import cycle, islice
  3. import pytest
  4. import networkx as nx
  5. def cycles(seq):
  6. """Yields cyclic permutations of the given sequence.
  7. For example::
  8. >>> list(cycles("abc"))
  9. [('a', 'b', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b')]
  10. """
  11. n = len(seq)
  12. cycled_seq = cycle(seq)
  13. for x in seq:
  14. yield tuple(islice(cycled_seq, n))
  15. next(cycled_seq)
  16. def cyclic_equals(seq1, seq2):
  17. """Decide whether two sequences are equal up to cyclic permutations.
  18. For example::
  19. >>> cyclic_equals("xyz", "zxy")
  20. True
  21. >>> cyclic_equals("xyz", "zyx")
  22. False
  23. """
  24. # Cast seq2 to a tuple since `cycles()` yields tuples.
  25. seq2 = tuple(seq2)
  26. return any(x == tuple(seq2) for x in cycles(seq1))
  27. class TestChainDecomposition:
  28. """Unit tests for the chain decomposition function."""
  29. def assertContainsChain(self, chain, expected):
  30. # A cycle could be expressed in two different orientations, one
  31. # forward and one backward, so we need to check for cyclic
  32. # equality in both orientations.
  33. reversed_chain = list(reversed([tuple(reversed(e)) for e in chain]))
  34. for candidate in expected:
  35. if cyclic_equals(chain, candidate):
  36. break
  37. if cyclic_equals(reversed_chain, candidate):
  38. break
  39. else:
  40. self.fail("chain not found")
  41. def test_decomposition(self):
  42. edges = [
  43. # DFS tree edges.
  44. (1, 2),
  45. (2, 3),
  46. (3, 4),
  47. (3, 5),
  48. (5, 6),
  49. (6, 7),
  50. (7, 8),
  51. (5, 9),
  52. (9, 10),
  53. # Nontree edges.
  54. (1, 3),
  55. (1, 4),
  56. (2, 5),
  57. (5, 10),
  58. (6, 8),
  59. ]
  60. G = nx.Graph(edges)
  61. expected = [
  62. [(1, 3), (3, 2), (2, 1)],
  63. [(1, 4), (4, 3)],
  64. [(2, 5), (5, 3)],
  65. [(5, 10), (10, 9), (9, 5)],
  66. [(6, 8), (8, 7), (7, 6)],
  67. ]
  68. chains = list(nx.chain_decomposition(G, root=1))
  69. assert len(chains) == len(expected)
  70. # This chain decomposition isn't unique
  71. # for chain in chains:
  72. # print(chain)
  73. # self.assertContainsChain(chain, expected)
  74. def test_barbell_graph(self):
  75. # The (3, 0) barbell graph has two triangles joined by a single edge.
  76. G = nx.barbell_graph(3, 0)
  77. chains = list(nx.chain_decomposition(G, root=0))
  78. expected = [[(0, 1), (1, 2), (2, 0)], [(3, 4), (4, 5), (5, 3)]]
  79. assert len(chains) == len(expected)
  80. for chain in chains:
  81. self.assertContainsChain(chain, expected)
  82. def test_disconnected_graph(self):
  83. """Test for a graph with multiple connected components."""
  84. G = nx.barbell_graph(3, 0)
  85. H = nx.barbell_graph(3, 0)
  86. mapping = dict(zip(range(6), "abcdef"))
  87. nx.relabel_nodes(H, mapping, copy=False)
  88. G = nx.union(G, H)
  89. chains = list(nx.chain_decomposition(G))
  90. expected = [
  91. [(0, 1), (1, 2), (2, 0)],
  92. [(3, 4), (4, 5), (5, 3)],
  93. [("a", "b"), ("b", "c"), ("c", "a")],
  94. [("d", "e"), ("e", "f"), ("f", "d")],
  95. ]
  96. assert len(chains) == len(expected)
  97. for chain in chains:
  98. self.assertContainsChain(chain, expected)
  99. def test_disconnected_graph_root_node(self):
  100. """Test for a single component of a disconnected graph."""
  101. G = nx.barbell_graph(3, 0)
  102. H = nx.barbell_graph(3, 0)
  103. mapping = dict(zip(range(6), "abcdef"))
  104. nx.relabel_nodes(H, mapping, copy=False)
  105. G = nx.union(G, H)
  106. chains = list(nx.chain_decomposition(G, root="a"))
  107. expected = [
  108. [("a", "b"), ("b", "c"), ("c", "a")],
  109. [("d", "e"), ("e", "f"), ("f", "d")],
  110. ]
  111. assert len(chains) == len(expected)
  112. for chain in chains:
  113. self.assertContainsChain(chain, expected)
  114. def test_chain_decomposition_root_not_in_G(self):
  115. """Test chain decomposition when root is not in graph"""
  116. G = nx.Graph()
  117. G.add_nodes_from([1, 2, 3])
  118. with pytest.raises(nx.NodeNotFound):
  119. nx.has_bridges(G, root=6)