-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathshortestpath.py
More file actions
30 lines (22 loc) · 818 Bytes
/
shortestpath.py
File metadata and controls
30 lines (22 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import heapq
from collections import defaultdict
def shortestPath(graph,src,dest):
h = []
# keep a track record of vertices with the cost
# heappop will return vertex with least cost
# greedy SRC -> MIN - > MIN -> MIN -> DEST
heapq.heappush(h,(0,src))
while len(h)!=0:
currcost,currvtx = heapq.heappop(h)
if currvtx == dest:
print("Path Exisits {} to {} with cost {}".format(src,dest,currcost))
break
for neigh,neighcost in graph[currvtx]:
heapq.heappush(h,(currcost+neighcost,neigh))
graph = defaultdict(list)
v,e = map(int,input().split())
for i in range(e):
u,v,w = map(str,input().split())
graph[u].append((v,int(w)))
src,dest = map(str,input().split())
shortestPath(graph,src,dest)