Welcome! If you’ve always wanted to learn and understand Dijkstra’s algorithm, then this article is for you. You will see how it works behind the scenes with a step-by-step graphical explanation.

You will learn:

  • Basic Graph Concepts (a quick review).
  • What Dijkstra’s Algorithm is used for.
  • How it works behind the scenes with a step-by-step example.

Let’s begin. 🔅

🔸 Introduction to Graphs

Let’s start with a brief introduction to graphs.

Basic Concepts

Graphs are data structures used to represent “connections” between pairs of elements.

  • These elements are called nodes. They represent real-life objects, persons, or entities.
  • The connections between nodes are called edges.

This is a graphical representation of a graph:

Nodes are represented with colored circles and edges are represented with lines that connect these circles.

💡 Tip: Two nodes are connected if there is an edge between them.


Graphs are directly applicable to real-world scenarios. For example, we could use graphs to model a transportation network where nodes would represent facilities that send or receive products and edges would represent roads or paths that connect them (see below).

Network represented with a graph

Types of Graphs

Graphs can be:

  • Undirected: if for every pair of connected nodes, you can go from one node to the other in both directions.
  • Directed: if for every pair of connected nodes, you can only go from one node to another in a specific direction. We use arrows instead of simple lines to represent directed edges.

💡 Tip: in this article, we will work with undirected graphs.

Weighted Graphs

A weight graph is a graph whose edges have a “weight” or “cost”. The weight of an edge can represent distance, time, or anything that models the “connection” between the pair of nodes it connects.

For example, in the weighted graph below you can see a blue number next to each edge. This number is used to represent the weight of the corresponding edge.

💡 Tip: These weights are essential for Dijkstra’s Algorithm. You will see why in just a moment.

🔹 Introduction to Dijkstra’s Algorithm

Now that you know the basic concepts of graphs, let’s start diving into this amazing algorithm.

  • Purpose and Use Cases
  • History
  • Basics of the Algorithm
  • Requirements

Purpose and Use Cases

With Dijkstra’s Algorithm, you can find the shortest path between nodes in a graph. Particularly, you can find the shortest path from a node (called the “source node”) to all other nodes in the graph, producing a shortest-path tree.

This algorithm is used in GPS devices to find the shortest path between the current location and the destination. It has broad applications in industry, specially in domains that require modeling networks.


This algorithm was created and published by Dr. Edsger W. Dijkstra, a brilliant Dutch computer scientist and software engineer.

In 1959, he published a 3-page article titled “A note on two problems in connexion with graphs” where he explained his new algorithm.

Dr. Edsger Dijkstra at ETH Zurich in 1994 (image by Andreas F. Borchert)

During an interview in 2001, Dr. Dijkstra revealed how and why he designed the algorithm:

What’s the shortest way to travel from Rotterdam to Groningen? It is the algorithm for the shortest path, which I designed in about 20 minutes. One morning I was shopping in Amsterdam with my young fiancée, and tired, we sat down on the café terrace to drink a cup of coffee and I was just thinking about whether I could do this, and I then designed the algorithm for the shortest path. As I said, it was a 20-minute invention. In fact, it was published in 1959, three years later. The publication is still quite nice. One of the reasons that it is so nice was that I designed it without pencil and paper. Without pencil and paper you are almost forced to avoid all avoidable complexities. Eventually that algorithm became, to my great amazement, one of the cornerstones of my fame. — As quoted in the article Edsger W. Dijkstra from An interview with Edsger W. Dijkstra.

Unbelievable, right? In just 20 minutes, Dr. Dijkstra designed one of the most famous algorithms in the history of Computer Science.

Basics of Dijkstra’s Algorithm

  • Dijkstra’s Algorithm basically starts at the node that you choose (the source node) and it analyzes the graph to find the shortest path between that node and all the other nodes in the graph.
  • The algorithm keeps track of the currently known shortest distance from each node to the source node and it updates these values if it finds a shorter path.
  • Once the algorithm has found the shortest path between the source node and another node, that node is marked as “visited” and added to the path.
  • The process continues until all the nodes in the graph have been added to the path. This way, we have a path that connects the source node to all other nodes following the shortest path possible to reach each node.

#data-science #artificial-intelligence #developer

Dijkstra's Shortest Path Algorithm - A Detailed and Visual Introduction
2.10 GEEK