-2

I am creating an app for a public transports app, and I would need to compute itineraries.

However, I am not sure about what algorithm to use? A classic pathfinding algorithm like Dijkstra's or A*?

The fact that I have to handle both

  1. basic pathfinding (stops to stops without walking)
  2. advanced pathfinding (walking between stops)
  3. harder combining both into one algorithm

makes me wonder what to choose precisely.

Thanks for helping

Mods: Feel free to move the question to the appropriate place if my post isn't where it belongs

Reza Mousavi
  • 4,420
  • 5
  • 31
  • 48
Blax
  • 500
  • 3
  • 7
  • 18
  • Combining all situations in a single algorithm is a "no-toil": the transportation mean characteristics are "hard-coded" in the edge costs. –  Sep 15 '18 at 09:55

1 Answers1

1

Dijkstra vs A* :

A* is faster to compute but can give wrong result, Dijkstra will find the shortest/fastest path all the time.

I'd use Dijkstra for that application.

pseudo-code :

class Path
{
    List<Node> nodesOfThePath;
    int length;
}

List<Path> paths = findAllLinksRelatedTo(startPoint); //You have to write a function to get the nodes linked to the given point
paths.Sort(); //sort using the length, ascending

while (paths.length > 0)
{
    if (paths[0].getLastestNode() == FinalPoint)
        return (paths[0]); //this is the shortest one

    List<Path> tempPaths = findAllLinksRelatedTo(paths[0].getLastestNode()); //get all nodes related to the lastest one of the shortest actual path or null if none was found
    paths.removeAt(0); //remove the shortest path
    foreach (tempPath in tempPaths)
        paths.Add(tempPath);
    paths.sort();
}
return (null); //no path found
Cid
  • 14,968
  • 4
  • 30
  • 45
  • Thanks ! One more question, I would just treat street intersections as nodes and their cost as time is that correct ? – Blax Sep 15 '18 at 09:13
  • Yes it is correct. If you have any kind of intersection, that's a node. The weight of a link between 2 nodes depends of your criterias (length, time to travel, etc...) – Cid Sep 15 '18 at 09:16
  • A\* also always gives the shortest path, if your heuristic is consistent and admissible. A\* is just a generalisation of Dijkstra. – Bernhard Barker Sep 15 '18 at 09:30