def quick_shortest(graph): N = graph.N-1 distances = {} previous = {} previous[0] = None distances[N] = 0.0 for idx in xrange(N-1,-1,-1): Q = priorityDictionary() for x in graph[idx]: Q[x] = graph[idx][x] + distances[x] small = Q.smallest() previous[idx] = small distances[idx] = Q[small] # get path from previous 21/08/13 09:10:14 paths = [] paths.append(0) start = 0 while start < N: paths.append(previous[start]) start = previous[start] return (distances, paths)
def dijkstra(graph, node_start, node_end=None): distances = {} previous = {} Q = priorityDictionary() for v in graph: distances[v] = graph.INFINITY previous[v] = graph.UNDEFINDED Q[v] = graph.INFINITY distances[node_start] = 0 Q[node_start] = 0 for v in Q: if v == node_end: break for u in graph[v]: cost_vu = distances[v] + graph[v][u] if cost_vu < distances[u]: distances[u] = cost_vu Q[u] = cost_vu previous[u] = v if node_end != None: return { 'cost': distances[node_end], 'path': path(previous, node_start, node_end) } else: return (distances, previous)
def dijkstra(graph, node_start, node_end=None): distances = {} previous = {} Q = priorityDictionary() for v in graph: distances[v] = graph.INFINITY previous[v] = graph.UNDEFINDED Q[v] = graph.INFINITY distances[node_start] = 0 Q[node_start] = 0 for v in Q: if v == node_end: break for u in graph[v]: cost_vu = distances[v] + graph[v][u] if cost_vu < distances[u]: distances[u] = cost_vu Q[u] = cost_vu previous[u] = v if node_end!=None: return {'cost': distances[node_end], 'path': path(previous, node_start, node_end)} else: return (distances, previous)
def dp_graph(graph, node_start, node_end=None): N = graph.N distances = {} previous = {} previous[node_start] = None for idx in xrange(0, node_start + 1, 1): distances[idx] = 0.0 for idx in xrange(node_start + 1, N, 1): Q = priorityDictionary() for x in graph[idx]: Q[x] = distances[x] + graph[idx][x] small = Q.smallest() if small < node_start: previous[idx] = node_start else: previous[idx] = Q.smallest() distances[idx] = Q[small] if node_end: return { 'cost': distances[node_end], 'path': path(previous, node_start, node_end) } else: return (distances, previous)
def dijkstra(g, length_graph, node_start): distances = {} Q = priorityDictionary() visited = [] for v in g: distances[v] = g.INFINITY Q[v] = g.INFINITY distances[node_start] = 0 Q[node_start] = 0 for v in Q: visited.append(v) if len(visited) == length_graph: break for u in g[v]: if u in visited: continue cost_vu = distances[v] + g[v][u] if cost_vu < distances[u]: distances[u] = cost_vu Q[u] = cost_vu return distances
def dp_graph(graph, node_start, node_end=None): N = graph.N distances = {} previous = {} previous[node_start] = None for idx in xrange(0, node_start+1, 1): distances[idx] = 0.0 for idx in xrange(node_start+1,N,1): Q = priorityDictionary() for x in graph[idx]: Q[x] = distances[x] + graph[idx][x] small = Q.smallest() if small < node_start: previous[idx] = node_start else: previous[idx] = Q.smallest() distances[idx] = Q[small] if node_end: return {'cost': distances[node_end], 'path': path(previous, node_start, node_end)} else: return (distances, previous)
def shortest_path(self, graph, node_start, node_end=UNDEFINED): INFINITY = 999 UNDEFINED = None distances = {} previous = {} Q = priorityDictionary() # Q = {} # print(type(INFINITY)) for v in graph: distances[v] = INFINITY previous[v] = UNDEFINED Q[v] = INFINITY distances[node_start] = 0 Q[node_start] = 0 for v in Q: if v == node_end: break for u in graph[v]: dist_vu = distances[v] + graph[v][u] if dist_vu < distances[u]: distances[u] = dist_vu Q[u] = dist_vu previous[u] = v if node_end: return { 'dist': distances[node_end], 'path': self.path(previous, node_start, node_end) } else: return (distances, previous)
def Dijkstra(self, node_start, node_end=None): ### # Dijkstra's algorithm # only use part of the the graph, i.e. aGraph (in the approximity of startPt # and endPt) to build Dict gDict in order to reduce complexity ### distances = {} previous = {} Q = priorityDictionary() for v in self.vertList: distances[v] = maxVal previous[v] = None Q[v] = maxVal distances[node_start] = 0 Q[node_start] = 0 for v in Q: if v == node_end: break for u in self.vertList[v].getConnections(): cost_vu = distances[v] + self.vertList[v].connectedTo[u][0] if u in self and cost_vu < distances[u]: distances[u] = cost_vu Q[u] = cost_vu previous[u] = v if node_end: return {'dist': distances[node_end], 'path': self.path(previous, node_start, node_end)} else: return (distances, previous)
def dijkstra(graph, node_start, node_end=None, visited=[]): distances = {} previous = {} Q = priorityDictionary() visited_copy = deepcopy(visited) for v in graph: distances[v] = graph.INFINITY previous[v] = graph.UNDEFINDED Q[v] = graph.INFINITY ''' distances[v] = graph.INFINITY previous[v] = graph.UNDEFINDED Q[v] = graph.INFINITY ''' distances[node_start] = 0 Q[node_start] = 0 for v in Q: visited_copy.append(v) if v == node_end: break for u in graph[v]: if u in visited_copy: continue cost_vu = 0 if distances[v] > graph[v][u]: cost_vu = distances[v] else: cost_vu = graph[v][u] if cost_vu < distances[u]: distances[u] = cost_vu Q[u] = cost_vu previous[u] = v ''' cost_vu = distances[v] + graph[v][u] if cost_vu < distances[u]: distances[u] = cost_vu Q[u] = cost_vu previous[u] = v ''' if node_end != None: return {'cost': distances[node_end], 'path': path(previous, node_start, node_end)} else: return (distances, previous)
def dijkstra(graph, node_start, node_end=None, visited=[]): distances = {} previous = {} Q = priorityDictionary() visited_copy = deepcopy(visited) for v in graph: distances[v] = graph.INFINITY previous[v] = graph.UNDEFINDED Q[v] = graph.INFINITY ''' distances[v] = graph.INFINITY previous[v] = graph.UNDEFINDED Q[v] = graph.INFINITY ''' distances[node_start] = 0 Q[node_start] = 0 for v in Q: visited_copy.append(v) if v == node_end: break for u in graph[v]: if u in visited_copy: continue cost_vu = 0 if distances[v] > graph[v][u]: cost_vu = distances[v] else: cost_vu = graph[v][u] if cost_vu < distances[u]: distances[u] = cost_vu Q[u] = cost_vu previous[u] = v ''' cost_vu = distances[v] + graph[v][u] if cost_vu < distances[u]: distances[u] = cost_vu Q[u] = cost_vu previous[u] = v ''' if node_end != None: return { 'cost': distances[node_end], 'path': path(previous, node_start, node_end) } else: return (distances, previous)
def dijkstra(graph, node_start, node_end=None): distances = {} previous = {} Q = priorityDictionary() for v in graph: for u in v.neighbors: distances[u] = inf previous[u] = None Q[u] = inf distances[node_start] = 0 Q[node_start] = 0 for v in Q: if v == node_end: break v_node = graph.get_node(v) if not v_node.is_active(): continue for u in v_node.neighbors: if not graph.get_node(u).is_active(): continue edge = graph.get_edge(v,u) if not edge.is_active(): continue cost_vu = distances[v] + edge.data.length if cost_vu < distances[u]: distances[u] = cost_vu Q[u] = cost_vu previous[u] = v if node_end: return {'cost': distances[node_end], 'path': path(previous, node_start, node_end), 'distances': distances} else: return (distances, previous)
def dijkstra3(graph, node_start, node_end=None, all_paths=None): distances = {} previous = {} Q = priorityDictionary() for v in graph: distances[v] = DiGraph.INFINITY previous[v] = DiGraph.UNDEFINDED Q[v] = DiGraph.INFINITY for u in graph[v]: if u in distances: continue distances[u] = DiGraph.INFINITY distances[node_start] = 0 Q[node_start] = 0 for v in Q: if v == node_end: break if not graph[v]: continue for u in graph[v]: newcost = graph[v][u] for w in range(0, len(all_paths)): if u == all_paths[w]: newcost = 100 cost_vu = distances[v] + newcost if cost_vu < distances[u]: distances[u] = cost_vu Q[u] = cost_vu previous[u] = v if node_end: return { 'cost': distances[node_end], 'path': path(previous, node_start, node_end) } else: return (distances, previous)
def dijkstra(graph, node_start, node_end=None): #print"graph",graph #print node_start, node_end #node_start = node_start.encode('utf-8') #node_end = node_end.encode('utf-8') distances = {} previous = {} Q = priorityDictionary() for v in graph: ##print "-%s-"%v distances[v] = DiGraph.INFINITY previous[v] = DiGraph.UNDEFINDED Q[v] = DiGraph.INFINITY for u in graph[v]: if u in distances: continue distances[u] = DiGraph.INFINITY distances[node_start] = 0 Q[node_start] = 0 for v in Q: if v == node_end: break if not graph[v]: continue for u in graph[v]: cost_vu = distances[v] + graph[v][u] if cost_vu < distances[u]: distances[u] = cost_vu Q[u] = cost_vu previous[u] = v if node_end: return {'cost': distances[node_end], 'path': path(previous, node_start, node_end)} else: return (distances, previous)
def quick_shortest(graph): N = graph.N - 1 distances = {} previous = {} previous[0] = None distances[N] = 0.0 for idx in xrange(N - 1, -1, -1): Q = priorityDictionary() for x in graph[idx]: Q[x] = graph[idx][x] + distances[x] small = Q.smallest() previous[idx] = small distances[idx] = Q[small] # get path from previous 21/08/13 09:10:14 paths = [] paths.append(0) start = 0 while start < N: paths.append(previous[start]) start = previous[start] return (distances, paths)