title: Dijkstra source to destination shortest path in directed, weighted graph
body: <p>In the <a href="https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Pseudocode" rel="nofollow">wiki page</a> on Dijkstra, I am informed that if destination is known, I can terminate the search after line <code>13</code>. I don't get this, how do I terminate the search after line <code>13</code>?</p>
  
  <pre><code>1  function Dijkstra(Graph, source):
  2
  3      dist[source] ← 0                       // Distance from source to source
  4      prev[source] ← undefined               // Previous node in optimal path initialization
  5
  6      create vertex set Q
  7
  8      for each vertex v in Graph:             // Initialization
  9          if v ≠ source:                      // v has not yet been removed from Q (unvisited nodes)
  10             dist[v] ← INFINITY             // Unknown distance from source to v
  11             prev[v] ← UNDEFINED            // Previous node in optimal path from source
  12         add v to Q                          // All nodes initially in Q (unvisited nodes)
  13     
  14     while Q is not empty:
  15         u ← vertex in Q with min dist[u]    // Source node in the first case
  16         remove u from Q 
  17         
  18         for each neighbor v of u:           // where v is still in Q.
  19             alt ← dist[u] + length(u, v)
  20             if alt &lt; dist[v]:               // A shorter path to v has been found
  21                 dist[v] ← alt 
  22                 prev[v] ← u 
  23
  24     return dist[], prev[]
  </code></pre>