Question:
In this code, all path between two point s and d is print.If the element (i,j) of the matrix “adjacency_matrix” represents the time required to go from point i to point j, how can the following program be changed, instead of print all paths, save all the paths and corresponding time to pass that paths, display the shortest path?
Answer:
Add another parameterpaths
to the recursion. Each time you recurse to the deepest point, add a copy of the current path
to paths
:I noticed that you need the path with the shortest cost rather than the path with the least number of points, so I stored the cost value in the matrix into the value of the dictionary and adjusted the structure of your class:
If you have better answer, please add a comment about this, thank you!