Skip to content

Resource · Guide or article

Dijkstra's Algorithm

Step-by-step tutorial on Dijkstra's shortest-path algorithm, tracing a priority-queue implementation on a six-node weighted graph from start node A. Suits candidates who know basic graphs and want to explain every distance update in an interview.

Reviewed

Open the original: Dijkstra's Algorithm (opens the original site in a new tab)

Practice this topicOpens the practice interviewer with a question on Graphs.

Add to a roadmap

Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.

Source content: English

Read this in other languages: 한국어, 日本語, 简体中文, 繁體中文, Українська, Español, Français, Deutsch, עברית Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph, which may represent, for example, road networks. The algorithm exists in many variants; Dijkstra's original variant found the shortest path between two nodes, but a more common variant fixes a single node as the "source" node and finds shortest paths from the source to all other nodes in the graph, producing a shortest-path tree.

PrepKit overview

PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.

Suited to
The source does not state a level
Format
Article

What it covers

  • The single-source variant that produces a shortest-path tree, contrasted with Dijkstra's original two-node version
  • A 16-image walkthrough showing nodes entering a min priority queue, the visited set growing, and distances to C, F and D being kept or lowered
  • Recording previousVertices to rebuild the actual route, ending with A to C to F to E as the shortest path to E
  • A list of applications including GPS navigation, OSPF and IS-IS routing and game pathfinding
  • Links to the folder's dijkstra.js implementation and the repository's priority queue data structure

How to use it

PrepKit study advice, not a claim made by the source.

Copy the example graph's edge weights from the step images, run the algorithm on paper, and write down the queue contents after each pull. Then implement it with a binary heap and check that your distances and the path to E match the walkthrough.

Scope and limitations

No complexity figures are given, and negative edge weights are not discussed. The example depends on images, and the code is JavaScript in a separate file.

Access and format

README on GitHub in the MIT-licensed trekhleb/javascript-algorithms repository; English, free to read, with links to translations in nine other languages.

Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88

Evidence behind this overview
  • src/algorithms/graph/dijkstra/README.md

Pages that include this

Get the PrepKit newsletter

Optional. You don't need it to use PrepKit.

How we handle your email