'Shortest Path First' type algorithm


I wanted to make a railway program in java that could find the shortest path from one station to another, where some stations are on many railway lines. If the stations and railway lines are stored in a database, and the user selects the start and end station could a algorithm such as Dijkstra's Shortest Path Algorithm solve the problem?? Does anyone know how i can implement this in java?

Your help is very much appreciated,

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Yes.  Since the edges (rail lengths) are not negative, Dijkstra can be used to solve the problem.
Implementing Dijkstra in Java is a matter of a few lines of code, but the problem is that you need to have all your database loaded in memory.

You can find an implementation here: http://renaud.waldura.com/doc/java/dijkstra/
With railway lines you have to interchange between them, there are 'weights' that needs to be considered.  On the London Underground, if one were travelling from Cockfosters to Earls Court the algorithm might work out that you travel on the Piccadilly line to Finsbury Park, change on to the Victoria line, travel to Green Park, then get back onto the Piccadilly line to finish the journey on that line.  If you were to do this it would appear shorter (less stations), but the interchange at Green Park takes 4-5 minutes (Finsbury Park is across the platform), then you have the headway between the trains to consider (and how many suitcases you were carrying).  This may result in the shorter journey taking longer in reality.  The 4-5 minute interchange, the frequency of the train service and the mobility of the passenger needs to be built into the algorithm.

(I wrote a program in 8080 Assembler many years ago that worked out journeys accurately, but I used a much more basic technique than Dijkstra. For basic you can use the word lazy if you like).
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

re08149383Author Commented:

That's the sort of thing i want, but for simplicity lets say it takes 2 mins between stations and 5 mins for an interchange, and to find the shortest time... if i were to have two table one with station data and one with line data i.e cetral, district etc... i would face a many-many problem on the database side, so is there any way i could get around this?... I'm really stuck, cos i know i need an associative table in-between to make two one-many relationships, but i don't know what it should contain?... does anyone have any sample code to get this to work?


The lazy way to do this is to start off at the Destination station (or the start location).  You have a linked list of the next stations for all stations.  The list for Earls Court would look something like this:-

Earls Court
- West Brompton
- Olympia
- West Kensington
- Barons Court
- Gloucester Road
- High St Ken

This is a simplification because I haven't put any "line" information in there

So you do this for all stations.

Initialise all stations with -1 value (ie an invalid value)

Having done this, you then simply start off with a stationdistancefromstart=0 at Earls Court.  You then iterate through all stations and you say

if the station being considered has a value >=0 then the Next stns to that station will get the next value up (provided they haven't already got a non zero value in them that is less than the current value) you've mentioned 2 mins, so let's so the iteration is 2.

So at the end of the first iteration Earls Court will contain value 0, all of the other stations mentioned earlier will contain value 2.

and so it goes, the loop ends when the destination gets the current iteration value stored.

This is a simplification of course.  The "interchange" problem is left as an exercise for the reader - however for a cut in the profits of this app you are selling....       ;-))

Earls Court actually is an example of a difficult (challenging) situation you will come up against as there are parallel routes which diverge at each end.

I believe the technique outlined is called a Relaxation technique. Apply heat to the edge of a piece of iron.  The contours of colour that appear across the metal indicate the temperature rise spreading across the metal, but eventually equilibrium is reached when the metal reaches a steady temperature across its surface.
re08149383Author Commented:
Thanks for your reply... i admit that i don't really understand it as i have only just started to learn about databases and i've been given this assignment to do for college :( ........so i'm not selling it :-) he he. I've been told that i'm looking too much into this and that the solution is not that hard... hmmm, doesn't feel like it. I have to put all this data into databases with SQL and stuff and emphasise less on java and JDBC (unfortunatly), as if this was just a java exercise, i hopefully could've done this without any help. Do you think this can be done with databases?

I appreciate all your help,

I believe the EE rules have something to say about questions such as this - another reason for pointing you iin the direction of answers, rather than solving them.  

If the college say you should use Dijkstra, then I think they will want you to use Dijkstra.  You need to find a web link that explains Dijkstra in non-mathematical jargon, together with a simple example. A couple of EE'ers suggested links earlier on and I led you on what turns out to be a wild goose chase.  

In your situation Java is a bit like a car.  It sounds like if you wanted to get from London to Edinburgh, you are more interested in building the car that will take you there, rather than planning the journey itself. Agreed you will not get there without the car, but you can always hire one, or indeed you can fly there or go by train (i.e., you will find that you have more options as a systems analyst than as a programmer).

Are you allowed to redefine your specs?... i.e. if you are allowed to specify which lines to use then you can ensure you only have one or less interchange per journey... you can have a table that gives each station on a line a value and use that to determine the time... simply using if else statements in java for this for interchanges might help (hint hint hint).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.