liveBook · Manning

Chapter 9: Dijkstra’s algorithm

What you will learn in chapter 9 of Grokking Algorithms, Second Edition

This introduction contrasts finding the path with the fewest segments with finding the fastest path when graph edges have travel times.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction contrasts finding the path with the fewest segments with finding the fastest path when graph edges have travel times.

Key terms: breadth-first search, Dijkstra’s algorithm, weighted graphs, shortest path

9.1 Working with Dijkstra’s algorithm

This section walks through Dijkstra’s algorithm on a travel-time graph and contrasts its lowest-total-time path with breadth-first search’s fewest-segments path.

Key terms: cheapest node, out-neighbors, costs, breadth-first search, weight, smallest total weight, shortest path

9.2 Terminology

Defines weighted graphs and cycles, then states when the section recommends breadth-first search, Dijkstra’s algorithm, or Bellman–Ford for shortest paths.

Key terms: weights, weighted graph, unweighted graph, breadth-first search, Dijkstra’s algorithm, cycle, total weight, shortest path, undirected graph, nonnegative

9.3 Trading for a piano

The section uses a series of trades to show how Dijkstra’s algorithm finds a minimum-cost path and how parent links reveal the trades on that path.

Key terms: nodes, weights, edges, shortest path, cheapest node, cost, out-neighbors, parent, path, drums

9.4 Negative-weight edges

The section uses a trade with a negative cost to show why Dijkstra’s algorithm can return the wrong shortest path.

Key terms: negative weight, edge, Dijkstra’s algorithm, negative-weight edges, processed node, costs

9.5 Implementation

This section shows how to represent a weighted graph and track costs, parents, and processed nodes while implementing Dijkstra’s algorithm.

Key terms: graph, out-neighbors, cost, hash table, costs, parents, processed, infinity, lowest cost, parent

9.6 Recap

The recap distinguishes which shortest-path algorithm to use for unweighted graphs, nonnegatively weighted graphs, and graphs with negative weights.

Key terms: breadth-first search, unweighted graph, Dijkstra’s algorithm, weighted graph, nonnegative, negative weights

Where this is used later

Concepts explained in this chapter

← Chapter 8: Balanced trees Chapter 10: Greedy algorithms →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.