Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the shortest path with the least number of edges

I need to modify Dijkstra's algorithm so that if there are several shortest paths I need to find the one with minimum number of edges on the path.

I've been stuck on how to use Dijkstra's method to find multiple shortest paths, how do you do that? doesn't it always output only 1 shortest path? pseudo code or any general direction would be very helpful.

like image 331
Jacek Trociński Avatar asked Nov 18 '13 07:11

Jacek Trociński


1 Answers

Instead of assigning every node with the distance from source you can assign the number of edges traversed so far also. So each node will have (distance,edges) instead of distance only. Everything else works as usual and for every terminal node you record the one with minimum value of (distance,edges).

like image 59
FUD Avatar answered Nov 04 '22 01:11

FUD