コード例 #1
0
ファイル: test_treedset.py プロジェクト: yjfiejd/graphs-dict
class TestDominatingSet2(unittest.TestCase):
    def setUp(self):
        self.N = 7
        self.G = Graph(self.N)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1),
            Edge(1, 3),
            Edge(1, 2),
            Edge(1, 4),
            Edge(2, 5),
            Edge(2, 6)
        ]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #self.G.show()

    def test_borie_dset(self):
        algorithm = BorieDominatingSet(self.G)
        algorithm.run()
        expected1 = set([1, 2])
        self.assertEqual(algorithm.cardinality, len(expected1))
        self.assertEqual(algorithm.dominating_set, expected1)
        # Testing dset.
        neighbors = set(algorithm.dominating_set)
        for node in algorithm.dominating_set:
            neighbors.update(self.G.iteradjacent(node))
        self.assertEqual(len(neighbors), self.G.v())

    def test_tree_dset1(self):
        algorithm = TreeDominatingSet1(self.G)
        algorithm.run()
        expected1 = set([1, 2])
        self.assertEqual(algorithm.cardinality, len(expected1))
        self.assertEqual(algorithm.dominating_set, expected1)
        # Testing dset.
        neighbors = set(algorithm.dominating_set)
        for node in algorithm.dominating_set:
            neighbors.update(self.G.iteradjacent(node))
        self.assertEqual(len(neighbors), self.G.v())

    def test_tree_dset2(self):
        algorithm = TreeDominatingSet2(self.G)
        algorithm.run()
        expected1 = set([1, 2])
        self.assertEqual(algorithm.cardinality, len(expected1))
        self.assertEqual(algorithm.dominating_set, expected1)
        # Testing dset.
        neighbors = set(algorithm.dominating_set)
        for node in algorithm.dominating_set:
            neighbors.update(self.G.iteradjacent(node))
        self.assertEqual(len(neighbors), self.G.v())

    def tearDown(self):
        pass
コード例 #2
0
class TestDominatingSet(unittest.TestCase):
    def setUp(self):
        self.N = 6
        self.G = Graph(self.N)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1),
            Edge(0, 3),
            Edge(1, 3),
            Edge(1, 4),
            Edge(1, 2),
            Edge(2, 5),
            Edge(3, 4),
            Edge(4, 5)
        ]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #self.G.show()

    def test_dominating_set(self):
        algorithm = RandomSequentialDominatingSet(self.G)
        algorithm.run()
        used = set(algorithm.dominating_set)
        for node in algorithm.dominating_set:
            used.update(self.G.iteradjacent(node))
        self.assertEqual(len(used), self.N)
        self.assertEqual(algorithm.cardinality, len(algorithm.dominating_set))
        self.assertTrue(algorithm.cardinality in set([2, 3]))  # best = 2
        #print ( algorithm.dominating_set )

    def test_dominating_set_source(self):
        algorithm = RandomSequentialDominatingSet(self.G)
        algorithm.run(1)
        used = set(algorithm.dominating_set)
        for node in algorithm.dominating_set:
            used.update(self.G.iteradjacent(node))
        self.assertEqual(len(used), self.N)
        self.assertEqual(algorithm.cardinality, len(algorithm.dominating_set))
        self.assertEqual(algorithm.cardinality, 2)  # best = 2
        #print ( algorithm.dominating_set )

    def test_exceptions(self):
        self.assertRaises(ValueError, RandomSequentialDominatingSet,
                          Graph(5, directed=True))

    def tearDown(self):
        pass
コード例 #3
0
class TestGraphLadder(unittest.TestCase):
    def setUp(self):
        self.N = 8  # number of nodes
        self.G = Graph(self.N)
        self.edges = [
            Edge(0, 1, 2),
            Edge(0, 2, 1),
            Edge(2, 3, 5),
            Edge(1, 3, 3),
            Edge(2, 4, 4),
            Edge(3, 5, 6),
            Edge(4, 6, 7),
            Edge(4, 5, 8),
            Edge(5, 7, 9),
            Edge(6, 7, 10)
        ]
        for edge in self.edges:
            self.G.add_edge(edge)

    def test_basic(self):
        self.assertFalse(self.G.is_directed())
        self.assertEqual(self.G.v(), self.N)
        self.assertEqual(self.G.e(), len(self.edges))

    def test_edges(self):
        for edge in self.edges:
            self.assertTrue(self.G.has_edge(edge))
            self.assertEqual(self.G.weight(edge), edge.weight)
        self.assertFalse(self.G.has_edge(Edge(0, 3)))
        self.assertEqual(self.G.weight(Edge(0, 3)), 0)  # no edge

    def test_del(self):
        self.assertEqual(self.G.e(), 10)
        self.G.del_node(7)
        self.assertEqual(self.G.e(), 8)
        self.G.del_node(2)
        self.assertEqual(self.G.e(), 5)

    def test_adjacent(self):
        for node in self.G.iteradjacent(0):
            self.assertTrue(node in [1, 2])
        for node in self.G.iteradjacent(2):
            self.assertTrue(node in [0, 3, 4])

    def tearDown(self):
        pass
