This problem can also be solved using Dijkstra’s algorithm.

The one thing to keep in mind is to store the parent vertex of any vertex, so that we can reconstruct the path once we reached the target node. To remember the path, we can use a vector to store where I con from in some vertex. And use the function printPath to print that vector backwards. Here is the printPath function [solution taken from another source.]



Leave a Reply

Your email address will not be published. Required fields are marked *