Dijkstra's Algorithm
Dijkstra's Algorithm
Dijkstra's algorithm finds the shortest-path spanning tree of a connected graph starting at a given vertex: the unique path in the tree from the starting vertex to any other vertex is the shortest path in the graph between those vertices. If you are interested only in the shortest path to a particular vertex, the algorithm can be stopped once that vertex is reached.
The numbers in parentheses at each vertex are called labels. Initially, the starting vertex has a label of 0, and all other vertices are labeled with . At each stage, the algorithm selects a previously unselected vertex with a minimal label. That vertex and its label turn red (and are added to the tree indicated by blue edges). That label will no longer change and is the length of the shortest path in the graph from the starting vertex to that particular vertex. Other labels are updated to reflect the distance to the corresponding vertex using a path only through previously selected vertices.
∞
Weights for the edges are generated at random, but a fixed set of weights is included to have a repeatable example.