test_expanders.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """Unit tests for the :mod:`networkx.generators.expanders` module.
  2. """
  3. import pytest
  4. import networkx as nx
  5. @pytest.mark.parametrize("n", (2, 3, 5, 6, 10))
  6. def test_margulis_gabber_galil_graph_properties(n):
  7. g = nx.margulis_gabber_galil_graph(n)
  8. assert g.number_of_nodes() == n * n
  9. for node in g:
  10. assert g.degree(node) == 8
  11. assert len(node) == 2
  12. for i in node:
  13. assert int(i) == i
  14. assert 0 <= i < n
  15. @pytest.mark.parametrize("n", (2, 3, 5, 6, 10))
  16. def test_margulis_gabber_galil_graph_eigvals(n):
  17. np = pytest.importorskip("numpy")
  18. sp = pytest.importorskip("scipy")
  19. import scipy.linalg
  20. g = nx.margulis_gabber_galil_graph(n)
  21. # Eigenvalues are already sorted using the scipy eigvalsh,
  22. # but the implementation in numpy does not guarantee order.
  23. w = sorted(sp.linalg.eigvalsh(nx.adjacency_matrix(g).toarray()))
  24. assert w[-2] < 5 * np.sqrt(2)
  25. @pytest.mark.parametrize("p", (3, 5, 7, 11)) # Primes
  26. def test_chordal_cycle_graph(p):
  27. """Test for the :func:`networkx.chordal_cycle_graph` function."""
  28. G = nx.chordal_cycle_graph(p)
  29. assert len(G) == p
  30. # TODO The second largest eigenvalue should be smaller than a constant,
  31. # independent of the number of nodes in the graph:
  32. #
  33. # eigs = sorted(sp.linalg.eigvalsh(nx.adjacency_matrix(G).toarray()))
  34. # assert_less(eigs[-2], ...)
  35. #
  36. @pytest.mark.parametrize("p", (3, 5, 7, 11, 13)) # Primes
  37. def test_paley_graph(p):
  38. """Test for the :func:`networkx.paley_graph` function."""
  39. G = nx.paley_graph(p)
  40. # G has p nodes
  41. assert len(G) == p
  42. # G is (p-1)/2-regular
  43. in_degrees = {G.in_degree(node) for node in G.nodes}
  44. out_degrees = {G.out_degree(node) for node in G.nodes}
  45. assert len(in_degrees) == 1 and in_degrees.pop() == (p - 1) // 2
  46. assert len(out_degrees) == 1 and out_degrees.pop() == (p - 1) // 2
  47. # If p = 1 mod 4, -1 is a square mod 4 and therefore the
  48. # edge in the Paley graph are symmetric.
  49. if p % 4 == 1:
  50. for u, v in G.edges:
  51. assert (v, u) in G.edges
  52. @pytest.mark.parametrize("graph_type", (nx.Graph, nx.DiGraph, nx.MultiDiGraph))
  53. def test_margulis_gabber_galil_graph_badinput(graph_type):
  54. with pytest.raises(
  55. nx.NetworkXError, match="`create_using` must be an undirected multigraph"
  56. ):
  57. nx.margulis_gabber_galil_graph(3, create_using=graph_type)
  58. @pytest.mark.parametrize("graph_type", (nx.Graph, nx.DiGraph, nx.MultiDiGraph))
  59. def test_chordal_cycle_graph_badinput(graph_type):
  60. with pytest.raises(
  61. nx.NetworkXError, match="`create_using` must be an undirected multigraph"
  62. ):
  63. nx.chordal_cycle_graph(3, create_using=graph_type)
  64. def test_paley_graph_badinput():
  65. with pytest.raises(
  66. nx.NetworkXError, match="`create_using` cannot be a multigraph."
  67. ):
  68. nx.paley_graph(3, create_using=nx.MultiGraph)