Created
July 3, 2018 11:13
-
-
Save erika-dike/5de1c903195ae82fc12759d7fbc912d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dijkstra_search(graph, start, goal): | |
frontier = PriorityQueue() | |
frontier.put(start, 0) | |
came_from = {} | |
cost_so_far = {} | |
came_from[start] = None | |
cost_so_far[start] = 0 | |
while not frontier.empty(): | |
current = frontier.get()[1] | |
if current == goal: | |
break | |
for neighbor in graph.neighbors(current): | |
new_cost = cost_so_far[current] + graph.cost(current, neighbor) | |
if neighbor not in cost_so_far or new_cost < cost_so_far[neighbor]: | |
cost_so_far[neighbor] = new_cost | |
priority = new_cost | |
frontier.put(neighbor, priority) | |
came_from[neighbor] = current | |
return came_from, cost_so_far |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment