comparison env/lib/python3.9/site-packages/networkx/generators/tests/test_expanders.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Unit tests for the :mod:`networkx.generators.expanders` module.
2
3 """
4
5 import networkx as nx
6 from networkx import adjacency_matrix
7 from networkx import number_of_nodes
8 from networkx.generators.expanders import chordal_cycle_graph
9 from networkx.generators.expanders import margulis_gabber_galil_graph
10 from networkx.generators.expanders import paley_graph
11
12 import pytest
13
14
15 def test_margulis_gabber_galil_graph():
16 for n in 2, 3, 5, 6, 10:
17 g = margulis_gabber_galil_graph(n)
18 assert number_of_nodes(g) == n * n
19 for node in g:
20 assert g.degree(node) == 8
21 assert len(node) == 2
22 for i in node:
23 assert int(i) == i
24 assert 0 <= i < n
25
26 np = pytest.importorskip("numpy")
27 scipy = pytest.importorskip("scipy")
28 scipy.linalg = pytest.importorskip("scipy.linalg")
29 # Eigenvalues are already sorted using the scipy eigvalsh,
30 # but the implementation in numpy does not guarantee order.
31 w = sorted(scipy.linalg.eigvalsh(adjacency_matrix(g).A))
32 assert w[-2] < 5 * np.sqrt(2)
33
34
35 def test_chordal_cycle_graph():
36 """Test for the :func:`networkx.chordal_cycle_graph` function."""
37 primes = [3, 5, 7, 11]
38 for p in primes:
39 G = chordal_cycle_graph(p)
40 assert len(G) == p
41 # TODO The second largest eigenvalue should be smaller than a constant,
42 # independent of the number of nodes in the graph:
43 #
44 # eigs = sorted(scipy.linalg.eigvalsh(adjacency_matrix(G).A))
45 # assert_less(eigs[-2], ...)
46 #
47
48
49 def test_paley_graph():
50 """Test for the :func:`networkx.paley_graph` function."""
51 primes = [3, 5, 7, 11, 13]
52 for p in primes:
53 G = paley_graph(p)
54 # G has p nodes
55 assert len(G) == p
56 # G is (p-1)/2-regular
57 in_degrees = {G.in_degree(node) for node in G.nodes}
58 out_degrees = {G.out_degree(node) for node in G.nodes}
59 assert len(in_degrees) == 1 and in_degrees.pop() == (p - 1) // 2
60 assert len(out_degrees) == 1 and out_degrees.pop() == (p - 1) // 2
61
62 # If p = 1 mod 4, -1 is a square mod 4 and therefore the
63 # edge in the Paley graph are symmetric.
64 if p % 4 == 1:
65 for (u, v) in G.edges:
66 assert (v, u) in G.edges
67
68
69 def test_margulis_gabber_galil_graph_badinput():
70 pytest.raises(nx.NetworkXError, margulis_gabber_galil_graph, 3, nx.DiGraph())
71 pytest.raises(nx.NetworkXError, margulis_gabber_galil_graph, 3, nx.Graph())