Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find minimum number of transfers for a metro or railway network?

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.

Addition to the question:

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?

like image 871
Alisa Avatar asked Jun 29 '10 02:06

Alisa


1 Answers

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.

like image 137
Keith Randall Avatar answered Oct 04 '22 07:10

Keith Randall