Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dijkstra's algorithm: What to do if there are two or more nodes with minimal weight?

Tags:

dijkstra

In Dijkstra's algorithm what should I do if there are two or more nodes with minimal weights at a point in the algorithm?

In wikipedia: http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm at step no. 6, it says

"Set the unvisited node marked with the smallest tentative distance as the next 'current node' and go back to step 3."

What if there are two or more nodes with "smallest tentative distance".

Can anyone help me with the algorithm?

like image 478
coolscitist Avatar asked Feb 13 '12 17:02

coolscitist


People also ask

Does Dijkstra's algorithm work where edges can have negative weight?

Dijkstra's algorithm solves the shortest-path problem for any weighted, directed graph with non-negative weights. It can handle graphs consisting of cycles, but negative weights will cause this algorithm to produce incorrect results.

Does Dijkstra work with zero weights?

Dijkstra itself has no problem with 0 weight, per definition of the algorithm. It only gets problematic with negative weights. Since in every round Dijkstra will settle a node. If you later find a negative weighted edge, this could lead to a shorter path to that settled node.

How do you find the shortest path between two nodes in a weighted graph?

One common way to find the shortest path in a weighted graph is using Dijkstra's Algorithm. Dijkstra's algorithm finds the shortest path between two vertices in a graph. It can also be used to generate a Shortest Path Tree - which will be the shortest path to all vertices in the graph (from a given source vertex).


1 Answers

Short Answer

Just pick either. Unless you have another heuristic to work with, you can't tell which would be better to pick.


Bit More Explaination

Think about sorting some elements into an array:

9 6 3 3 8

sorted with lowest first is

3 3 6 8 9

If you were to query that array to determine the lowest, the answer is 3. Which 3 doesn't matter.


Similarly if you were to have some more information. Say for example that those ints were actually floats and were sorted by their integer parts. You might end up with the array:

3.2  3.1  6.0  8.5  9.2

Here you have another heuristic to work with, you can check the decimal part too, and you can determine that 3.1 is the lowest.

like image 185
James Webster Avatar answered Oct 19 '22 01:10

James Webster