コード例 #4
0
ファイル: test_graphs.py プロジェクト: ufkapano/graphs-dict
class TestGraphLadder(unittest.TestCase):

    def setUp(self):
        self.N = 8           # number of nodes
        self.G = Graph(self.N)
        self.edges = [
            Edge(0, 1, 2), Edge(0, 2, 1), Edge(2, 3, 5),
            Edge(1, 3, 3), Edge(2, 4, 4), Edge(3, 5, 6), Edge(4, 6, 7),
            Edge(4, 5, 8), Edge(5, 7, 9), Edge(6, 7, 10)]
        for edge in self.edges:
            self.G.add_edge(edge)

    def test_basic(self):
        self.assertFalse(self.G.is_directed())
        self.assertEqual(self.G.v(), self.N)
        self.assertEqual(self.G.e(), len(self.edges))

    def test_edges(self):
        for edge in self.edges:
            self.assertTrue(self.G.has_edge(edge))
            self.assertEqual(self.G.weight(edge), edge.weight)
        self.assertFalse(self.G.has_edge(Edge(0, 3)))
        self.assertEqual(self.G.weight(Edge(0, 3)), 0)  # no edge

    def test_del(self):
        self.assertEqual(self.G.e(), 10)
        self.G.del_node(7)
        self.assertEqual(self.G.e(), 8)
        self.G.del_node(2)
        self.assertEqual(self.G.e(), 5)

    def test_adjacent(self):
        for node in self.G.iteradjacent(0):
            self.assertTrue(node in [1, 2])
        for node in self.G.iteradjacent(2):
            self.assertTrue(node in [0, 3, 4])

    def tearDown(self): pass
コード例 #5
0
def make_random_ktree(n, k):  # using list
    """Make a random k-tree with n vertices."""
    if k >= n:
        raise ValueError("bad k")  # run time error possible
    graph = Graph(n)
    if n < 1:
        raise ValueError("bad n")
    elif n == 1:
        graph.add_node(0)
    else:
        for node in xrange(n):
            graph.add_node(node)
        # Make {n-k-1, ..., n-1} into (k+1)-clique in graph.
        for source in xrange(n - k - 1, n):
            for target in xrange(n - k - 1, n):
                if source < target:
                    graph.add_edge(Edge(source, target))
        node = n - k - 2
        while node >= 0:
            # Wybor source z przedzialu od node+1 do n-k-1.
            # To jest jakby wybor duzej (k+1)-kliki,
            source = random.choice(xrange(node + 1, n - k))
            # Teraz zbieram wierzcholki tej kliki, ale one maja byc
            # wieksze od source, wieksze numery!
            neighbors = list(target for target in graph.iteradjacent(source)
                             if source < target)
            neighbors.append(source)  # closed neighborhood
            # Z duzej (k+1)-kliki wybieram mala k-klike.
            idx = random.randrange(0, len(neighbors))
            swap(neighbors, idx, -1)
            neighbors.pop()
            # Connect node to all nodes from neighbors.
            for target in neighbors:
                graph.add_edge(Edge(node, target))
            node -= 1
    return graph
コード例 #6
0
ファイル: sptools.py プロジェクト: ufkapano/graphs-dict
def make_random_ktree(n, k):   # using list
    """Make a random k-tree with n vertices."""
    if k >= n:
        raise ValueError("bad k")   # run time error possible
    graph = Graph(n)
    if n < 1:
        raise ValueError("bad n")
    elif n == 1:
        graph.add_node(0)
    else:
        for node in xrange(n):
            graph.add_node(node)
        # Make {n-k-1, ..., n-1} into (k+1)-clique in graph.
        for source in xrange(n-k-1, n):
            for target in xrange(n-k-1, n):
                if source < target:
                    graph.add_edge(Edge(source, target))
        node = n-k-2
        while node >= 0:
            # Wybor source z przedzialu od node+1 do n-k-1.
            # To jest jakby wybor duzej (k+1)-kliki,
            source = random.choice(xrange(node+1, n-k))
            # Teraz zbieram wierzcholki tej kliki, ale one maja byc
            # wieksze od source, wieksze numery!
            neighbors = list(target for target in graph.iteradjacent(source)
                            if source < target)
            neighbors.append(source)   # closed neighborhood
            # Z duzej (k+1)-kliki wybieram mala k-klike.
            idx = random.randrange(0, len(neighbors))
            swap(neighbors, idx, -1)
            neighbors.pop()
            # Connect node to all nodes from neighbors.
            for target in neighbors:
                graph.add_edge(Edge(node, target))
            node -= 1
    return graph