File: test_cycles.py

package info (click to toggle)
python-igraph 0.11.8%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,480 kB
  • sloc: ansic: 24,545; python: 21,699; sh: 107; makefile: 35; sed: 2
file content (187 lines) | stat: -rw-r--r-- 5,311 bytes parent folder | download | duplicates (2)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import random
import unittest

from igraph import Graph


class CycleTests(unittest.TestCase):
    def setUp(self):
        random.seed(42)

    def test_is_acyclic(self):
        g = Graph.Tree(121, 3, mode="out")
        self.assertTrue(g.is_acyclic())

        g = Graph()
        self.assertTrue(g.is_acyclic())

        g = Graph.Ring(5)
        self.assertFalse(g.is_acyclic())

    def test_is_dag(self):
        g = Graph(5, [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)], directed=True)
        self.assertTrue(g.is_dag())
        g.to_undirected()
        self.assertFalse(g.is_dag())
        g = Graph.Barabasi(1000, 2, directed=True)
        self.assertTrue(g.is_dag())
        g = Graph.GRG(100, 0.2)
        self.assertFalse(g.is_dag())
        g = Graph.Ring(10, directed=True, mutual=False)
        self.assertFalse(g.is_dag())

    def test_fundamental_cycles(self):
        g = Graph(
            [
                (1, 2),
                (2, 3),
                (3, 1),
                (4, 5),
                (5, 4),
                (4, 5),
                (6, 7),
                (7, 8),
                (8, 9),
                (9, 6),
                (6, 8),
                (10, 10),
                (10, 11),
                (12, 12),
            ]
        )
        cycles = [sorted(cycle) for cycle in g.fundamental_cycles()]
        assert cycles == [[0, 1, 2], [4, 5], [3, 5], [6, 7, 10], [8, 9, 10], [11], [13]]

        cycles = [sorted(cycle) for cycle in g.fundamental_cycles(start_vid=6)]
        assert cycles == [[6, 7, 10], [8, 9, 10]]

        cycles = [
            sorted(cycle) for cycle in g.fundamental_cycles(start_vid=6, cutoff=1)
        ]
        assert cycles == [[6, 7, 10], [8, 9, 10]]

    def test_minimum_cycle_basis(self):
        g = Graph(
            [
                (1, 2),
                (2, 3),
                (3, 1),
                (4, 5),
                (5, 4),
                (4, 5),
                (6, 7),
                (7, 8),
                (8, 9),
                (9, 6),
                (6, 8),
                (10, 10),
                (10, 11),
                (12, 12),
            ]
        )
        cycles = g.minimum_cycle_basis()
        assert cycles == [
            (11,),
            (13,),
            (3, 5),
            (4, 5),
            (0, 1, 2),
            (6, 7, 10),
            (8, 9, 10),
        ]

        g = Graph.Lattice((5, 6), circular=True)
        cycles = g.minimum_cycle_basis()
        expected = [
            (0, 1, 10, 3),
            (0, 51, 50, 53),
            (1, 8, 9, 18),
            (2, 3, 12, 5),
            (2, 53, 52, 55),
            (4, 5, 14, 7),
            (4, 55, 54, 57),
            (6, 7, 16, 9),
            (6, 57, 56, 59),
            (8, 59, 58, 51),
            (10, 11, 20, 13),
            (11, 18, 19, 28),
            (12, 13, 22, 15),
            (14, 15, 24, 17),
            (16, 17, 26, 19),
            (20, 21, 30, 23),
            (21, 28, 29, 38),
            (22, 23, 32, 25),
            (24, 25, 34, 27),
            (26, 27, 36, 29),
            (30, 31, 40, 33),
            (31, 38, 39, 48),
            (32, 33, 42, 35),
            (34, 35, 44, 37),
            (36, 37, 46, 39),
            (40, 41, 50, 43),
            (41, 48, 49, 58),
            (42, 43, 52, 45),
            (44, 45, 54, 47),
            (0, 8, 6, 4, 2),
            (1, 51, 41, 31, 21, 11),
        ]
        assert len(cycles) == len(expected)
        for expected_cycle, observed_cycle in zip(expected, cycles):
            assert expected_cycle == observed_cycle or expected_cycle == (
                observed_cycle[0],
            ) + tuple(reversed(observed_cycle[1:]))

        g = Graph.Lattice((5, 6), circular=True)
        cycles = g.minimum_cycle_basis(cutoff=2, complete=False)
        expected = [
            (0, 1, 10, 3),
            (0, 51, 50, 53),
            (1, 8, 9, 18),
            (2, 3, 12, 5),
            (2, 53, 52, 55),
            (4, 5, 14, 7),
            (4, 55, 54, 57),
            (6, 7, 16, 9),
            (6, 57, 56, 59),
            (8, 59, 58, 51),
            (10, 11, 20, 13),
            (11, 18, 19, 28),
            (12, 13, 22, 15),
            (14, 15, 24, 17),
            (16, 17, 26, 19),
            (20, 21, 30, 23),
            (21, 28, 29, 38),
            (22, 23, 32, 25),
            (24, 25, 34, 27),
            (26, 27, 36, 29),
            (30, 31, 40, 33),
            (31, 38, 39, 48),
            (32, 33, 42, 35),
            (34, 35, 44, 37),
            (36, 37, 46, 39),
            (40, 41, 50, 43),
            (41, 48, 49, 58),
            (42, 43, 52, 45),
            (44, 45, 54, 47),
            (0, 8, 6, 4, 2),
            (1, 51, 41, 31, 21, 11),
        ]
        assert len(cycles) == len(expected) - 1
        for expected_cycle, observed_cycle in zip(expected[:-1], cycles):
            assert expected_cycle == observed_cycle or expected_cycle == (
                observed_cycle[0],
            ) + tuple(reversed(observed_cycle[1:]))


def suite():
    cycle_suite = unittest.defaultTestLoader.loadTestsFromTestCase(CycleTests)
    return unittest.TestSuite([cycle_suite])


def test():
    runner = unittest.TextTestRunner()
    runner.run(suite())


if __name__ == "__main__":
    test()