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:
Let’s begin. 🔅
Let’s start with a brief introduction to graphs.
Graphs are data structures used to represent “connections” between pairs of elements.
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
Graphs can be:
💡 Tip: in this article, we will work with undirected 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.
Now that you know the basic concepts of graphs, let’s start diving into this amazing algorithm.
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.
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.
#data-science #artificial-intelligence #developer
Learn and understand Dijkstra's algorithm. 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. 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.