2

I need to write a JavaScript algorithm to find the shortest path between 2 co-ordinates. I have looked at using a few route finding algorithms, such as the A* algorithm.

The difference in my application however is that I know all of the co-ordinates that the path can take.

For example, in the image below, the green square would be the starting place co-ord, with the red square being the end place co-ord. The co-ord represented by every black square would be stored in an an array (or other data structure).

So I need the shortest path from the green square, to the red square, but it can only pass through a black square to get there. Would I still use the A* algorithm to implement this?

Grid

5
  • Can you move diagonal?
    – Martin Cup
    Commented Dec 6, 2016 at 10:17
  • Yes, diagonal movements are allowed. Sorry, I should have specified. @MartinCup
    – Joe Morgan
    Commented Dec 6, 2016 at 10:18
  • 1
    Why wouldn't you be able to use A* ? it seems ok
    – Ji aSH
    Commented Dec 6, 2016 at 10:18
  • From my research, I can only find that A* uses all of the co-ordinates when finding its path. Would I be able to use just the set of black co-ordinates? @Ash
    – Joe Morgan
    Commented Dec 6, 2016 at 10:22
  • You can use A*, but your estimates for the remaining distance will have to be more conservative if the path is fairly restricted. If the estimates become too conservative, then the overhead needed to do them can make A* perform worst than Dijkstra.
    – JerryM
    Commented Dec 6, 2016 at 17:36

1 Answer 1

2

Yes you can use A*. You would calculate the distance (number of moves) from every black coordinate to the red square. Then you got a graph structure from which square to which square you can move and every node in that graph has the distance stored to the red square. Then you apply the A* to that graph and get the shortest path.

EDIT

For A* you need a heuristic, that tells you which node is closer to the endpoint. Calculating the "air distance" between a black field and the red field gives you this heuristic for each field. Then you do A*, which is basically the Dijkstra-Algorithm with a heuristic. In your example the air distance between the green and the red field if the top left corner is (x = 0, y = 0), red is (14, 7) and green is (0, 1) then the air distance would be ABS(14 - 0) + ABS(7 - 1) = 20. So it is very easy to calculate from the coordinates.

5
  • Thanks for the quick answer! Would this work the same with diagonal movements?
    – Joe Morgan
    Commented Dec 6, 2016 at 10:36
  • Yes, you just get 4 more maximum possible connections from one node to others. Because with diagonal movements one node can have maximum 8 possible neighbors, without it can have maximum 4. And you calculate the distance differently, because diagonal is shorter.
    – Martin Cup
    Commented Dec 6, 2016 at 10:38
  • 1
    " And you calculate the distance differently" More precisely, the "king distance" is the max(|x1-x2|, |y1-y2|) (with | ... | being abs). Commented Dec 6, 2016 at 10:47
  • This solution appears to be something like, first do Dijkstra, and then do A*. Once you are done counting black squares (if you were keeping pointers to the next square) you already have your solution so why do A*?
    – JerryM
    Commented Dec 6, 2016 at 17:40
  • I should have been more clear in my answer. I made an edit to it. I think this is a simple example in his question. But if there is a more complex world, A* can come in handy.
    – Martin Cup
    Commented Dec 6, 2016 at 17:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.