/
DijkstraSingle.py
59 lines (47 loc) · 1.26 KB
/
DijkstraSingle.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
from priorityDict import priorityDictionary
from psim import PSim
def Dijkstra(G,start,end=None):
Distances = {}
Predecessors = {}
Q = priorityDictionary()
Q[start] = 0
for v in Q:
Distances[v] = Q[v]
if v == end: break
for w in G[v]:
length = Distances[v] + G[v][w]
if w in Distances:
if length < Distances[w]:
raise ValueError, "Found a better path to a final vertex"
elif w not in Q or length < Q[w]:
Q[w] = length
Predecessors[w] = v
return (Distances,Predecessors)
"""
I haven't quite been able to get this working, but the idea is to parallelize the
individual iterations of the Dijkstra algorithm too to increase the parallelization.
"""
def Dijkstra_p(G,start,end=None):
D = {} # dictionary of final distances
P = {} # dictionary of predecessors
Q = priorityDictionary() # est.dist. of non-final vert.
Q[start] = 0
comm = PSim(len(Q)+1)
if comm.rank==0:
i = 1
for v in Q:
D[v] = Q[v]
if v == end: break
comm.send(i, v)
i = i + 1
else:
v = comm.recv(0)
for w in G[v]:
vwLength = D[v] + G[v][w]
if w in D:
if vwLength < D[w]:
raise ValueError, "Dijkstra: found better path to already-final vertex"
elif w not in Q or vwLength < Q[w]:
Q[w] = vwLength
P[w] = v
return (D,P)