aboutsummaryrefslogtreecommitdiff
path: root/.venv/lib/python3.12/site-packages/networkx/algorithms/tests/test_graphical.py
blob: 99f766f799d8573e80d905482f4b685a2d16bcc0 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import pytest

import networkx as nx


def test_valid_degree_sequence1():
    n = 100
    p = 0.3
    for i in range(10):
        G = nx.erdos_renyi_graph(n, p)
        deg = (d for n, d in G.degree())
        assert nx.is_graphical(deg, method="eg")
        assert nx.is_graphical(deg, method="hh")


def test_valid_degree_sequence2():
    n = 100
    for i in range(10):
        G = nx.barabasi_albert_graph(n, 1)
        deg = (d for n, d in G.degree())
        assert nx.is_graphical(deg, method="eg")
        assert nx.is_graphical(deg, method="hh")


def test_string_input():
    pytest.raises(nx.NetworkXException, nx.is_graphical, [], "foo")
    pytest.raises(nx.NetworkXException, nx.is_graphical, ["red"], "hh")
    pytest.raises(nx.NetworkXException, nx.is_graphical, ["red"], "eg")


def test_non_integer_input():
    pytest.raises(nx.NetworkXException, nx.is_graphical, [72.5], "eg")
    pytest.raises(nx.NetworkXException, nx.is_graphical, [72.5], "hh")


def test_negative_input():
    assert not nx.is_graphical([-1], "hh")
    assert not nx.is_graphical([-1], "eg")


class TestAtlas:
    @classmethod
    def setup_class(cls):
        global atlas
        from networkx.generators import atlas

        cls.GAG = atlas.graph_atlas_g()

    def test_atlas(self):
        for graph in self.GAG:
            deg = (d for n, d in graph.degree())
            assert nx.is_graphical(deg, method="eg")
            assert nx.is_graphical(deg, method="hh")


def test_small_graph_true():
    z = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
    assert nx.is_graphical(z, method="hh")
    assert nx.is_graphical(z, method="eg")
    z = [10, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2]
    assert nx.is_graphical(z, method="hh")
    assert nx.is_graphical(z, method="eg")
    z = [1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
    assert nx.is_graphical(z, method="hh")
    assert nx.is_graphical(z, method="eg")


def test_small_graph_false():
    z = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
    assert not nx.is_graphical(z, method="hh")
    assert not nx.is_graphical(z, method="eg")
    z = [6, 5, 4, 4, 2, 1, 1, 1]
    assert not nx.is_graphical(z, method="hh")
    assert not nx.is_graphical(z, method="eg")
    z = [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
    assert not nx.is_graphical(z, method="hh")
    assert not nx.is_graphical(z, method="eg")


def test_directed_degree_sequence():
    # Test a range of valid directed degree sequences
    n, r = 100, 10
    p = 1.0 / r
    for i in range(r):
        G = nx.erdos_renyi_graph(n, p * (i + 1), None, True)
        din = (d for n, d in G.in_degree())
        dout = (d for n, d in G.out_degree())
        assert nx.is_digraphical(din, dout)


def test_small_directed_sequences():
    dout = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
    din = [3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1]
    assert nx.is_digraphical(din, dout)
    # Test nongraphical directed sequence
    dout = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
    din = [103, 102, 102, 102, 102, 102, 102, 102, 102, 102]
    assert not nx.is_digraphical(din, dout)
    # Test digraphical small sequence
    dout = [1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
    din = [2, 2, 2, 2, 2, 2, 2, 2, 1, 1]
    assert nx.is_digraphical(din, dout)
    # Test nonmatching sum
    din = [2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1]
    assert not nx.is_digraphical(din, dout)
    # Test for negative integer in sequence
    din = [2, 2, 2, -2, 2, 2, 2, 2, 1, 1, 4]
    assert not nx.is_digraphical(din, dout)
    # Test for noninteger
    din = dout = [1, 1, 1.1, 1]
    assert not nx.is_digraphical(din, dout)
    din = dout = [1, 1, "rer", 1]
    assert not nx.is_digraphical(din, dout)


def test_multi_sequence():
    # Test nongraphical multi sequence
    seq = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1]
    assert not nx.is_multigraphical(seq)
    # Test small graphical multi sequence
    seq = [6, 5, 4, 4, 2, 1, 1, 1]
    assert nx.is_multigraphical(seq)
    # Test for negative integer in sequence
    seq = [6, 5, 4, -4, 2, 1, 1, 1]
    assert not nx.is_multigraphical(seq)
    # Test for sequence with odd sum
    seq = [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
    assert not nx.is_multigraphical(seq)
    # Test for noninteger
    seq = [1, 1, 1.1, 1]
    assert not nx.is_multigraphical(seq)
    seq = [1, 1, "rer", 1]
    assert not nx.is_multigraphical(seq)


def test_pseudo_sequence():
    # Test small valid pseudo sequence
    seq = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1]
    assert nx.is_pseudographical(seq)
    # Test for sequence with odd sum
    seq = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
    assert not nx.is_pseudographical(seq)
    # Test for negative integer in sequence
    seq = [1000, 3, 3, 3, 3, 2, 2, -2, 1, 1]
    assert not nx.is_pseudographical(seq)
    # Test for noninteger
    seq = [1, 1, 1.1, 1]
    assert not nx.is_pseudographical(seq)
    seq = [1, 1, "rer", 1]
    assert not nx.is_pseudographical(seq)


def test_numpy_degree_sequence():
    np = pytest.importorskip("numpy")
    ds = np.array([1, 2, 2, 2, 1], dtype=np.int64)
    assert nx.is_graphical(ds, "eg")
    assert nx.is_graphical(ds, "hh")
    ds = np.array([1, 2, 2, 2, 1], dtype=np.float64)
    assert nx.is_graphical(ds, "eg")
    assert nx.is_graphical(ds, "hh")
    ds = np.array([1.1, 2, 2, 2, 1], dtype=np.float64)
    pytest.raises(nx.NetworkXException, nx.is_graphical, ds, "eg")
    pytest.raises(nx.NetworkXException, nx.is_graphical, ds, "hh")