Dijkstra's algorithm does not work, my understanding should be erroneous

Here's my interpretation of how Dijkstra's algorithm as described by wikipedia will deal with the graph below.

First, he marks the shortest distance to all neighboring nodes, so A gets 1, and C gets 7. Then he chooses a neighbor with the current shortest path. This A. Origin is marked as visited and will no longer be viewed. The shortest (and only) path from start to B through A is now 12. A is marked as visited. The shortest path from start to destination via B is 13. B is marked as visited. The shortest path from Origin to C through destination is 14, but this is the double previous shortest path to C, which is 7, so 14 is discarded. Destination is now marked as visited.

The destination is marked marked, so the algorithm is completed. Here is the result:

Supposedly failing case for Dijkstra

So did I misinterpret the description? Is the wikipedia description incorrect? Or did I somehow stumble upon a case that shows that Dijkstra's algorithm is incorrect (which I really doubt)? I noticed that the chosen path is chosen greedily, but, apparently, this is one of the defining characteristics of the algorithm, however in this case it seems to give the wrong result.

+4
source share
3 answers

Assign each node an approximate distance value: set it to zero for our initial node and to infinity for all other nodes. Mark all nodes invisible. Set the starting node as current. Create a set of invisible nodes called an invisible set consisting of all nodes except the starting node. (Wiki)

Using your example:

unvisited

A = inf; B = inf; C = inf; Dest = inf; 

visits

 Origin = 0; 

For the current node, consider all of its invisible neighbors and calculate their preliminary distances.

 O => A = 1; O => C = 7; 

Now we are at point A.

From A, the only way is B, this gives us

 O => AB = 12; O => C = 7 

C is now the lowest distance and this is the new current node

 O => CD = 8 

Since D is the destination and 8 <12, the CD route is selected.

+8
source

After visiting B, the total distance traveled so far is 12. 7 is less than 12, and therefore the search will resume at C.

A node is marked only after visiting all its neighbors. At the beginning, the origin is marked as visited, and it is assigned a distance from zero. All other nodes are marked as invisible.

+4
source

He then selects a neighbor with the current shortest path.

No, it selects an invisible node with the shortest path.

Wikipedia writes:

Set the invisible node with the smallest preliminary distance as the next "current node" and return to step 3.

He does not say that this is a neighbor.

And the pseudo-code from Wikipedia says:

  8 while Q is not empty: // The main loop 9 u := vertex in Q with smallest distance in dist[] ; 

where Q contains all the nodes that have not yet been visited.

+2
source

Source: https://habr.com/ru/post/1387477/


All Articles