def dijkstras(graph, startVert, goalVert): """ This algorithm searches a graph using Dijkstras algorithm to find the shortest path from every point to a goal point (actually searches from goal to every point, but it's the same thing. It uses a priority queue to store the indices of vertices that it still needs to examine. It returns the best path frmo startVert to goalVert, but otherwise startVert does not play into the search.""" num_q_nodes_removed = 0 max_queue_size = -1 if startVert == goalVert: return [] q = PriorityQueue() visited = set() pred = {} cost = {} for vert in graph.getVertices(): cost[vert] = 1000.0 pred[vert] = None q.insert(cost[vert], vert) visited.add(goalVert) cost[goalVert] = 0 q.update(cost[goalVert], goalVert) while not q.isEmpty(): (nextCTG, nextVert) = q.firstElement() if q.getSize() > max_queue_size: max_queue_size = q.getSize() q.delete() num_q_nodes_removed += 1 visited.add(nextVert) # print("--------------") # print("Popping", nextVert, nextCTG) neighbors = graph.getNeighbors(nextVert) for n in neighbors: neighNode = n[0] edgeCost = n[1] if neighNode not in visited and\ cost[neighNode] > nextCTG + edgeCost: # print("Node", neighNode, "From", nextVert) # print("New cost =", nextCTG + edgeCost) cost[neighNode] = nextCTG + edgeCost pred[neighNode] = nextVert q.update(cost[neighNode], neighNode) # This part is finding the best path from all nodes to the goal. Not necessary # for vert in graph.getVertices(): # bestPath = reconstructPath(goalVert, vert, pred) # bestPath.reverse() # print("Best path from ", vert, "to", goalVert, "is", bestPath) print("====> Dijkstra number of nodes visited: ", len(visited)) print("====> Dijkstra number of nodes removed: ", num_q_nodes_removed) print("====> Dijkstra max size of queue: ", max_queue_size) finalPath = reconstructPath(goalVert, startVert, pred) finalPath.reverse() return finalPath
def dijkstras(graph, startVert, goalVert): """ This algorithm searches a graph using Dijkstras algorithm to find the shortest path from every point to a goal point (actually searches from goal to every point, but it's the same thing. It uses a priority queue to store the indices of vertices that it still needs to examine. It returns the best path frmo startVert to goalVert, but otherwise startVert does not play into the search.""" if startVert == goalVert: return [] q = PriorityQueue() visited = set() pred = {} cost = {} for vert in graph.getVertices(): cost[vert] = 1000.0 pred[vert] = None q.insert(cost[vert], vert) visited.add(goalVert) cost[goalVert] = 0 q.update(cost[goalVert], goalVert) while not q.isEmpty(): (nextCTG, nextVert) = q.firstElement() q.delete() visited.add(nextVert) print("--------------") print("Popping", nextVert, nextCTG) neighbors = graph.getNeighbors(nextVert) for n in neighbors: neighNode = n[0] edgeCost = n[1] if neighNode not in visited and\ cost[neighNode] > nextCTG + edgeCost: print("Node", neighNode, "From", nextVert) print("New cost =", nextCTG + edgeCost) cost[neighNode] = nextCTG + edgeCost pred[neighNode] = nextVert q.update( cost[neighNode], neighNode ) for vert in graph.getVertices(): bestPath = reconstructPath(goalVert, vert, pred) bestPath.reverse() print("Best path from ", vert, "to", goalVert, "is", bestPath) finalPath = reconstructPath(goalVert, startVert, pred) finalPath.reverse() return finalPath