0

I am trying to implement an algorithm that calculates the shortest path from the far left (A) to the far right node (B).

For example:

-----------------------------------------------
|                                             |
|   x                                         |
|                                  x          |
|                                             |
|         x                                   |
| A                             x             |
|                    x                        |
|                                          B  |
|        x                                    |
-----------------------------------------------

So far I have an adjacency matrix. I have tried to implement this using Dijkstra's algorithm, but I only get the distance using this algorithm, I would also need the path information (which nodes are passed?).

How could I implement this?

ptrck
  • 79
  • 2
  • 8
  • 2
    After running Dijkstra's algorithm, you'll have a distance value for each node. Starting at the end node, find the neighbor with the lowest distance, iteratively, until you reach the start node, which has a distance of 0. This walk gives you the shortest path. – Cris Luengo Apr 25 '19 at 21:38
  • 4
    If you implement Dijkstra's algorithm with a predecessor array, you will be able to recover the path. See https://stackoverflow.com/questions/28998597/how-to-save-shortest-path-in-dijkstra-algorithm/28999743#28999743 – beaker Apr 25 '19 at 21:40

0 Answers0