Lecture 14 Finger Exercise
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Consider the weighted directed graph below for the following questions.
Question 2.
Ben Bitdiddle tried to reproduce the code from lecture for Dijkstra's algorithm, and came up with the following.
While much of it is correct, there is a bug in it.
(You may assume he's correctly using the helper functions If Ben were to run his Specify your answer as a Example format:remove_min(), neighbors(), and update_node().)def dijkstra(graph, start, goal):
queue = [(0, [start])]
finished = set()
while len(queue) > 0:
# get a path off the true frontier
cost, path = remove_min(queue)
node = path[-1]
# return when goal comes off of priority queue
if node == goal:
return cost, path
# update paths to neighbors on priority queue
for next_node, weight in neighbors(graph, node):
if next_node not in finished:
finished.add(next_node)
new_cost = cost + weight
new_path = path + [next_node]
update_node(queue, new_path, new_cost)
return None
dijkstra() implementation with S as start and G as goal:
next_node be assigned to G before the function returns?
Assume that among paths with equal cost on the queue, remove_min() prioritizes paths that end on a letter that is earlier in the alphabet.dict with the key "result" mapping to a tuple, and the key "num_visits" mapping to an int.
The "result" tuple should be of the form (cost, path), where cost is an int and path is a list of strs that begins with "S" and ends with "G".{
"result": (100, ["S", "A", "B", "C", "G"]),
"num_visits": 100,
}