views:

99

answers:

5

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?

+1  A: 

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.

Jerry Coffin
+1  A: 

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.

Justin L.
+1  A: 

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.

Nikita Rybak
@Nikita: why should I add 10000 to the weight(A, B) if they are on the same line? I thought that the point was to add 10000 to the transfer from A (on line 1) to A (on line 2).
Alisa
@Alisa Idea is the same, only details differ. I expanded answer to show how it works.
Nikita Rybak
@Nikita, I have edited my post. Please see it if you're interested.
Alisa
A: 

Don't forget the important step.

alt text

Martin Beckett
+2  A: 

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.

Keith Randall
I like the idea of the weights pair. I think that might work too.
Alisa
You might want to also add different weights based on the type of transfer that will be occurring. For example, an underground to underground transfer would have a low weight as the underground runs regularly (in the city I live in, every 10 minutes is the slowest), while a underground to bus/trolley transfer would be medium weight, and a trolley/bus to trolley/bus transfer would carry the highest weight as the bus/trolley is impacted by traffic and there is no guaranteeing the amount of time that may be spent waiting for the transfer
thorkia