Chapters

Hide chapters

Data Structures & Algorithms in Swift

Fifth Edition · iOS 18 · Swift 6.0 · Xcode 16.2

42. Dijkstra’s Algorithm
Written by Vincent Ngo

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Have you ever used the Google or Apple Maps app to find the shortest distance or fastest time from one place to another? Dijkstra’s algorithm is particularly useful in GPS networks to help find the shortest path between two places.

Dijkstra’s algorithm is a greedy algorithm. A greedy algorithm constructs a solution step-by-step, and it picks the most optimal path at every step in isolation. It misses solutions where some steps might cost more, but the overall cost is lower. Nevertheless, it usually arrives at a pretty good solution very quickly.

Dijkstra’s algorithm finds the shortest paths between vertices in either directed or undirected graphs. Given a vertex in a graph, the algorithm will find all shortest paths from the starting vertex.

Some other applications of Dijkstra’s algorithm include:

  1. Communicable disease transmission: Discover where biological diseases are spreading the fastest.
  2. Telephone networks: Routing calls to highest-bandwidth paths available in the network.
  3. Mapping: Finding the shortest and fastest paths for travelers.

Example

All the graphs you have looked at thus far have been undirected. Let’s change it up a little and work with a directed graph! Imagine the directed graph below represents a GPS network:

The vertices represent physical locations, and the edges represent one-way paths of a given cost between locations.

3 9 2 8 2 1 1 3 1 8 5 2 3 1 H G C E B F A D

In Dijkstra’s algorithm, you first choose a starting vertex since the algorithm needs a starting point to find a path to the rest of the nodes in the graph. Assume the starting vertex you pick is vertex A.

First pass

3 9 2 8 2 1 1 3 1 8 5 2 3 1 A H F B D E C G 8 A 9 A 1 A nil nil nil nil B C D E F G H Start A

Second pass

8 A 9 A 1 A nil nil nil nil B C D E F G H Start A

3 1 6 4 9 1 6 8 3 9 3 1 3 8 U K T K F U C R Bfiwp O 7 O 8 A qiw mot gud kel 1 U 6 O bun veb fel 2 P 0 U 3 A Q W L L O L W M

Third pass

Start A 8 A 9 A nil nil nil nil 8 A 9 A nil nil nil 4 G 1 A 1 A B G C D E F G H

3 2 6 7 9 6 8 2 9 2 0 2 5 2 I M B Y F O F Jxony U 6 A 1 I kef qac jih nah 1 I 0 E rin heb qaj 6 Q 6 N 8 A 0 O 8 R 9 U 1 Y guz zer 6 O Y L W G S U L G S S

Fourth pass

Start A 8 A 9 A nil nil nil nil 8 A 9 A nil nil nil 4 G 4 G 1 A 1 A 7 C 9 A 5 C nil nil 1 A B G C C D E F G H

5 2 2 2 8 3 4 3 3 6 1 1 1 3 A X S F F Q Rhuqx U 3 O 4 U mux goh gax hep 8 A 9 A leb wej tat 3 W 4 S 9 A 1 I 0 C 7 E vax yaz 7 I 6 U 1 I 8 E fuc 5 S 8 A 7 C 0 Y T C R I D H U F Z P C I

Fifth pass

Start A 8 A 9 A nil nil nil nil 8 A 9 A nil nil nil 4 G 4 G 1 A 1 A 7 C 9 A nil nil 1 A 6 E 9 A 7 E nil 4 G 1 A 5 C 5 C B G C E C D E F G H

Mhibv U 8 I 5 O huk diw tav bib 8 O 9 O hir gos qed 4 Z 6 H 1 A 2 A 9 H 3 A yuq fey 7 O 4 U 1 U 0 O fav 1 S 0 I 3 G 9 E 8 O qak 8 E 2 R 9 E 4 K 6 X T X W I L N N I G J L 4 6 5 4 5 1 6 4 5 2 3 1 3 6 O Q T K B X K I

Sixth pass

Start A 8 A 9 A nil nil nil nil 8 A 9 A nil nil nil 4 G 4 G 1 A 1 A 7 C 9 A nil nil 1 A 6 E 9 A 7 E nil 4 G 1 A 5 C 9 A 7 E nil 6 E 4 G 1 A 5 C 5 C B G C E B C D E F G H

