Dijkstra's Algorithm

Initializing live version
Download to Desktop

Requires a Wolfram Notebook System

Interact on desktop, mobile and cloud with the free Wolfram Player or other Wolfram Language products.

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.


Contributed by: Marc Brodie  (March 2011)
(Wheeling Jesuit University)
Open content licensed under CC BY-NC-SA



Feedback (field required)
Email (field required) Name
Occupation Organization
Note: Your message & contact information may be shared with the author of any specific Demonstration for which you give feedback.