-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathdijkstra.py
More file actions
33 lines (28 loc) · 790 Bytes
/
dijkstra.py
File metadata and controls
33 lines (28 loc) · 790 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
31
32
33
from collections import defaultdict
from heapq import *
def dijkstra(edges, f, t):
g = defaultdict(list)
for l,r,c in edges:
g[l].append((c,r))
q, seen = [(0,f,())], set()
while q:
(cost,v1,path) = heappop(q)
if v1 not in seen:
seen.add(v1)
path = (v1, path)
if v1 == t: return (cost, path)
for c, v2 in g.get(v1, ()):
if v2 not in seen:
heappush(q, (cost+c, v2, path))
return float("inf")
if __name__ == "__main__":
edges = [
("2","3",6),("1","3",6)
]
print "=== Dijkstra ==="
print edges
print "A -> E:"
print dijkstra(edges, "1", "2")
print "F -> G:"
print dijkstra(edges, "1", "3")
print dijkstra(edges, "1", "4")