コード例 #1
0
 def test_remove_edge_duplicate_correct(self):
     graph = Graph([(1, 2, 1), (1, 2, 1), (1, 2, 2), (2, 3, 1), (3, 1, 1)])
     graph.remove_edge(1, 2, 1)
     expected = [
         Edge(*args)
         for args in [(1, 2, 1), (1, 2, 2), (2, 3, 1), (3, 1, 1)]
     ]
     self.assertEqual(expected, graph.all_edges)
コード例 #2
0
 def test_remove_edge_correct(self):
     graph = Graph(self.edges)
     graph.remove_edge(1, 4, 4)
     expected = [
         Edge(*args)
         for args in [(1, 2, 4), (2, 4, 1), (2, 3, 4), (3, 4, 4)]
     ]
     self.assertEqual(expected, graph.all_edges)
コード例 #3
0
 def test_add_new_edges_simple_true(self):
     graph = Graph([(1, 2, 4), (1, 3, 4), (2, 3, 1), (2, 4, 4), (3, 4, 4)])
     min_route = [[2, 3]]
     new_graph = eularian.add_new_edges(graph, min_route)
     expected = [(1, 2, 4), (1, 3, 4), (2, 3, 1), (2, 4, 4), (3, 4, 4),
                 (2, 3, 1)]
     self.assertEqual(expected, new_graph.all_edges)
コード例 #4
0
 def test_make_eularian_simple_true(self):
     """ Test an obvious eularian circuit modification. """
     graph = Graph([(1, 2, 4), (1, 3, 4), (2, 3, 1), (2, 4, 4), (3, 4, 4)])
     new_graph, cost = eularian.make_eularian(graph)
     expected = [(1, 2, 4), (1, 3, 4), (2, 3, 1), (2, 4, 4), (3, 4, 4),
                 (2, 3, 1)]
     self.assertEqual((new_graph.all_edges, cost), (expected, 1))
コード例 #5
0
 def test_add_new_edges_simple_true(self):
     # A belted diamond, with no dead-ends
     graph = Graph([(1, 2, 4), (1, 3, 4), (2, 3, 1), (2, 4, 4), (3, 4, 4)])
     min_route = [[2, 3]]
     new_graph = eularian.add_new_edges(graph, min_route)
     expected = [
         Edge(*args)
         for args in [(1, 2, 4), (1, 3, 4), (2, 3, 1), (2, 4,
                                                        4), (3, 4,
                                                             4), (2, 3, 1)]
     ]
     self.assertCountEqual(expected, new_graph.all_edges)
コード例 #6
0
 def test_make_eularian_simple_true(self):
     """ Test an obvious eularian circuit modification. """
     # A belted diamond, with no dead-ends
     graph = Graph([(1, 2, 4), (1, 3, 4), (2, 3, 1), (2, 4, 4), (3, 4, 4)])
     new_graph, dead_ends = eularian.make_eularian(graph)
     expected = [
         Edge(*args)
         for args in [(1, 2, 4), (1, 3, 4), (2, 3, 1), (2, 4,
                                                        4), (3, 4,
                                                             4), (2, 3, 1)]
     ]
     self.assertCountEqual(new_graph.all_edges, expected)
     self.assertEqual(dead_ends, 0)
コード例 #7
0
 def test_find_dead_ends_double_correct(self):
     """ Find dead ends in a graph. """
     # Two adjacent triandles with tails on peak and belt
     graph = Graph([
         (1, 2, 1),
         (1, 4, 1),
         (1, 6, 1),
         (2, 3, 1),
         (2, 4, 1),
         (4, 5, 1),
         (4, 6, 1),
     ])
     self.assertCountEqual([Edge(2, 3, 1), Edge(4, 5, 1)],
                           eularian.find_dead_ends(graph))
コード例 #8
0
 def test_is_semi_eularian_true(self):
     # Diamond w/ one crossing edge: semi-Eularian
     graph = Graph([(1, 2, 1), (2, 3, 1), (3, 4, 1), (4, 1, 1), (2, 4, 2)])
     self.assertTrue(graph.is_semi_eularian)
コード例 #9
0
 def test_is_semi_eularian_false_eularian(self):
     # A simple Eularian diamond
     graph = Graph([(1, 2, 1), (2, 3, 1), (3, 4, 1), (4, 1, 1)])
     self.assertFalse(graph.is_semi_eularian)
