test_coding.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """Unit tests for the :mod:`~networkx.algorithms.tree.coding` module."""
  2. from itertools import product
  3. import pytest
  4. import networkx as nx
  5. from networkx.utils import edges_equal, nodes_equal
  6. class TestPruferSequence:
  7. """Unit tests for the Prüfer sequence encoding and decoding
  8. functions.
  9. """
  10. def test_nontree(self):
  11. with pytest.raises(nx.NotATree):
  12. G = nx.cycle_graph(3)
  13. nx.to_prufer_sequence(G)
  14. def test_null_graph(self):
  15. with pytest.raises(nx.NetworkXPointlessConcept):
  16. nx.to_prufer_sequence(nx.null_graph())
  17. def test_trivial_graph(self):
  18. with pytest.raises(nx.NetworkXPointlessConcept):
  19. nx.to_prufer_sequence(nx.trivial_graph())
  20. def test_bad_integer_labels(self):
  21. with pytest.raises(KeyError):
  22. T = nx.Graph(nx.utils.pairwise("abc"))
  23. nx.to_prufer_sequence(T)
  24. def test_encoding(self):
  25. """Tests for encoding a tree as a Prüfer sequence using the
  26. iterative strategy.
  27. """
  28. # Example from Wikipedia.
  29. tree = nx.Graph([(0, 3), (1, 3), (2, 3), (3, 4), (4, 5)])
  30. sequence = nx.to_prufer_sequence(tree)
  31. assert sequence == [3, 3, 3, 4]
  32. def test_decoding(self):
  33. """Tests for decoding a tree from a Prüfer sequence."""
  34. # Example from Wikipedia.
  35. sequence = [3, 3, 3, 4]
  36. tree = nx.from_prufer_sequence(sequence)
  37. assert nodes_equal(list(tree), list(range(6)))
  38. edges = [(0, 3), (1, 3), (2, 3), (3, 4), (4, 5)]
  39. assert edges_equal(list(tree.edges()), edges)
  40. def test_decoding2(self):
  41. # Example from "An Optimal Algorithm for Prufer Codes".
  42. sequence = [2, 4, 0, 1, 3, 3]
  43. tree = nx.from_prufer_sequence(sequence)
  44. assert nodes_equal(list(tree), list(range(8)))
  45. edges = [(0, 1), (0, 4), (1, 3), (2, 4), (2, 5), (3, 6), (3, 7)]
  46. assert edges_equal(list(tree.edges()), edges)
  47. def test_inverse(self):
  48. """Tests that the encoding and decoding functions are inverses."""
  49. for T in nx.nonisomorphic_trees(4):
  50. T2 = nx.from_prufer_sequence(nx.to_prufer_sequence(T))
  51. assert nodes_equal(list(T), list(T2))
  52. assert edges_equal(list(T.edges()), list(T2.edges()))
  53. for seq in product(range(4), repeat=2):
  54. seq2 = nx.to_prufer_sequence(nx.from_prufer_sequence(seq))
  55. assert list(seq) == seq2
  56. class TestNestedTuple:
  57. """Unit tests for the nested tuple encoding and decoding functions."""
  58. def test_nontree(self):
  59. with pytest.raises(nx.NotATree):
  60. G = nx.cycle_graph(3)
  61. nx.to_nested_tuple(G, 0)
  62. def test_unknown_root(self):
  63. with pytest.raises(nx.NodeNotFound):
  64. G = nx.path_graph(2)
  65. nx.to_nested_tuple(G, "bogus")
  66. def test_encoding(self):
  67. T = nx.full_rary_tree(2, 2**3 - 1)
  68. expected = (((), ()), ((), ()))
  69. actual = nx.to_nested_tuple(T, 0)
  70. assert nodes_equal(expected, actual)
  71. def test_canonical_form(self):
  72. T = nx.Graph()
  73. T.add_edges_from([(0, 1), (0, 2), (0, 3)])
  74. T.add_edges_from([(1, 4), (1, 5)])
  75. T.add_edges_from([(3, 6), (3, 7)])
  76. root = 0
  77. actual = nx.to_nested_tuple(T, root, canonical_form=True)
  78. expected = ((), ((), ()), ((), ()))
  79. assert actual == expected
  80. def test_decoding(self):
  81. balanced = (((), ()), ((), ()))
  82. expected = nx.full_rary_tree(2, 2**3 - 1)
  83. actual = nx.from_nested_tuple(balanced)
  84. assert nx.is_isomorphic(expected, actual)
  85. def test_sensible_relabeling(self):
  86. balanced = (((), ()), ((), ()))
  87. T = nx.from_nested_tuple(balanced, sensible_relabeling=True)
  88. edges = [(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)]
  89. assert nodes_equal(list(T), list(range(2**3 - 1)))
  90. assert edges_equal(list(T.edges()), edges)