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.
Below your infinity-setting
for loop, anywhere you see
paths_and_distances[some_variable_name], change it to
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
Take a look at
paths_and_distances[neighbor] = new_distance. You’ve set the distance for
Add a new line below to set the corresponding path equal to
Finally, change your return statement so that it returns the path of