示例#1
0
    def setUp(self):
        self.petersen_graph = petersen_graph()
        self.random_graph10_5 = random_graph(10, 0.5)
        self.random_graph100_3 = random_graph(100, 0.3)

        self.petersen_cliques = [
            [0, 1], [0, 4], [0, 5], [1, 2], [1, 6],
            [2, 3], [2, 7], [3, 4], [3, 8], [4, 9],
            [5, 7], [5, 8], [6, 8], [6, 9], [7, 9],
        ]

        self.random_graph_edges = [
            (0, 2), (0, 3), (0, 5), (0, 7),
            (0, 9), (1, 2), (1, 3), (1, 5),
            (1, 6), (1, 7), (1, 9), (2, 3),
            (2, 5), (2, 6), (2, 7), (2, 8),
            (3, 5), (3, 7), (4, 6), (4, 7),
            (5, 6), (5, 7), (5, 9), (6, 7),
            (6, 8), (6, 9), (7, 9),
        ]

        self.random_graph = Graph()
        self.random_graph.add_edges(self.random_graph_edges)
        self.random_graph_clique = [0, 2, 3, 5, 7]

        self.coprime_graph10 = coprime_pairs_graph(10)
        self.coprime_graph10_weight = 30
        self.coprime_graph10_clique = [1, 5, 7, 8, 9]

        self.coprime_graph100 = coprime_pairs_graph(100)
        self.coprime_graph100_weight = 1356
        self.coprime_graph100_clique = [
            1, 17, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,
            67, 71, 73, 79, 81, 83, 88, 89, 91, 95, 97,
        ]
示例#2
0
    def test_korman(self):
        (num_colors, classes) = korman(self.petersen_graph)
        self.assertEqual(num_colors, self.petersen_colors)
        self.assertTrue(is_valid_color_classes(self.petersen_graph, classes))

        (num_colors, classes) = korman(self.small_random)
        self.assertEqual(num_colors, self.small_colors)
        self.assertEqual(classes, self.small_classes)

        (num_colors, classes) = korman(self.small_random2)
        self.assertEqual(num_colors, self.small_colors2)
        self.assertEqual(classes, self.small_classes2)

        (num_colors, classes) = korman(self.big_random)
        self.assertEqual(num_colors, self.big_colors)
        self.assertEqual(classes, self.big_classes)

        (num_colors, classes) = korman(self.coprime_graph30)
        self.assertEqual(num_colors, self.coprime_graph30_colors)
        self.assertTrue(is_valid_color_classes(self.coprime_graph30, classes))

        (num_colors, color_map) = korman(self.coprime_graph30, classes=False)
        self.assertEqual(color_map, {1: 7, 2: 1, 3: 2, 4: 1, 5: 3, 6: 1, 7: 4, 8: 1, 9: 2, 10: 1, 11: 5, 12: 1, 13: 6,
                                     14: 1, 15: 2, 16: 1, 17: 8, 18: 1, 19: 9, 20: 1, 21: 2, 22: 1, 23: 10, 24: 1, 25:
                                     3, 26: 1, 27: 2, 28: 1, 29: 11, 30: 1})

        for i in xrange(10):
            graph = random_graph(20, 0.5)
            k_num, classes = korman(graph)
            b_num, classes = branch_and_bound(graph)
            self.assertEqual(b_num, k_num)
            self.assertTrue(is_valid_color_classes(graph, classes))
