# Tutorial :How to find minimum number of transfers for a metro or railway network? ### Question:

I am aware that Dijkstra's algorithm can find the minimum distance between two nodes (or in case of a metro - stations). My question though concerns finding the minimum number of transfers between two stations. Moreover, out of all the minimum transfer paths I want the one with the shortest time.

Now in order to find a minimum-transfer path I utilize a specialized BFS applied to metro lines, but it does not guarantee that the path found is the shortest among all other minimum-transfer paths.

I was thinking that perhaps modifying Dijkstra's algorithm might help - by heuristically adding weight (time) for each transfer, such that it would deter the algorithm from making transfer to a different line. But in this case I would need to find the transfer weights empirically.

I have been recommended to add a "penalty" to each time the algorithm wants to transfer to a different subway line. Here I explain some of my concerns about that.

I have put off this problem for a few days and got back to it today. After looking at the problem again it looks like doing Dijkstra algorithm on stations and figuring out where the transfer occurs is hard, it's not as obvious as one might think.

Here's an example: If here I have a partial graph (just 4 stations) and their metro lines: A (red), B (red, blue), C (red), D (blue). Let station A be the source. And the connections are :
---- D(blue) - B (blue, red) - A (red) - C (red) -----

If I follow the Dijkstra algorithm: initially I place A into the queue, then dequeue A in the 1st iteration and look at its neighbors : B and C, I update their distances according to the weights A-B and A-C. Now even though B connects two lines, at this point I don't know if I need to make a transfer at B, so I do not add the "penalty" for a transfer. Let's say that the distance between A-B < A-C, which causes on the next iteration for B to be dequeued. Its neighbor is D and only at this point I see that the transfer had to be made at B. But B has already been processed (dequeued). S

So I am not sure how this "delay" in determining the need for transfer would affect the integrity of the algorithm. Any thoughts?

### Solution:1

You can make each of your weights a pair: `(# of transfers, time)`. You can add these weights in the obvious way, and compare them in lexicographic order (compare # of transfers first, use time as the tiebreaker).

Of course, as others have mentioned, using `K * (# of transfers) + time` for some large enough K produces the same effect as long as you know the maximum time apriori and you don't run out of bits in your weight storage.

### Solution:2

I'm going to be describing my solution using the A* Algorithm, which I consider to be an extension (and an improvement -- please don't shoot me) of Dijkstra's Algorithm that is easier to intuitively understand. The basics of it goes like this:

1. Add the starting path to the priority queue, weighted by distance-so-far + minimum distance to goal
2. Every iteration, take the lowest weighted path and explode it into every path that is one step from it (discarding paths that wrap around themselves) and put it back into the queue. Stop if you find a path that ends in the goal.

Instead of making your weight simply distance-so-far + minimum-distance-to-goal, you could use two weights: Stops and Distance/Time, compared this way:

Basically, to compare:

• Compare stops first, and report this comparison if possible (i.e., if they aren't the same)
• If stops are equal, compare distance traveled

And sort your queue this way.

If you've ever played Mario Party, think of stops as Stars and distance as Coins. In the middle of the game, a person with two stars and ten coins is going to be above someone with one star and fifty coins.

Doing this guarantees that the first node you take out of your priority queue will be the level that has the least amount of stops possible.

### Solution:3

You have the right idea, but you don't really need to find the transfer weights empirically -- you just have to ensure that the weight for a single transfer is greater than the weight for the longest possible travel time. You should be pretty safe if you give a transfer a weight equivalent to, say, a year of travel time.

### Solution:4

As Amadan noted in a comment, it's all about creating right graph. I'll just describe it in more details.

Consider two vertexes (stations) to have edge if they are on a single line. With this graph (and weights 1) you will find minimum number of transitions with Dijkstra.

Now, lets assume that maximum travel time is always less 10000 (use your constant). Then, weight of edge AB (A and B are on one line) is a `time_to_travel_between(A, B) + 10000`.

Running Dijkstra on such graph will guarantee that minimal number of transitions is used and minimum time is reached in the second place.

update on comment
Let's "prove" it. There're two solution: with 2 transfers and 40 minutes travel time and with 3 transfers and 25 minutes travel time. In first case you travel on 3 lines, so path weight will be 3*10000 + 40. In second: 4*10000 + 25. First solution will be chosen.

### Solution:5

I had the same problem as you, until now. I was using Dijkstra. The penalties for transfers is a very good idea indeed and I've been using it for a while now. The main problem is that you cannot use it directly in the weight as you first you have to identify the transfer. And I didn't want to modify the algorithm.

So what I'be been doing, is that each time and you find a transfer, delete the node, add it with the penalty weight and rerun the graph.

But this way I found out that Dijkstra wont work. And this is where I tried Floyd-Warshall which au contraire to Dijkstra compares all possible paths through the graph between each pair of vertices.

It helped me with my problem switching to Floyd-Warshall. Hope it helps you as well. Its easier to code and lot more easier to implement.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »