0

I'm trying to think of a graph which all edges have positive weights, except of one edge such that Dijkstra's algorithm fails to produce the correct output.

I'd be glad for an idea.

EDIT:
I've seen this graph as a counter-example but I don't understand why. The vertex A would be the last to pop-out from the queue and then we will Relax() the edge A->E. So the path S->A->E will be chosen, which is the correct path (And not S->B->E as was claimed)

enter image description here

Thanks

Elimination
  • 2,619
  • 4
  • 22
  • 38

1 Answers1

2

Dijkstra terminates upon expanding the goal node. Also we use a priority queue in dijkstra (not a queue) so that we expand the node that has the least cost. So in your example A will never be expanded.

  open list = [ S cost:0 ] // priortiy queue
pop S out of open list
  closed list = [ S cost:0 ]
  open list = [ B cost:1 ; A cost:5 ]
pop B out of open list
  closed list = [ S cost:0 ; B cost:1 ]
  open list = [ E cost:2 ; A cost:5 ]
pop E out of open list
// it's better to terminate when we reach the goal but if we don't
// it doesn't make any difference we are going to find the shortest path
// to other nodes
   closed list = [ S cost:0 ; B cost:1 ; E cost:2 ]
   open list = [ A cost:5 ]
pop A out of open list
   // there isn't any nodes that we can push to open list
   closed list = [ S cost:0 ; B cost:1 ; E cost:2 ; A cost:5 ]
   open_list = []

Dijkstra push a node to its closed list upon expanding it because it assumes it has find the shortest path to it. So even if we don't terminate upon reaching a goal we will never expand A because it is in our closed list.

Saeid
  • 4,147
  • 7
  • 27
  • 43
  • What do you mean by "the goal node"? I know that Dijkstra's algorithm terminates when the priority-queue is empty. – Elimination Sep 07 '16 at 14:49
  • 1
    I'll explain it in my answer shortly. – Saeid Sep 07 '16 at 14:50
  • CLRS book says otherwise. We add a vertex to the closed set iff it got out of the priority-queue. – Elimination Sep 07 '16 at 14:55
  • "CLRS book says otherwise. We add a vertex to the closed set iff it got out of the priority-queue" That is exactly what I said, expanding means poping a node out of the p-queue and adding\updating its neighbors in open list. – Saeid Sep 07 '16 at 14:58
  • Alright, but anyway - if you agree that the vertex `A` will popped-out at some point then you should realize that the `A->E` edge will be "relaxed" and so, the `S->A->E` path will be chosen. – Elimination Sep 07 '16 at 15:01
  • No `E` is already in the closed list it won't change anymore. That is the whole point behind the closed list. We won't relax edges to nodes that are in closed list. – Saeid Sep 07 '16 at 15:05
  • @Elimination Anyway if you want to relax nodes indefinitely you will have `Bellman-Ford` algorithm which works on graphs with negative and positive edges. – Saeid Sep 07 '16 at 15:15
  • But `A` isn't in the closed-set right? And you popped it out from the p-queue. Right? so you will relax the edge `A->E`. No doubt. – Elimination Sep 07 '16 at 16:43
  • NO, IT WONT ! Run any implementation of dijkstra you can find and see it for yourself. http://www.algorithmist.com/index.php/Dijkstra's_algorithm – Saeid Sep 07 '16 at 16:50