コード例 #1
0
    def test_dijkstra(self):
        g = Dijkstra(9)
        g.graph = [[0, 4, 0, 0, 0, 0, 0, 8, 0], [4, 0, 8, 0, 0, 0, 0, 11, 0],
                   [0, 8, 0, 7, 0, 4, 0, 0, 2], [0, 0, 7, 0, 9, 14, 0, 0, 0],
                   [0, 0, 0, 9, 0, 10, 0, 0, 0], [0, 0, 4, 14, 10, 0, 2, 0, 0],
                   [0, 0, 0, 0, 0, 2, 0, 1, 6], [8, 11, 0, 0, 0, 0, 1, 0, 7],
                   [0, 0, 2, 0, 0, 0, 6, 7, 0]]

        self.assertEqual(g.dijkstra(0), [0, 4, 12, 19, 21, 11, 9, 8, 14])
コード例 #2
0
    def test_dijkstra_no_edges(self):
        g = Dijkstra(9)
        g.graph = [
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
        ]

        self.assertEqual(
            g.dijkstra_min_heap(0),
            [
                0,
                float('inf'),
                float('inf'),
                float('inf'),
                float('inf'),
                float('inf'),
                float('inf'),
                float('inf'),
                float('inf'),
            ],
        )
        self.assertEqual(
            g.dijkstra_fib_heap(0),
            [
                0,
                float('inf'),
                float('inf'),
                float('inf'),
                float('inf'),
                float('inf'),
                float('inf'),
                float('inf'),
                float('inf'),
            ],
        )
コード例 #3
0
    def test_dijkstra_large_graph(self):
        g = Dijkstra(17) 
        g.graph = [[0, 3, 5, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
           [3, 0, 8, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
           [5, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],            
           [0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 0, 0, 8],            
           [0, 0, 0, 0, 0, 0, 0, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0],            
           [0, 1, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0], 
           [0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0],            
           [7, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 4, 0, 0, 1, 0, 0], 
           [0, 0, 0, 0, 6, 0, 0, 0, 0, 4, 0, 9, 0, 0, 0, 0, 0], 
           [0, 0, 0, 0, 0, 7, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, 0], 
           [0, 0, 0, 2, 0, 0, 5, 0, 0, 0, 0, 0, 0, 2, 0, 0, 3], 
           [0, 0, 0, 0, 0, 0, 0, 4, 9, 0, 0, 0, 6, 0, 9, 5, 0], 
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 6, 0, 5, 0, 9, 0], 
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 5, 0, 0, 0, 1], 
           [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 9, 0, 0, 0, 2, 0], 
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 9, 0, 2, 0, 9],
           [0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 3, 0, 0, 1, 0, 9, 0] 
          ] 

        self.assertEqual(g.dijkstra(0), [0, 3, 5, 21, 13, 4, 24, 7, 15, 11, 19, 11, 12, 17, 8, 10, 18])
        self.assertEqual(g.dijkstra_min_heap(0), [0, 3, 5, 21, 13, 4, 24, 7, 15, 11, 19, 11, 12, 17, 8, 10, 18])
        self.assertEqual(g.dijkstra_fib_heap(0), [0, 3, 5, 21, 13, 4, 24, 7, 15, 11, 19, 11, 12, 17, 8, 10, 18])
コード例 #4
0
    def test_dijkstra_empty_graph(self):
        g = Dijkstra(0)
        g.graph = []

        self.assertEqual(g.dijkstra_min_heap(0), [])
        self.assertEqual(g.dijkstra_fib_heap(0), [])
コード例 #5
0
    def test_dijkstra_small_graph(self):
        g = Dijkstra(8) 
        g.graph = [[0, 0, 8, 0, 0, 0, 0, 0], 
           [0, 0, 8, 1, 0, 4, 0, 0], 
           [8, 8, 0, 0, 6, 0, 0, 0], 
           [0, 1, 0, 0, 0, 1, 0, 6], 
           [0, 0, 6, 0, 0, 0, 0, 0], 
           [0, 4, 0, 1, 0, 0, 3, 5], 
           [0, 0, 0, 0, 0, 3, 0, 6], 
           [0, 0, 0, 6, 0, 5, 6, 0] 
          ] 
        self.assertEqual(g.dijkstra(0), [0, 16, 8, 17, 14, 18, 21, 23])
        self.assertEqual(g.dijkstra_min_heap(0), [0, 16, 8, 17, 14, 18, 21, 23])
        self.assertEqual(g.dijkstra_fib_heap(0), [0, 16, 8, 17, 14, 18, 21, 23])

        g = Dijkstra(9)
        g.graph = [
            [0, 4, 0, 0, 0, 0, 0, 8, 0],
            [4, 0, 8, 0, 0, 0, 0, 11, 0],
            [0, 8, 0, 7, 0, 4, 0, 0, 2],
            [0, 0, 7, 0, 9, 14, 0, 0, 0],
            [0, 0, 0, 9, 0, 10, 0, 0, 0],
            [0, 0, 4, 14, 10, 0, 2, 0, 0],
            [0, 0, 0, 0, 0, 2, 0, 1, 6],
            [8, 11, 0, 0, 0, 0, 1, 0, 7],
            [0, 0, 2, 0, 0, 0, 6, 7, 0],
        ]

        self.assertEqual(g.dijkstra(0), [0, 4, 12, 19, 21, 11, 9, 8, 14])
        self.assertEqual(g.dijkstra_min_heap(0), [0, 4, 12, 19, 21, 11, 9, 8, 14])
        self.assertEqual(g.dijkstra_fib_heap(0), [0, 4, 12, 19, 21, 11, 9, 8, 14])