-
Notifications
You must be signed in to change notification settings - Fork 2
/
4.2_x_dijkstra.py
68 lines (54 loc) · 1.38 KB
/
4.2_x_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
56
57
58
59
60
61
62
63
64
65
66
67
68
from graph import *
import sys
state_unvisited = 'unvisited'
state_visiting = 'visiting'
state_visited = 'visited'
def dijkstra(g, src, dst):
if src == dst: return 0
dist = {}
prev = {}
unvisited = []
for v in g.vertices:
dist[v.name] = sys.maxint
prev[v.name] = None
unvisited.append(v)
dist[src.name] = 0
while len(unvisited) > 0:
n = extract_cheapest(unvisited, dist)
for child in g.get_adjacent(n):
w = g.get_weight(n, child)
if dist[n.name] + w < dist[child.name]:
dist[child.name] = dist[n.name] + w
prev[child.name] = n.name
p = dst.name
path = []
while True:
path.append(p)
if p == src.name:
break
p = prev[p]
return dist[dst.name], path
def extract_cheapest(unvisited, dist):
best_dist = None
best_ix = 0
for i in range(len(unvisited)):
if best_dist == None or dist[unvisited[i].name] < best_dist:
best_dist = dist[unvisited[i].name]
best_ix = i
best = unvisited.pop(best_ix)
return best
def test_dijkstra(g, src, dst):
print 'Shortest route from ' + src.to_string() + ' to ' + dst.to_string() + ': ' + str(dijkstra(g, src, dst))
g = Graph()
v = []
for i in range(11):
v.append(g.add_vertex('V' + str(i), i))
g.add_edge(v[1], v[2]);
g.add_edge(v[1], v[3]);
g.add_edge(v[2], v[4]);
g.add_edge(v[2], v[5]);
g.add_edge(v[3], v[6]);
g.add_edge(v[3], v[7]);
g.add_edge(v[5], v[8]);
print g.to_string()
test_dijkstra(g, v[1], v[8])