LearnViz CompSci
Home / Learn / Algorithms / Dijkstra's Algorithm

Intermediate Topic 34 of 51 Algorithms

Dijkstra's Algorithm

Shortest paths from a source with non-negative weights.

How it works

Dijkstra's algorithm finds the shortest paths from a source vertex to all others in a graph with non-negative edge weights. It keeps tentative distances, repeatedly settles the unvisited vertex with the smallest distance, taken from a priority queue, and relaxes its outgoing edges. With a binary heap it runs in O((V + E) log V).

Where it is used

GPS and map routing, network routing protocols such as OSPF, game pathfinding, flight itineraries and any shortest-path query on weighted graphs use Dijkstra's algorithm. A* is a goal-directed extension.

Common pitfall

Negative edge weights make it incorrect; use Bellman-Ford. Processing stale entries from the queue wastes time unless skipped. Using a visited flag at the wrong moment, or not storing parents, loses the path. For unweighted graphs BFS is simpler and faster.

Where it fits in the course

Dijkstra's Algorithm is topic 34 of 51 in Algorithms, rated Intermediate. It follows Topological Sort. The next topic is Bellman-Ford Algorithm.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Dijkstra's Algorithm?
Shortest paths from a source with non-negative weights. It belongs to Algorithms and is an intermediate-level topic.
Where do people use Dijkstra's Algorithm?
GPS and map routing, network routing protocols such as OSPF, game pathfinding, flight itineraries and any shortest-path query on weighted graphs use Dijkstra's algorithm.
What is the most common mistake with Dijkstra's Algorithm?
Negative edge weights make it incorrect; use Bellman-Ford.
What should I learn before this topic?
Start with Topological Sort, the topic just before it in Algorithms.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Algorithms quiz. You can also explore Graph Traversal in the Visual Lab.
Is Dijkstra's Algorithm hard to learn?
It is an Intermediate topic. Be comfortable with the Beginner topics in this area first, then read the common pitfall carefully, since it is where most mistakes happen.
How long does it take to learn Dijkstra's Algorithm?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Dijkstra's Algorithm?
Topological Sort, Bellman-Ford Algorithm, Depth-First Search are closely connected. The full list is in the related topics section of this page.
What should I study after Dijkstra's Algorithm?
Continue with Bellman-Ford Algorithm, the next topic in Algorithms.

Related topics