Esempio n. 1
0
    def test_bfs(self):
        # Figure 22-3
        graph = Graph()
        graph.add_vertex(Vertex('r'))
        graph.add_vertex(Vertex('s'))
        graph.add_vertex(Vertex('t'))
        graph.add_vertex(Vertex('u'))
        graph.add_vertex(Vertex('v'))
        graph.add_vertex(Vertex('w'))
        graph.add_vertex(Vertex('x'))
        graph.add_vertex(Vertex('y'))
        graph.add_2_edges('r', 's')
        graph.add_2_edges('r', 'v')
        graph.add_2_edges('s', 'w')
        graph.add_2_edges('t', 'u')
        graph.add_2_edges('t', 'w')
        graph.add_2_edges('t', 'x')
        graph.add_2_edges('u', 'x')
        graph.add_2_edges('u', 'y')
        graph.add_2_edges('w', 'x')
        graph.add_2_edges('x', 'y')

        came_from = bfs_with_src(graph, 's')[0]
        self.assertDictEqual({
            'v': 'r',
            'r': 's',
            'w': 's',
            't': 'w',
            'x': 'w',
            'y': 'x',
            'u': 't',
        }, came_from)
Esempio n. 2
0
 def test_mohammed_scimitars(self):
     graph = Graph()
     for i in range(11):
         graph.add_vertex(Vertex(i))
     for edge in ((0, 1), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), (3, 5),
                  (3, 6), (4, 5), (4, 8), (5, 6), (5, 8), (6, 7), (6, 10),
                  (7, 8), (7, 9), (7, 10), (8, 9)):
         graph.add_2_edges(edge[0], edge[1])
     self.assertSequenceEqual(
         [0, 1, 2, 3, 5, 4, 8, 7, 6, 10, 7, 9, 8, 5, 6, 3, 1, 4, 0],
         euler_tour_undirected(graph))
Esempio n. 3
0
 def test_tree_diameter(self):
     methods = (tree_diameter_recur, tree_diameter_bfs)
     tree = Graph()
     for i in range(0, 16):
         tree.add_vertex(Vertex(i))
     tree.add_2_edges(0, 1)
     tree.add_2_edges(0, 2)
     tree.add_2_edges(0, 3)
     tree.add_2_edges(1, 4)
     tree.add_2_edges(1, 5)
     tree.add_2_edges(2, 6)
     tree.add_2_edges(3, 7)
     tree.add_2_edges(3, 8)
     tree.add_2_edges(4, 9)
     tree.add_2_edges(5, 10)
     tree.add_2_edges(5, 11)
     tree.add_2_edges(6, 12)
     tree.add_2_edges(6, 13)
     tree.add_2_edges(6, 14)
     tree.add_2_edges(14, 15)
     for tree_diameter_method in methods:
         for root in range(0, 16):
             # print('root: %d' % root)
             self.assertEqual(7, tree_diameter_method(tree, root))
Esempio n. 4
0
    def test_euler_undirected(self):
        graph = Graph()
        for i in range(7, -1, -1):
            graph.add_vertex(Vertex(i))
        graph.add_2_edges(0, 1)
        graph.add_2_edges(1, 2)
        graph.add_2_edges(0, 3)
        graph.add_2_edges(1, 3)
        graph.add_2_edges(1, 4)
        graph.add_2_edges(2, 4)
        graph.add_2_edges(3, 5)
        graph.add_2_edges(3, 6)
        graph.add_2_edges(4, 6)
        graph.add_2_edges(4, 7)
        graph.add_2_edges(5, 6)
        graph.add_2_edges(6, 7)
        self.assertSequenceEqual([0, 1, 2, 4, 6, 3, 5, 6, 7, 4, 1, 3, 0],
                                 euler_tour_undirected(graph))

        graph = Graph()
        for i in range(6):
            graph.add_vertex(Vertex(i))
        graph.add_2_edges(0, 1)
        graph.add_2_edges(0, 2)
        graph.add_2_edges(0, 3)
        graph.add_2_edges(0, 4)
        graph.add_2_edges(1, 2)
        graph.add_2_edges(1, 3)
        graph.add_2_edges(1, 4)
        graph.add_2_edges(2, 3)
        graph.add_2_edges(3, 4)
        graph.add_2_edges(2, 5)
        graph.add_2_edges(4, 5)
        self.assertSequenceEqual([0, 1, 2, 3, 4, 5, 2, 0, 3, 1, 4, 0],
                                 euler_tour_undirected(graph))