コード例 #10
0
 def test_is_eularian_true(self):
     # A simple Eularian diamond
     graph = Graph([(1, 2, 1), (2, 3, 1), (3, 4, 1), (4, 1, 1)])
     self.assertTrue(graph.is_eularian)
コード例 #11
0
 def setUp(self):
     """ Set up a default graph. """
     # Two very wide, flat triangles, Semi-Eularian
     self.edges = [(1, 2, 4), (1, 4, 4), (2, 4, 1), (2, 3, 4), (3, 4, 4)]
     self.graph = Graph(self.edges)
コード例 #12
0
class TestGraph(unittest.TestCase):
    def setUp(self):
        """ Set up a default graph. """
        # Two very wide, flat triangles, Semi-Eularian
        self.edges = [(1, 2, 4), (1, 4, 4), (2, 4, 1), (2, 3, 4), (3, 4, 4)]
        self.graph = Graph(self.edges)

    def test_node_keys(self):
        self.assertEqual([1, 2, 3, 4], self.graph.node_keys)

    def test_odd_nodes(self):
        self.assertEqual([2, 4], self.graph.odd_nodes)

    def test_all_edges_correct(self):
        self.assertEqual(self.edges, self.graph.all_edges)

    def test_edge_cost_correct(self):
        self.assertEqual(4, self.graph.edge_cost(1, 4))

    def test_edge_cost_correct(self):
        self.assertNotEqual(1, self.graph.edge_cost(1, 2))

    def test_edge_cost_reversed(self):
        self.assertEqual(4, self.graph.edge_cost(4, 1))

    def test_total_cost_correct(self):
        self.assertEqual(17, self.graph.total_cost)

    def test_add_edge_correct_easy(self):
        graph = Graph([(1, 2, 1)])  # One edge, two nodes
        graph.add_edge(2, 3, 2)  # Add an edge from 2 to 3 w/ cost 2
        self.assertEqual([(1, 2, 1), (2, 3, 2)], graph.all_edges)

    def test_add_edge_correct_duplicate(self):
        graph = Graph([(1, 2, 1)])  # One edge, two nodes
        graph.add_edge(1, 2, 1)  # Add a parallel edge
        self.assertEqual([(1, 2, 1), (1, 2, 1)], graph.all_edges)

    def test_remove_edge_correct(self):
        graph = Graph(self.edges)
        graph.remove_edge(1, 4, 4)
        expected = [(1, 2, 4), (2, 4, 1), (2, 3, 4), (3, 4, 4)]
        self.assertEqual(expected, graph.all_edges)

    def test_remove_edges_correct(self):
        graph = Graph(self.edges)
        graph.remove_edges([Edge(1, 4, 4), Edge(2, 3, 4)])
        expected = [(1, 2, 4), (2, 4, 1), (3, 4, 4)]
        self.assertEqual(expected, graph.all_edges)

    def test_remove_edge_duplicate_correct(self):
        graph = Graph([(1, 2, 1), (1, 2, 1), (1, 2, 2), (2, 3, 1), (3, 1, 1)])
        graph.remove_edge(1, 2, 1)
        expected = [(1, 2, 1), (1, 2, 2), (2, 3, 1), (3, 1, 1)]
        self.assertEqual(expected, graph.all_edges)

    def test_edge_options(self):
        expected = [(1, 2, 4), (1, 4, 4)]
        self.assertEqual(expected, self.graph.edge_options(1))

    def test_is_eularian_true(self):
        # A simple Eularian diamond
        graph = Graph([(1, 2, 1), (2, 3, 1), (3, 4, 1), (4, 1, 1)])
        self.assertTrue(graph.is_eularian)

    def test_is_eularian_false_semi_eularian(self):
        # Diamond w/ one crossing edge: semi-Eularian
        graph = Graph([(1, 2, 1), (2, 3, 1), (3, 4, 1), (4, 1, 1), (2, 4, 2)])
        self.assertFalse(graph.is_eularian)

    def test_is_eularian_false_non_eularian(self):
        # Diamond w/ two crossing edges: non-Eularian
        graph = Graph([(1, 2, 1), (2, 3, 1), (3, 4, 1), (4, 1, 1), (2, 4, 2),
                       (1, 3, 2)])
        self.assertFalse(graph.is_eularian)

    def test_is_semi_eularian_false_eularian(self):
        # A simple Eularian diamond
        graph = Graph([(1, 2, 1), (2, 3, 1), (3, 4, 1), (4, 1, 1)])
        self.assertFalse(graph.is_semi_eularian)

    def test_is_semi_eularian_true(self):
        # Diamond w/ one crossing edge: semi-Eularian
        graph = Graph([(1, 2, 1), (2, 3, 1), (3, 4, 1), (4, 1, 1), (2, 4, 2)])
        self.assertTrue(graph.is_semi_eularian)

    def test_is_semi_eularian_false_non_eularian(self):
        # Diamond w/ two crossing edges: non-Eularian
        graph = Graph([(1, 2, 1), (2, 3, 1), (3, 4, 1), (4, 1, 1), (2, 4, 2),
                       (1, 3, 2)])
        self.assertFalse(graph.is_semi_eularian)

    def test_is_bridge_true(self):
        #  Two triangles joined by 'CD'
        graph = Graph([(1, 2, 1), (1, 3, 1), (2, 3, 1), (3, 4, 1), (4, 5, 1),
                       (4, 6, 1), (5, 6, 1)])
        self.assertTrue(graph.is_bridge((3, 4, 1)))

    def test_is_bridge_false(self):
        #  Two triangles joined by 'CD'
        graph = Graph([(1, 2, 1), (1, 3, 1), (2, 3, 1), (3, 4, 1), (4, 5, 1),
                       (4, 6, 1), (5, 6, 1)])
        self.assertFalse(graph.is_bridge((2, 3, 1)))
