The algorithm is very comprehensive and here it is….
Algorithm:-
- Set the starting vertex
- Find the BFS (or DFS) for it
- For every vertex in the BFS
- Find the shortest path from it to the starting vertex
- Add that shortest path to the resultant TREE if its already not there and not forming a loop
