3

I recently had to take a look at the Dijkstra algorithm and it's proof.

The proof of the algorithm terminates in all sources I could find with the equality of quantity of all vertices and the quantity of all vertices visited (e.g. R=V or S=V). Moreover the while-loop of this algorithm terminates when Q (initiated as all vertices of the graph) is empty, so all vertices have to be visited.

I don't get why this has to be the case. Aren't there graphs where the algorithm doesn't has to visit all vertices e.g.: The start vertex is connected to the "find" vertex directly with weight 1 and to other vertices with weight 10.

I hope you get the problem I have here.

Edit: This is the pseudo-code I used from Cormen:

enter image description here

BenStudio
  • 51
  • 1
  • 4
  • 1
    The algorithm for the shortest path between two nodes stops as soon as the destination node has been reached. Maybe you mix up two problems. –  Jun 08 '18 at 09:14
  • According to [Wiki](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm), `If the destination node has been marked visited ... then stop`. – Pham Trung Jun 08 '18 at 09:37
  • Wiki tells me "Dijkstra's original variant found the shortest path between two nodes, but a more common variant fixes a single node as the 'source' node and finds shortest paths from the source to all other nodes in the graph, producing a shortest-path tree." Which flavour are you interested in? – AakashM Jun 08 '18 at 10:10
  • 1
    This is where I had my mistake... So the algorithm I am facing doesn't look for one specific node but creates a shortest path tree like you said... well this finally solves my problem. Thank you – BenStudio Jun 08 '18 at 10:14

1 Answers1

6

Dijkstra's algorithm in default form computes the shortest distance from a starting node to all connected nodes. Even in this form it does not visit all nodes: only the vertices of a connected component have to be checked.

For the shortest path between two nodes there is a more efficient version which stops once all possible routes are longer than the shortest one found so far. This version does not always visit all nodes of the connected sub-graph.

Beginner
  • 5,277
  • 6
  • 34
  • 71