Example #1
0
def shortest_paths(graph, start):
    # Dijkstra's algorithm for single source shortest paths
    # O(|V| log |V| + |E| log |V|)
    paths = {node: None for node in graph}
    distances = {node: float('inf') for node in graph}
    distances[start] = 0

    pq = PriorityQueue()
    for node in graph:
        if node == start:
            pq.put(node, 0)
        else:
            pq.put(node, float('inf'))

    while not pq.is_empty():
        node, distance = pq.get()

        for neighbour, edge_cost in graph[node]:
            new_cost = distance + edge_cost

            if new_cost < distances[neighbour]:
                distances[neighbour] = new_cost
                paths[neighbour] = node

                pq.update(neighbour, new_cost)

    return paths, distances
Example #2
0
def shortest_paths(graph, start):
    # Dijkstra's algorithm for single source shortest paths
    # O(|V| log |V| + |E| log |V|)
    paths = {node: None for node in graph}
    distances = {node: float("inf") for node in graph}
    distances[start] = 0

    pq = PriorityQueue()
    for node in graph:
        if node == start:
            pq.put(node, 0)
        else:
            pq.put(node, float("inf"))

    while not pq.is_empty():
        node, distance = pq.get()

        for neighbour, edge_cost in graph[node]:
            new_cost = distance + edge_cost

            if new_cost < distances[neighbour]:
                distances[neighbour] = new_cost
                paths[neighbour] = node

                pq.update(neighbour, new_cost)

    return paths, distances