コード例 #13
0
 def test_remove_edges_correct(self):
     graph = Graph(self.edges)
     graph.remove_edges([Edge(1, 4, 4), Edge(2, 3, 4)])
     expected = [(1, 2, 4), (2, 4, 1), (3, 4, 4)]
     self.assertEqual(expected, graph.all_edges)
コード例 #14
0
 def test_add_edge_correct_duplicate(self):
     graph = Graph([(1, 2, 1)])  # One edge, two nodes
     graph.add_edge(1, 2, 1)  # Add a parallel edge
     self.assertEqual([(1, 2, 1), (1, 2, 1)], graph.all_edges)
コード例 #15
0
 def test_is_bridge_true(self):
     #  Two triangles 1-2-3 and 4-5-6 joined by '3-4' bridge
     graph = Graph([(1, 2, 1), (1, 3, 1), (2, 3, 1), (3, 4, 1), (4, 5, 1),
                    (4, 6, 1), (5, 6, 1)])
     self.assertTrue(graph.is_bridge(3))  # Edge 3 aka '3-4 'is a bridge
コード例 #16
0
 def test_is_semi_eularian_false_non_eularian(self):
     # Diamond w/ two crossing edges: non-Eularian
     graph = Graph([(1, 2, 1), (2, 3, 1), (3, 4, 1), (4, 1, 1), (2, 4, 2),
                    (1, 3, 2)])
     self.assertFalse(graph.is_semi_eularian)
コード例 #17
0
 def test_find_dead_ends_simple_correct(self):
     """ Find dead ends in a graph. """
     # A triangle with an antenna
     graph = Graph([(1, 2, 1), (1, 4, 2), (2, 3, 1), (2, 4, 1)])
     self.assertCountEqual([Edge(2, 3, 1)], eularian.find_dead_ends(graph))
コード例 #18
0
 def test_is_bridge_false(self):
     #  Two triangles 1-2-3 and 4-5-6 joined by '3-4' bridge
     graph = Graph([(1, 2, 1), (1, 3, 1), (2, 3, 1), (3, 4, 1), (4, 5, 1),
                    (4, 6, 1), (5, 6, 1)])
     self.assertFalse(
         graph.is_bridge(2))  # Edge 2 aka '2-3' is not a bridge
コード例 #19
0
 def test_is_bridge_true(self):
     #  Two triangles joined by 'CD'
     graph = Graph([(1, 2, 1), (1, 3, 1), (2, 3, 1), (3, 4, 1), (4, 5, 1),
                    (4, 6, 1), (5, 6, 1)])
     self.assertTrue(graph.is_bridge((3, 4, 1)))
コード例 #20
0
 def test_add_edge_correct_easy(self):
     graph = Graph([(1, 2, 1)])  # One edge, two nodes
     graph.add_edge(2, 3, 2)  # Add an edge from 2 to 3 w/ cost 2
     self.assertEqual([(1, 2, 1), (2, 3, 2)], graph.all_edges)