Dijkstra's algorithm (or Dijkstra's Shortest path First algorithm, SPF algorithm) is an algorithm for finding the shortest paths between nodes in a graph, which may represent, for example, road networks. A widely used application of shortest path algorithm is network routing protocols, most notably IS-IS (Intermediate System to Intermediate System) and open Shortest path As a solution, he re-observed the algorithm known as Prim's minimal spanning tree algorithm (known earlier to Jarník, and also rediscovered by Prim).He designed the shortest path algorithm and later implemented it for ARMAC for a slightly simplified transportation map of 64 city in the Netherlands (64, so that 6 bits would be sufficient to encode the city number).What is the shortest manner to travel from Rotterdam to Groningen, in general: from given city to given city.

If the destination node has been marked visited (when planning a path between two specific nodes) or if the smallest tentative distance among the nodes in the unvisited set is infinity (when planning a complete traversal; happens when there is no connection between the initial node and remaining unvisited nodes), then stop. Otherwise, choose the unvisited node that is marked with the smallest tentative distance, put it as the new" current node", and go back to step 3.When planning a path, it is actually not necessary to wait until the destination node is" visited" as above: the algorithm can stop once the destination node has the smallest tentative distance among all" unvisited" nodes (and thus could be choose as the next" current").

```
package org.gs.digraph
import org.gs.queue.IndexMinPQ
import scala.annotation.tailrec
/** Solves for shortest path from a source where edge weights are non-negative
*
* @constructor creates a new DijkstraSP with an edge weighted digraph and source vertex
* @param g acyclic digraph, edges have direction and weight
* @param s source vertex
* @see [[https://algs4.cs.princeton.edu/44sp/DijkstraSP.java.html]]
* @author Scala translation by Gary Struthers from Java by Robert Sedgewick and Kevin Wayne.
*/
class DijkstraSP(g: EdgeWeightedDigraph, s: Int) {
require(g.edges forall (_.weight >= 0))
private[digraph] val _distTo = Array.fill[Double](g.numV)(Double.PositiveInfinity)
_distTo(s) = 0.0
private[digraph] val edgeTo = new Array[DirectedEdge](g.numV)
private val pq = new IndexMinPQ[Double](g.numV)
relaxVertices()
private def relaxVertices() {
def relax(e: DirectedEdge) {
val v = e.from
val w = e.to
if (_distTo(w) > _distTo(v) + e.weight) {
_distTo(w) = _distTo(v) + e.weight
edgeTo(w) = e
if (pq.contains(w)) pq.decreaseKey(w, _distTo(w)) else pq.insert(w, _distTo(w))
}
}
@tailrec
def loop() {
if (!pq.isEmpty) {
val v = pq.delMin
g.adj(v) foreach (e => relax(e))
loop()
}
}
pq.insert(s, _distTo(s))
loop()
}
/** returns length of shortest path from source to v */
def distTo(v: Int): Double = _distTo(v)
/** returns if there is a path from source to v */
def hasPathTo(v: Int): Boolean = _distTo(v) < Double.PositiveInfinity
/** returns path from source to v if it exists */
def pathTo(v: Int): Option[List[DirectedEdge]] = {
if (!hasPathTo(v)) None else {
@tailrec
def loop(e: DirectedEdge, path: List[DirectedEdge] ): List[DirectedEdge] = {
if(e != null) loop(edgeTo(e.from), e :: path) else path
}
val path = loop(edgeTo(v), List[DirectedEdge]())
Some(path)
}
}
}
```