-
Notifications
You must be signed in to change notification settings - Fork 3
/
dijkstra.py
55 lines (48 loc) · 1.45 KB
/
dijkstra.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# based on pseudocode at http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
import copy
def dijkstra(graph, start_node, target=None):
# initialize distance list
dist = dict()
previous = dict()
dist[start_node] = 0
Q = copy.deepcopy(graph)
def extract_min():
min = None
ret = None
for key in dist:
if key in Q and ((dist[key] < min) if min != None else True):
min = dist[key]
ret = key
if ret is not None:
del Q[ret]
return ret
while Q:
u = extract_min()
for v in graph[u]:
alt = dist[u] + graph[u][v]
if v not in dist or alt < dist[v]:
dist[v] = alt
previous[v] = u
node_list = list()
try:
next = target
while True:
node_list.insert(0,next)
next = previous[next]
except:
pass
return node_list
# graph structure:
# dictionary whose keys are the nodes in the graph
# the value of the dictionary ot each key is a dictionary containing the adjacent nodes and the value of the edge
test_graph = {'A':{'B':1,'C':3},
'B':{'A':1,'C':1},
'C':{'A':3,'B':1},
}
test_graph2 = {'A':{'B':3,'C':1},
'B':{'A':1,'C':1},
'C':{'A':3,'B':1},
}
print dijkstra(test_graph, 'A', 'C')
print dijkstra(test_graph2, 'A', 'C')