test_polynomials.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """Unit tests for the :mod:`networkx.algorithms.polynomials` module."""
  2. import pytest
  3. import networkx as nx
  4. sympy = pytest.importorskip("sympy")
  5. # Mapping of input graphs to a string representation of their tutte polynomials
  6. _test_tutte_graphs = {
  7. nx.complete_graph(1): "1",
  8. nx.complete_graph(4): "x**3 + 3*x**2 + 4*x*y + 2*x + y**3 + 3*y**2 + 2*y",
  9. nx.cycle_graph(5): "x**4 + x**3 + x**2 + x + y",
  10. nx.diamond_graph(): "x**3 + 2*x**2 + 2*x*y + x + y**2 + y",
  11. }
  12. _test_chromatic_graphs = {
  13. nx.complete_graph(1): "x",
  14. nx.complete_graph(4): "x**4 - 6*x**3 + 11*x**2 - 6*x",
  15. nx.cycle_graph(5): "x**5 - 5*x**4 + 10*x**3 - 10*x**2 + 4*x",
  16. nx.diamond_graph(): "x**4 - 5*x**3 + 8*x**2 - 4*x",
  17. nx.path_graph(5): "x**5 - 4*x**4 + 6*x**3 - 4*x**2 + x",
  18. }
  19. @pytest.mark.parametrize(("G", "expected"), _test_tutte_graphs.items())
  20. def test_tutte_polynomial(G, expected):
  21. assert nx.tutte_polynomial(G).equals(expected)
  22. @pytest.mark.parametrize("G", _test_tutte_graphs.keys())
  23. def test_tutte_polynomial_disjoint(G):
  24. """Tutte polynomial factors into the Tutte polynomials of its components.
  25. Verify this property with the disjoint union of two copies of the input graph.
  26. """
  27. t_g = nx.tutte_polynomial(G)
  28. H = nx.disjoint_union(G, G)
  29. t_h = nx.tutte_polynomial(H)
  30. assert sympy.simplify(t_g * t_g).equals(t_h)
  31. @pytest.mark.parametrize(("G", "expected"), _test_chromatic_graphs.items())
  32. def test_chromatic_polynomial(G, expected):
  33. assert nx.chromatic_polynomial(G).equals(expected)
  34. @pytest.mark.parametrize("G", _test_chromatic_graphs.keys())
  35. def test_chromatic_polynomial_disjoint(G):
  36. """Chromatic polynomial factors into the Chromatic polynomials of its
  37. components. Verify this property with the disjoint union of two copies of
  38. the input graph.
  39. """
  40. x_g = nx.chromatic_polynomial(G)
  41. H = nx.disjoint_union(G, G)
  42. x_h = nx.chromatic_polynomial(H)
  43. assert sympy.simplify(x_g * x_g).equals(x_h)