示例#3
0
    def test_maxis(self):
        print "Testing MAXIS coloring algorithm"

        (num_colors, color_map) = maxis(self.petersen_graph, classes=False)
        self.assertEqual(num_colors, self.petersen_colors)
        self.assertEqual(color_map, {0: 0, 1: 1, 2: 2, 3: 1, 4: 2, 5: 1, 6: 2, 7: 0, 8: 0, 9: 1})

        graph = random_graph(50, 0.5)
        (num_colors, classes) = maxis(graph, color_limit=30)
        self.assertTrue(is_valid_color_classes(graph, classes))

        graph = random_graph(60, 0.5)
        (num_colors, classes) = maxis(graph, use_greedy=False, color_limit=30, mis_limit=30, verbose=True)
        self.assertTrue(is_valid_color_classes(graph, classes))

        graph = random_graph(50, 0.5)
        (num_colors, classes) = maxis(graph, mis_limit=50, color_limit=30)
        self.assertTrue(is_valid_color_classes(graph, classes))

        graph = random_graph(50, 0.5)
        (num_colors, classes) = maxis(graph, use_greedy=True, color_limit=30, mis_limit=40, verbose=True)
        self.assertTrue(is_valid_color_classes(graph, classes))

        graph = random_graph(50, 0.5)
        (num_colors, classes) = maxis(graph, use_greedy=True, color_limit=0, mis_limit=40, verbose=True)
        self.assertTrue(is_valid_color_classes(graph, classes))

        graph = random_graph(50, 0.5)
        (num_colors, classes) = maxis(graph, use_greedy=True, color_limit=30)
        self.assertTrue(is_valid_color_classes(graph, classes))

        for i in xrange(10):
            graph = random_graph(10, 0.8)
            _, classes = maxis(graph, verbose=True)
            self.assertTrue(is_valid_color_classes(graph, classes))
示例#4
0
    def test_branch_and_bound(self):
        print "Testing branch and bound coloring algorithm"
        (num_colors, classes) = branch_and_bound(self.petersen_graph)
        self.assertEqual(num_colors, self.petersen_colors)
        self.assertTrue(is_valid_color_classes(self.petersen_graph, classes))

        (num_colors, classes) = branch_and_bound(self.coprime_graph30)
        self.assertEqual(num_colors, self.coprime_graph30_colors)
        self.assertTrue(is_valid_color_classes(self.coprime_graph30, classes))

        (num_colors, color_map) = branch_and_bound(self.coprime_graph30, classes=False)
        self.assertEqual(color_map, {1: 5, 2: 11, 3: 10, 4: 11, 5: 9, 6: 10, 7: 8, 8: 11, 9: 10, 10: 9, 11: 7, 12: 10,
                                     13: 6, 14: 8, 15: 9, 16: 11, 17: 4, 18: 10, 19: 3, 20: 9, 21: 8, 22: 7, 23: 2, 24:
                                     10, 25: 9, 26: 6, 27: 10,
                                     28: 8, 29: 1, 30: 9})

        (num_colors, classes) = branch_and_bound(self.small_random)
        self.assertEqual(num_colors, self.small_colors)
        self.assertEqual(classes, self.small_classes)

        (num_colors, classes) = branch_and_bound(self.small_random2)
        self.assertEqual(num_colors, self.small_colors2)
        self.assertEqual(classes, self.small_classes2)

        (num_colors, classes) = branch_and_bound(self.big_random)
        self.assertEqual(num_colors, self.big_colors)
        self.assertEqual(classes, self.big_classes)

        graph = random_graph(30, 0.5)
        (num_colors, classes) = branch_and_bound(graph)
        self.assertTrue(is_valid_color_classes(graph, classes))

        for i in xrange(10):
            graph = random_graph(10, 0.8)
            _, classes = branch_and_bound(graph)
            self.assertTrue(is_valid_color_classes(graph, classes))
示例#5
0
    def test_dsatur(self):
        print "Testing DSATUR coloring algorithm"
        (num_colors, classes) = dsatur(self.petersen_graph)
        self.assertTrue(num_colors <= self.petersen_colors)
        self.assertTrue(is_valid_color_classes(self.petersen_graph, classes))

        (num_colors, color_map) = dsatur(self.petersen_graph, classes=False)
        self.assertEqual(num_colors, self.petersen_colors)
        self.assertEqual(color_map, {0: 0, 1: 1, 2: 2, 3: 1, 4: 2, 5: 1, 6: 2, 7: 0, 8: 0, 9: 1})

        (num_colors, classes) = dsatur(self.coprime_graph30)
        self.assertTrue(num_colors <= self.coprime_graph30_colors)
        self.assertTrue(is_valid_color_classes(self.coprime_graph30, classes))

        for i in xrange(10):
            graph = random_graph(10, 0.8)
            _, classes = dsatur(graph)
            self.assertTrue(is_valid_color_classes(graph, classes))
