Dijkstra algorithm

Edsger W. Dijkstra conceived his algorithm to solve the shortest path for graphs between 1956-1959.

His algorithm finds the shortest path between two nodes, but other variants exist to find the shortest paths between an origin and all other nodes; this is called a shortest path tree. Let's see how it works, and then we will implement it in Swift. We are going to explain it with the following example graph. We want the shortest path between node A and node E:

Dijkstra algorithm

Shortest path example

The steps are as follows:

  1. The algorithm starts by marking the first node as the current node. It puts all the nodes as unvisited inside a set. It also ...

Get Swift Data Structure and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.