0

Dijkstras algorithm assumes the closest neighbor based on edge weight between the start and intermediate node. This is repeated until the destination node is reached.

What if the shortest path between start node and an intermediate node is an indirect route through several other intermediate nodes?

saladguy
  • 75
  • 1
  • 1
  • 7

3 Answers3

2

What if the shortest path between start node and an intermediate node is an indirect route through several other intermediate nodes?

Traveling through multiple nodes is usually the case when trying to find the shortest path. If there aren't multiple possible paths then why would you need Dijkstra?

Imagine the following graph:

Graph example 1

To get a better understanding lets say the algorithm starts by traveling clockwise, starting at START to the 1 node. It will find out that START -> 1 -> 6 -> END costs 7. It will then go counterclockwise and find out that START -> 3 -> 5 -> 8 -> 9 -> END costs 5. The algorithm will then mark the counterclockwise path as the shortest path to get from START to END.

Now let's say we have the following graph:

Graph example 2

The algorithm will find that START -> 1 -> 9 costs 5 (clockwise) and that START -> 3 -> 5 -> 8 -> 9 costs 4 (counterclockwise). So the algorithm will mark the counterclockwise path as the shortest path to get from START to END, which costs 5. Next, the algorithm will try to find another, if possible shorter, path through START -> 1 -> 6 -> END. It will find that this path costs 4 and it will mark this path as the shortest path to get from START to END.

Jevgeni Geurtsen
  • 3,133
  • 4
  • 17
  • 35
  • These examples do not have a direct path from start to end (regardless of whether it is lower or higher cost than the indirect path); all the paths have intermediate nodes. – MT0 Oct 17 '17 at 15:01
1

Take the simple graph with costs for each edge in brackets:

   _____ (5)
  /     \
Root   Dest
  \__A__/
 (2)    (1)

Then Dijkstra's algorithm using a min-priority queue will:

  • Start at the root and put all the paths formed by the adjacent edges onto the queue:
    queue = [(root-A : 2), (root-dest : 5)]
  • Remove the path from the queue with minimum cost:
    • Note that this is the first time A has been visited so the minimum cost to reach A is 2 (and if any subsequent, higher-cost, paths reach A then they can be ignored).
    • Put all the all the paths formed by that path and the adjacent edges onto the queue:
      queue = [(root-A-dest : 3), (root-dest : 5)]
  • Remove the path from the queue with minimum cost:
    • Note that this is the first time dest has been visited so the minimum cost to reach dest is 3.
    • The destination has been reached so the algorithm will stop.

So, with a min-priority queue, if there is a shorter path through indirect vertices compared to the direct path then the direct edge can remain unprocessed in the queue.

If the indirect path between 2 graph nodes is shorter than the direct path, can Dijkstras algorithm detect it?

Yes, the example above demonstrates that it will.

MT0
  • 143,790
  • 11
  • 59
  • 117
0

I don't think you quite understand how the algorithm works. Yes, it checks the nearest neighbor first - but it still checks all of the other neighbors, it doesn't discard them simply because they weren't the nearest. If the shortest path truly goes through an indirect path, then each node along that path will necessarily have a shorter total distance than, and therefore be visited before, the longer direct path. The prohibition of negative weights in the input to the algorithm is needed to ensure this.

jasonharper
  • 9,450
  • 2
  • 18
  • 42