Read · 1 min
Dijkstra's algorithm can be used in weighted graphs to compute the shortest distance from a vertex to all other vertices in the graph.
Suppose our vertices are . Suppose we want to calculate the shortest distances from to all others. We will keep track of a set of solved vertices for those which we know the shortest distance. We will call this shortest distance for any once we calculate it.
Initially let just contain , and . Now we iteratively add elements to , until we can no longer, by the following step:
For each vertex , find its neighbor with the minimum weight . Among all , we add the one that minimizes to , and its distance is .
This is a complicated algorithm, so be sure to visit this interactive demonstration.