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)
def Dijkstra(self): distances = {} individual_dist = {} reverse = {} path = {} final_dist = {} # dictionary of final distances Q = priorityDictionary() # distances of non final predecessors Q[self.start] = 0 visited = [] for road in Q: if road in distances: individual_dist[road] = distances[road] if road in visited: continue if road not in visited: visited.append(road) final_dist[road] = Q[road] if road in reverse: if reverse[road] == 0.3: if self.direction == '+': self.direction == '-' else: self.direction == '+' for neighbour in self.intersctn[road]: if road == self.end: starting = self.intersctn[neighbour][road] ending = self.endPoint else: starting = self.intersctn[road][neighbour] ending = self.intersctn[neighbour][road] if neighbour in visited or starting == ending: continue dist, reverseTime = self.calcDist(road, starting, neighbour, ending, self.direction) totalDist = final_dist[road] + dist if neighbour not in Q or (totalDist <= Q[neighbour]): Q[neighbour] = totalDist path[neighbour] = road reverse[neighbour] = reverseTime distances[neighbour] = dist + reverseTime if road == self.start: distances[self.start], reverse[ self.start] = self.calcDist( self.start, self.startPoint, neighbour, self.intersctn[self.start][neighbour], self.direction) if len(visited) == self.noOfRoads: break return (distances, path, reverse)
def Dijkstra(self): distances={} individual_dist={} reverse={} path={} final_dist = {} # dictionary of final distances Q = priorityDictionary() # distances of non final predecessors Q[self.start] = 0 visited=[] for road in Q: if road in distances: individual_dist[road]=distances[road] if road in visited: continue if road not in visited: visited.append(road) final_dist[road] = Q[road] if road in reverse: if reverse[road]==0.3: if self.direction == '+': self.direction=='-' else: self.direction=='+' for neighbour in self.intersctn[road]: if road==self.end: starting=self.intersctn[neighbour][road] ending=self.endPoint else: starting=self.intersctn[road][neighbour] ending=self.intersctn[neighbour][road] if neighbour in visited or starting == ending: continue dist,reverseTime= self.calcDist(road, starting,neighbour, ending,self.direction) totalDist = final_dist[road] + dist if neighbour not in Q or (totalDist <= Q[neighbour] ): Q[neighbour] = totalDist path[neighbour] = road reverse[neighbour]=reverseTime distances[neighbour]=dist+reverseTime if road==self.start: distances[self.start],reverse[self.start]=self.calcDist(self.start,self.startPoint,neighbour,self.intersctn[self.start][neighbour],self.direction) if len(visited)==self.noOfRoads: break return (distances,path,reverse)
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)