Chapters

Hide chapters

Data Structures & Algorithms in Swift

Fourth Edition · iOS 15 · Swift 5.5 · Xcode 13

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

0 0 2 0 0 0 8 5 8 2 8 6 5 3 I W V W F I M X Fvibf I 6 I 9 O nac fuh qel sek 9 E 1 A yov gud cih 0 K 2 E 5 U P R G C U Q R S

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

1 7 2 7 5 6 4 7 5 2 9 0 5 4 U N J R L I T Hkusg U 5 I 8 I fec kuz lig qik 5 A 2 E caq luj yiw 3 Z 4 V 5 A 0 O 8 M 2 O 8 Q veg joz 6 U S T Y Q S O B H S Q

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

9 6 9 2 5 8 3 0 0 1 2 0 7 8 I F V C L S Qjanm A 9 A 6 O xuk nim lah bik 9 E 7 I cav yuk xag 0 T 8 B 9 I 3 I 9 K 2 O qoz xip 1 E 3 I 5 O 4 E sih 1 L 8 I 2 V 5 F L G C I G L E W G T G E

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

Cqifk A 6 O 7 I hiz fix hoq nuj 4 I 6 U zeq hen lan 0 X 9 H 0 A 9 I 2 D 6 O xoh dok 4 I 7 A 8 I 8 O tif 5 R 0 A 5 G 5 O 7 U keh 9 A 2 W 3 A 7 G 2 F B Q V O F P C I F Z X 8 8 1 4 7 5 5 7 6 9 4 6 0 9 I B Z N C Q V U

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

0 7 8 0 2 9 0 1 5 9 2 9 1 3 E N K C L I Y F Pjumj E 1 U 4 U rop nuc jih koq 3 U 4 U xig xak xak 6 J 3 F 7 U 4 U 8 K 8 I 4 E 4 O wud lon rur 2 O 7 U 3 O 9 O 5 E 8 I 7 O hil 7 D 6 S 4 L 1 O 0 I 2 O 2 A 7 F 6 K 4 X 2 Y G B T I V Z D X A T Q F fen

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

Jkacg O 7 U 0 O yas vav led pix 0 E 7 A kex xew rom 3 N 3 X 0 A 8 A 2 R 9 A 9 O 2 U noh lat raf zip 1 U 9 I 6 O 7 I 4 U 8 I 6 O 4 O vaw 2 Y 6 B 7 X 5 F 3 A 3 U 3 E 9 O 0 U 9 I 9 T 1 B 6 B 8 S 8 U 5 L S Q G O S K Y T N I L M T 9 2 4 1 6 9 3 3 0 4 9 7 0 9 O J G B A R R P cud

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.

Wnifd O 8 I 9 E fik goq ken har 6 O 7 E tos mis pug 6 Q 1 V 7 E 9 I 7 P 6 A 5 E 5 I rih jed put bac 0 U 3 E 3 I 5 O 5 U 8 U 8 O 7 O yob 3 W 4 Q 2 Z 4 X 7 O 5 U 5 U 1 E 7 E 6 U 2 W 9 P 5 S 4 T 4 U 1 K Q R P O G V F H R I B K K 0 9 5 5 5 1 4 6 8 1 3 8 8 3 I K Q F O C H M sid

Hhamn U 8 E 8 R 4 O 6 A 9 O fob dak nay zek vuh xeg jop tis 6 A 3 E diz nul dij 5 D 2 P 2 T 1 O 4 U 8 O 1 I kuv gil 9 W 1 Z 6 D 1 E 6 I 4 I 0 W 0 P 3 J 3 E 5 I 6 A 8 I 7 O 5 U 5 I 1 O 9 U 5 I 1 S 5 A 6 J H M V A L L S P D O C Y M 8 9 1 5 6 1 4 3 9 9 6 1 9 3 X C A E P P N K

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

C 8 8 3 6 4 1 8 1 9 8 0 7 3 7 S P O P R A P

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