|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | + |
| 13 | +import unittest |
| 14 | + |
| 15 | +import rustworkx |
| 16 | + |
| 17 | + |
| 18 | +class TestLocalComplement(unittest.TestCase): |
| 19 | + def test_multigraph(self): |
| 20 | + graph = rustworkx.PyGraph(multigraph=True) |
| 21 | + node = graph.add_node("") |
| 22 | + with self.assertRaises(ValueError): |
| 23 | + rustworkx.local_complement(graph, node) |
| 24 | + |
| 25 | + def test_invalid_node(self): |
| 26 | + graph = rustworkx.PyGraph(multigraph=False) |
| 27 | + node = graph.add_node("") |
| 28 | + with self.assertRaises(rustworkx.InvalidNode): |
| 29 | + rustworkx.local_complement(graph, node + 1) |
| 30 | + |
| 31 | + def test_clique(self): |
| 32 | + N = 5 |
| 33 | + graph = rustworkx.generators.complete_graph(N, multigraph=False) |
| 34 | + |
| 35 | + for node in range(0, N): |
| 36 | + expected_graph = rustworkx.PyGraph(multigraph=False) |
| 37 | + expected_graph.extend_from_edge_list([(i, node) for i in range(0, N) if i != node]) |
| 38 | + |
| 39 | + complement_graph = rustworkx.local_complement(graph, node) |
| 40 | + |
| 41 | + self.assertTrue( |
| 42 | + rustworkx.is_isomorphic( |
| 43 | + expected_graph, |
| 44 | + complement_graph, |
| 45 | + ) |
| 46 | + ) |
| 47 | + |
| 48 | + def test_empty(self): |
| 49 | + N = 5 |
| 50 | + graph = rustworkx.generators.empty_graph(N, multigraph=False) |
| 51 | + |
| 52 | + expected_graph = rustworkx.generators.empty_graph(N, multigraph=False) |
| 53 | + |
| 54 | + complement_graph = rustworkx.local_complement(graph, 0) |
| 55 | + self.assertTrue( |
| 56 | + rustworkx.is_isomorphic( |
| 57 | + expected_graph, |
| 58 | + complement_graph, |
| 59 | + ) |
| 60 | + ) |
| 61 | + |
| 62 | + def test_local_complement(self): |
| 63 | + # Example took from https://arxiv.org/abs/1910.03969, figure 1 |
| 64 | + graph = rustworkx.PyGraph(multigraph=False) |
| 65 | + graph.extend_from_edge_list( |
| 66 | + [(0, 1), (0, 3), (0, 5), (1, 2), (2, 3), (2, 4), (3, 4), (3, 5)] |
| 67 | + ) |
| 68 | + |
| 69 | + expected_graph = rustworkx.PyGraph(multigraph=False) |
| 70 | + expected_graph.extend_from_edge_list( |
| 71 | + [(0, 1), (0, 3), (0, 5), (1, 2), (1, 3), (1, 5), (2, 3), (2, 4), (3, 4)] |
| 72 | + ) |
| 73 | + |
| 74 | + complement_graph = rustworkx.local_complement(graph, 0) |
| 75 | + self.assertTrue( |
| 76 | + rustworkx.is_isomorphic( |
| 77 | + expected_graph, |
| 78 | + complement_graph, |
| 79 | + ) |
| 80 | + ) |
0 commit comments