Dijkstra's algorithm, am I or the teacher mistaken?

71 Views Asked by At

Imagine that Dijkstra’s algorithm has been used to show the length of the shortest path from $a$ to $g$ in the graph in figure 1. Which of the following vertices is added first to the set $S$?

It's a multiple choice answer with options : c , d, e or f.

I just want to see if I understand it right. First it starts at $a$. We write the updated values from $b$, $i$ and $d$. We then take the shortest path, $b$. Then it goes to $b$, and then we update it again and hereafter we can choose either $h$ or $e$. Hence it's the shortest path. Now my question is, is that what you think is meant in the question? And second could I choose $h$ instead of $e$ (like do I choose which one I want to use?)

enter image description here

1

There are 1 best solutions below

0
On

Yes, $e$ is the one added first out of $c,d,e$, and $f$ for the reasons you have said. You are free to choose whichever of $h$ and $e$ you want, it does not affect the correctness of the algorithm, and in this case it also does not affect the answer to the question in your exercise. Note that this answer assumes that by $S$ you mean the set of vertices that have been visited by the algorithm, not the set of vertices on the frontier.