Search code examples
algorithmshortest-pathpath-finding

Best algorithm to get the path with less changes


I am working with a subway network. I have to find some paths between A and B that respect some rules, for example, the fastest, shortest, etc... I know how to solve those, but one of the rules is to find a path with less changes between lines, is there any algorithm used for that?


Solution

  • Treat a subway line as a node in graph, and connect two nodes by an edge if there is an intersection of two subway lines. Now, you can use Dijkstra's algorithm to find the shortest path.