Colors: Red: d(i) Yellow: node d(j) and arc (i,j) Green: set shortest path to source
At the end of alg shortest path from source to sink is highlighted in yellow.
Each node is given a distance label, d(i), which represents an upper bound on the distance of the shortest path to reach it from the source. To begin all the distance labels are set to infinity except for the source which has a distance label of 0, since it is obviously 0 away from the source. From here the algorithm works by selecting a node whose distance label is greater than the distance label of one of its predecessors plus the distance of the arc between the two and reseting it to this new, lower upper bound. That is if d(j) > d(i) + c(i,j) where c(i j) is the distance of an arc from node i to node j d(j) is set to d(i) + c(i, j). This continues until no more labels can be set. This algorithm works in O(n^2).
Dijkstra
Code
Dijkstra.java
Node.java
Edge.java
Graph.java
MainApplet.java