summaryrefslogtreecommitdiff
path: root/networkx/algorithms/approximation/tests/test_dominating_set.py
blob: 6b90d85ecf73bb56370fd92fdec25e3bbbb91ce3 (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
import pytest

import networkx as nx
from networkx.algorithms.approximation import (
    min_edge_dominating_set,
    min_weighted_dominating_set,
)


class TestMinWeightDominatingSet:
    def test_min_weighted_dominating_set(self):
        graph = nx.Graph()
        graph.add_edge(1, 2)
        graph.add_edge(1, 5)
        graph.add_edge(2, 3)
        graph.add_edge(2, 5)
        graph.add_edge(3, 4)
        graph.add_edge(3, 6)
        graph.add_edge(5, 6)

        vertices = {1, 2, 3, 4, 5, 6}
        # due to ties, this might be hard to test tight bounds
        dom_set = min_weighted_dominating_set(graph)
        for vertex in vertices - dom_set:
            neighbors = set(graph.neighbors(vertex))
            assert len(neighbors & dom_set) > 0, "Non dominating set found!"

    def test_star_graph(self):
        """Tests that an approximate dominating set for the star graph,
        even when the center node does not have the smallest integer
        label, gives just the center node.

        For more information, see #1527.

        """
        # Create a star graph in which the center node has the highest
        # label instead of the lowest.
        G = nx.star_graph(10)
        G = nx.relabel_nodes(G, {0: 9, 9: 0})
        assert min_weighted_dominating_set(G) == {9}

    def test_null_graph(self):
        """Tests that the unique dominating set for the null graph is an empty set"""
        G = nx.Graph()
        assert min_weighted_dominating_set(G) == set()

    def test_min_edge_dominating_set(self):
        graph = nx.path_graph(5)
        dom_set = min_edge_dominating_set(graph)

        # this is a crappy way to test, but good enough for now.
        for edge in graph.edges():
            if edge in dom_set:
                continue
            else:
                u, v = edge
                found = False
                for dom_edge in dom_set:
                    found |= u == dom_edge[0] or u == dom_edge[1]
                assert found, "Non adjacent edge found!"

        graph = nx.complete_graph(10)
        dom_set = min_edge_dominating_set(graph)

        # this is a crappy way to test, but good enough for now.
        for edge in graph.edges():
            if edge in dom_set:
                continue
            else:
                u, v = edge
                found = False
                for dom_edge in dom_set:
                    found |= u == dom_edge[0] or u == dom_edge[1]
                assert found, "Non adjacent edge found!"

        graph = nx.Graph()  # empty Networkx graph
        with pytest.raises(ValueError, match="Expected non-empty NetworkX graph!"):
            min_edge_dominating_set(graph)