Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dijkstra algorithm alternatives - shortest path in graph, bus routes

i am using slightly modified Dijkstra algorithm in my app but it`s quite slow and i know there have to be a lot better approach. My input data are bus stops with specified travel times between each other ( ~ 400 nodes and ~ 800 paths, max. result depth = 4 (max 4 bus changes or nothing).

Input data (bus routes) :

bus_id | location-from | location-to | travel-time | calendar_switch_for_today
XX | A | B | 12 | 1
XX | B | C | 25 | 1
YY | C | D | 5  | 1
ZZ | A | D | 15 | 0

dijkstraResolve(A,D, '2012-10-10') -> (XX,A,B,12),(XX,B,C,25),(YY,C,D,5) 
=> one bus change, 3 bus stops to final destination
* A->D cant be used as calendar switch is OFF

As you can imagine, in more complicated graphs where e.g. main city(node) does have 170 connections to different cities is Dijkstra slower (~ more then 5 seconds) because compute all neighbours first one by one as it`s not "trying" to reach target destination by some other way...

Could you recommend me any other algorithm which could fit well ?

I was looking on :

  • http://xlinux.nist.gov/dads//HTML/bellmanford.html (is it faster ?)

  • http://jboost.sourceforge.net/examples.html (i do not see
    straightforward example here...)

Would be great to have (just optional things) : - option to prefer minimal number of bus changes or minimal time - option to look on alternatives way (if travel time is similar)

Thank you for tips

like image 334
Martin V. Avatar asked Sep 30 '12 21:09

Martin V.


1 Answers

Maybe A* algorithm? See: http://en.wikipedia.org/wiki/A-star_algorithm

Maybe contraction hierarchies? See: http://en.wikipedia.org/wiki/Contraction_hierarchies.

Contraction hierarchies are implemented by the very nice, very fast Open Source Routing Machine (OSRM):

http://project-osrm.org/

and by OpenTripPlanner:

http://opentripplanner.com/

A* is implemented by a number of routing systems. Just do a search with Google.

OpenTripPlanner is a multi-modal routing system and, as long as I can see, should be very similar to your project.

like image 184
AlexBottoni Avatar answered Sep 24 '22 23:09

AlexBottoni