示例#6
0
    def test_greedy_sequential(self):
        print "Testing greedy sequential coloring algorithm"
        (num_colors, classes) = greedy_sequential(self.petersen_graph)
        self.assertTrue(num_colors == self.petersen_colors)
        self.assertTrue(is_valid_color_classes(self.petersen_graph, classes))

        (num_colors, classes) = greedy_sequential(self.petersen_graph, classes=False)
        self.assertTrue(num_colors == self.petersen_colors)
        self.assertTrue(classes, {0: 0, 1: 2, 2: 0, 3: 2, 4: 1, 5: 2, 6: 1, 7: 1, 8: 0, 9: 0})

        (num_colors, classes) = greedy_sequential(self.coprime_graph30, passes=2)
        self.assertTrue(num_colors <= self.coprime_graph30_colors)
        self.assertTrue(is_valid_color_classes(self.coprime_graph30, classes))

        for i in xrange(10):
            graph = random_graph(10, 0.8)
            _, classes = greedy_sequential(graph)
            self.assertTrue(is_valid_color_classes(graph, classes))
示例#7
0
    def test_vertices(self):
        graph = random_graph(10, 0.5)

        self.assertTrue(graph.vertices() == range(10))
        self.assertTrue(graph.vertex_set() == set(range(10)))

        self.assertTrue(graph.num_vertices() == 10)
        self.assertTrue(len(graph.graph_dict.keys()) == 10)

        new_graph = graph.remove_vertex(0)
        self.assertTrue(new_graph.num_vertices() == 9)
        self.assertTrue(len(new_graph.graph_dict.keys()) == 9)

        self.assertTrue(graph.num_vertices() == 10)
        self.assertTrue(len(graph.graph_dict.keys()) == 10)

        new_graph = graph.remove_vertices([0, 1, 2])
        self.assertTrue(new_graph.num_vertices() == 7)
        self.assertTrue(len(new_graph.graph_dict.keys()) == 7)

        graph.delete_vertex(0)
        self.assertTrue(graph.num_vertices() == 9)
        self.assertTrue(len(graph.graph_dict.keys()) == 9)
示例#8
0
    def setUp(self):
        self.petersen_graph = petersen_graph()
        self.random_graph10_5 = random_graph(10, 0.5)
        self.random_graph100_3 = random_graph(100, 0.3)

        self.petersen_cliques = [
            [0, 1],
            [0, 4],
            [0, 5],
            [1, 2],
            [1, 6],
            [2, 3],
            [2, 7],
            [3, 4],
            [3, 8],
            [4, 9],
            [5, 7],
            [5, 8],
            [6, 8],
            [6, 9],
            [7, 9],
        ]

        self.random_graph_edges = [
            (0, 2),
            (0, 3),
            (0, 5),
            (0, 7),
            (0, 9),
            (1, 2),
            (1, 3),
            (1, 5),
            (1, 6),
            (1, 7),
            (1, 9),
            (2, 3),
            (2, 5),
            (2, 6),
            (2, 7),
            (2, 8),
            (3, 5),
            (3, 7),
            (4, 6),
            (4, 7),
            (5, 6),
            (5, 7),
            (5, 9),
            (6, 7),
            (6, 8),
            (6, 9),
            (7, 9),
        ]

        self.random_graph = Graph()
        self.random_graph.add_edges(self.random_graph_edges)
        self.random_graph_clique = [0, 2, 3, 5, 7]
        self.random_graph_clique2 = [1, 5, 6, 7, 9]

        self.coprime_graph10 = coprime_pairs_graph(10)
        self.coprime_graph10_weight = 30
        self.coprime_graph10_clique = [1, 5, 7, 8, 9]

        self.coprime_graph100 = coprime_pairs_graph(100)
        self.coprime_graph100_weight = 1356
        self.coprime_graph100_clique = [
            1,
            17,
            23,
            29,
            31,
            37,
            41,
            43,
            47,
            53,
            59,
            61,
            67,
            71,
            73,
            79,
            81,
            83,
            88,
            89,
            91,
            95,
            97,
        ]