def test_dijkstra(self):
        graph = [[(2, 1), (5, 2)], [(2, 0), (4, 2), (6, 3), (10, 4)],
                 [(5, 0), (4, 1), (2, 3)], [(2, 2), (6, 1), (1, 5)],
                 [(10, 1), (3, 5), (5, 6)], [(1, 3), (3, 4), (9, 6)],
                 [(5, 4), (9, 5)]]
        djk = Dijkstra()

        actual = djk.dijkstra(graph, 0)
        expected = [0, 2, 5, 7, 11, 8, 16]
        assert actual == expected

        actual = djk.dijkstra(graph, 0, 6)
        expected = [0, 2, 3, 5, 4, 6]
        assert actual == expected
Exemple #2
0
    def johnson(network):
        """
        Calculates the shortest path using Johnson's algorithm
        Parameters
        ----------
        src : str, int
            An arbitrary node that does not exist in the STN.
        Returns
        -------
        distance_matrix : List[List[int]]
            A 2-D list representing the shortest distances between all the nodes
        """
        distance_matrix = [[] for x in range(network.length)]

        potential_function = BellmanFord.bellman_ford(network)

        if not potential_function:
            return False

        for node_idx in range(network.length):
            distance_matrix[node_idx] = Dijkstra.dijkstra(
                network, node_idx, potential_function=potential_function)

        if network.flag:
            network.flag = False

        # network.distance_matrix = distance_matrix
        return distance_matrix
def dijkstra(network, src, succ_direction=True, potential_function=False):
    return Dijkstra.dijkstra(network, src, succ_direction, potential_function)