简体   繁体   中英

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 eg 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 :

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

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.

Sounds like you're looking for A* . It's a variant of Djikstra's which uses a heuristic to speed up the search. Under certain reasonable assumptions, A* is the fastest optimal algorithm. Just make sure to always break ties towards the endpoint .

There are also variants of A* which can provide near-optimal paths in much shorter time. See for example here and here .


Bellman-Ford (as suggested in your question) tends to be slower than either Djikstra's or A* - it is primarily used when there are negative edge-weights, which there are not here.

The A* algorithm would be great for this; it achieves better performance by using heuristics.

Here is a simple tutorial to get you started: Link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM