Exemplo n.º 1
0
class TestSearches(unittest.TestCase):
    r'''
       2---->4
      ^|\    |\
     / | \   | V
    1  |  \  | 6
     \ |   \ | ^
      VV    VV/
       3---->5
    '''

    def setUp(self):
        self.test_graph = WeightedDigraph()
        self.test_graph.add_edges([(1, 2, 1), (1, 3, 1), (2, 3, 1), \
            (2, 4, 1), (2, 5, 1), (3, 5, 1), (4, 5, 1), (4, 6, 1), (5, 6, 1)])

    def test_depth_first_search(self):
        self.assertEquals(depth_first_search(self.test_graph, 1, 6), \
            [1, 3, 5, 6])
        self.assertEquals(depth_first_search(self.test_graph, 3, 4), None)
        self.assertEquals(depth_first_search(self.test_graph, 1, 7), None)

    def test_recursive_depth_first_search(self):
        self.assertEquals( \
            recursive_depth_first_search(self.test_graph, 1, 6), \
            [1, 2, 3, 5, 6])
        self.assertEquals( \
            recursive_depth_first_search(self.test_graph, 3, 4), None)
        self.assertEquals( \
            recursive_depth_first_search(self.test_graph, 1, 7), None)

    def test_breadth_first_search(self):
        self.assertEquals(breadth_first_search(self.test_graph, 1, 6), \
            [1, 2, 3, 4, 5, 6])
        self.assertEquals(breadth_first_search(self.test_graph, 5, 1), None)
        self.assertEquals(breadth_first_search(self.test_graph, 1, 7), None)

    def test_short_breadth_first_search(self):
        self.assertEquals(breadth_first_search(self.test_graph, 1, 2), [1, 2])
Exemplo n.º 2
0
 def setUp(self):
     self.test_graph = WeightedDigraph()
     self.test_graph.add_edges([(1, 2, 1), (1, 3, 1), (2, 3, 1), \
         (2, 4, 1), (2, 5, 1), (3, 5, 1), (4, 5, 1), (4, 6, 1), (5, 6, 1)])