9 9 6 3 5 9 2 1 0 1 3 4 7 5 U G H M W A F T Shahx O 5 U 0 O hah paf qek jat 7 U 8 O ruh par vep 2 L 7 J 7 A 9 A 8 J 6 O 6 E 3 E ziw wov mah 0 U 3 I 1 O 8 E 1 I 4 A 0 E taf 8 N 8 K 0 G 8 I 9 U 2 I 9 U 9 L 7 R 1 X 1 S Y B Q E S H G X A J R S ces

Seventh pass

Start A 8 A 9 A nil nil nil nil 8 A 9 A nil nil nil 4 G 4 G 1 A 1 A 7 C 9 A 7 E 9 A nil nil nil 1 A 6 E 6 E 6 E 9 A 9 A 7 E nil 4 G 4 G 4 G 7 E 1 A 1 A 1 A 5 C 5 C 5 C 5 C B G C E B D C D E F G H nil

Vnahs I 4 O 7 I tet wur tiz wis 3 U 9 O gus wub taz 5 M 6 F 4 O 8 U 3 Q 0 U 3 I 3 I veg paf ram zab 7 O 8 O 5 E 2 A 6 O 0 O 7 U 8 O liv 2 B 4 T 5 T 7 R 5 E 7 I 4 O 1 A 9 O 6 U 8 N 3 N 9 S 6 T 4 A 6 T T R L I N J Y M G E Z F N 9 0 2 6 3 1 6 7 1 3 9 5 5 0 A D Q Z U N B D hox

Eighth pass

You have covered every vertex except for H. H has two outgoing edges to G and F. However, there is no path from A to H. Because there is no path, the whole column for H is nil.

Kyekd O 7 E 1 A fep sic lok lub 4 A 2 O teq toy ned 4 B 5 H 6 U 8 E 3 N 9 O 4 A 1 I bef gej dit fat 6 E 3 I 4 A 9 E 7 O 3 I 4 A 6 E qot 8 P 0 C 0 J 0 B 4 E 8 U 8 I 4 I 2 E 0 E 3 W 7 B 6 L 3 X 2 O 1 F S L L O Y H F B Z E Y S X 3 1 2 3 2 7 4 1 0 2 2 8 4 9 A Q P C U R P K qub

Ssemj A 2 U 8 W 2 E 2 O 1 E hut nut mob tig fil ruz gim sob 0 O 2 O bot vaz sij 7 T 8 D 2 Z 9 O 2 I 8 O 9 I reg jaf 4 B 9 X 6 H 9 A 9 E 9 E 2 L 2 D 6 R 1 U 9 O 1 I 2 O 5 I 4 A 3 O 1 O 9 E 9 E 5 R 8 O 4 L G F J O F Y Z H G I Q L Y 4 2 6 3 2 7 8 7 7 7 2 9 9 8 F G O U L X S W

Implementation

Open up the starter playground for this chapter. This playground comes with an adjacency list graph and a priority queue, which you will use to implement Dijkstra’s algorithm.

Helper methods

Before building Dijkstra, let’s create some helper methods that will help create the algorithm.

Tracing back to the start

C to G to A G 3 9 2 8 2 1 1 3 1 8 5 2 3 1 H C E D B A F

Calculating total distance

Total distance = 4 A G 3 1 C

Generating the shortest paths

After the distance method, add the following:

Finding a specific path

Add the following method to class Dijkstra:

Trying out your code

3 9 2 8 2 1 1 3 1 8 5 2 3 1 H G C E B F A D

G 0 4 8 6 8 1 0 5 8 3 6 6 0 9 Q S I J Q E H

Performance

In Dijkstra’s algorithm, you constructed your graph using an adjacency list. You used a min-priority queue to store vertices and extract the vertex with the minimum path. This process has an overall time complexity of O(log V). The heap operations of extracting the minimum element or inserting an element both take O(log V) respectively.

Key points

  • Dijkstra’s algorithm finds a path to the rest of the nodes given a starting vertex.
  • This algorithm is useful for finding the shortest paths between different endpoints.
  • Visit state is used to track the edges back to the start vertex.
  • The priority queue data structure ensures returning the vertex with the shortest path.
  • Because it chooses the shortest path at each step, it is said to be greedy!
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now