8

enter image description here

In this network Dijkstra’s shortest-path algorithm is used. The question is which path will A use to reach D because both are equal?

enter image description here

is that table missing?

makkuzu
  • 485
  • 2
  • 4
  • 19

2 Answers2

2

It depends on your actual implementation and the way your input graph was described (e.g. edges can go in different order and this will have an impact on the result if there are many).

However, it's guaranteed that it will find some path which has optimal length.

Your table seems to be wrong at E and F vertices. The parent vertex for E is D (AB->BD->DE = 3 + 4 + 2 = 9), so is for F.

dreamzor
  • 5,795
  • 4
  • 41
  • 61
  • So, in this homework should I add both B and C as next router to reach D? – makkuzu Dec 22 '13 at 17:35
  • Indeed, how else would you know if C-D edge doesn't have a length of 1? – dreamzor Dec 22 '13 at 17:37
  • It asks for the shortest path from A to all network nodes by the way – makkuzu Dec 22 '13 at 17:38
  • If you know the *length* of the shortest path from A to D, then it doesn't matter which path you will actually use to go from D to other vertices in case that A->D->...->VERTEX has optimal length. Just pick that one eventual vertex from the queue and go on with it. – dreamzor Dec 22 '13 at 17:42
  • I am sorry but I am little confused so I have added my table on the question above. Now, in this table I should also add Destination D again and for that Next router will be C and cost 7, right? – makkuzu Dec 22 '13 at 17:47
  • Not necessarily, the single B at D is fine. Again, it doesn't matter which particular vertex you've chosen if there are many optimal paths. – dreamzor Dec 22 '13 at 17:49
2

It depends on the implementation of the relaxation function. For example, the algorithm described in the wikipedia uses strictly a less-than comparison: if alt < dist[v] so in this case (and all the implementations I've seen) the shortest path from A to D is A -> B -> D.

Why? Because (S = settled nodes and Q = queue of nodes, a pair of distance, parent):

  1. Start relaxing A, so you get the S = {A:0} and Q = {B:3,A C:5,A D:9,A}
  2. From Q select B and relax it. You get S = {A:0 B:3,A} and Q = {C:(5,A) D:7,B E:10,B}
  3. From Q select C and relax it. You get S = {A:0 B:3,A C:5,A} and Q = {D:7,B E:10,B}.
  4. From Q select D and you can finish the algorithm.

Note that in step 3, you don't need to change the parent of D because the new path is not better than the current path. If the relaxation algorithm uses a less-than-or-equal comparison, then the result will be different.

Javier
  • 376
  • 2
  • 12