0

I was given a task where I need to apply Dijkstra's Shortest Path Algorithm in my python code. Let say we have a few cities involved. I need to find the one that has the shortest route. In summary, the task is to:

  • Find the shortest route.
  • List all the possible paths.

I was successfully be able to find the shortest path among the cities, but I do not know how to list all the possible paths using heapq. Can anyone help me?

This is the graph I provided for better understanding of the graph:

The graph that I construct

The Code:

import heapq
from collections import defaultdict
global shortestPath
global shortestCost
shortestPath = "null"
shortestCost = 100000
def dijkstra(graph,src,dest): 
    h = []
  
    heapq.heappush(h,(0,src))
    global shortestPath
    global shortestCost
    global source
    while len(h)!=0:
        currcost,currvtx = heapq.heappop(h)
        if currvtx == dest:
            print("\t\t\t{} to {} with cost {}".format(src,dest,currcost))
            if currcost < shortestCost:
                if dest == src:
                    continue
                else:
                    shortestCost = currcost
                    shortestPath = dest
            break
        for neigh,neighcost in graph[currvtx]:
            heapq.heappush(h,(currcost+neighcost,neigh))

city = ["A","B","C","D","E","F"]
graph = defaultdict(list) 
graph["A"] = [("B",1),("C",5)]
graph["B"] = [('C', 6), ('D', 23)]
graph["C"] = [('E', 35)]
graph["D"] = [('F', 26)]
graph["E"] = [('D', 54)]

print ("\t=============================================================================")
print ("\t                     Dijkstra's Shortest Path Algorithm")
print ("\t=============================================================================")
print ("\n\tAssume 1 cost = 10 km")
print ("\n\tCity availability: {} ".format(city))
src = "A"
print ("\n\tPossible paths from {} :\n".format(src))
for i in city:
    dijkstra(graph,src,i)
print ("\n\t=============================================================================")
print("\t\tThe shortest path from {} is {} to {} with cost {}".format(src,src,shortestPath,shortestCost))
print ("\t=============================================================================")

The output:

    =============================================================================
                         Dijkstra's Shortest Path Algorithm
    =============================================================================

    Assume 1 cost = 10 km

    City availability: ['A', 'B', 'C', 'D', 'E', 'F'] 

    Possible paths from A :

            A to A with cost 0
            A to B with cost 1
            A to C with cost 5
            A to D with cost 24
            A to E with cost 40
            A to F with cost 50

    =============================================================================
        The shortest path from A is A to B with cost 1
    =============================================================================

My apologies if this post seems unfitting as this is my first question that I have posted in here.

Your help is highly appreciated. Thank you.

0 Answers0