Try running the code as is now and see how it does with a graph of vertices.
Oh no! Our new path_and_distances
values have wreaked havoc on the function! Why is this happening?
Our function is breaking when called because we went from tracking only distance to tracking distance AND a path.
Let’s clean this up a bit.
Instructions
Below your infinity-setting for
loop, anywhere you see paths_and_distances[some_variable_name]
, change it to paths_and_distances[some_variable_name][0]
.
This way, you only set and get the distance for those instances. Do NOT change the line you changed in the for
loop though.
Phew! Now at least the code is working again.
Jump down to where you set new_distance
. Add a new line below and set new_path
equal to the path of current_vertex
with the addition of neighbor.name
.
Take a look at paths_and_distances[neighbor][0] = new_distance
. You’ve set the distance for neighbor
in paths_and_distances
.
Add a new line below to set the corresponding path equal to new_path
.
Finally, change your return statement so that it returns the path of target
.