liveBook · Manning

Chapter 6: Breadth-first search

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

The chapter introduction presents breadth-first search as a way to find shortest distances and shows that “shortest” can mean different things in different applications.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction presents breadth-first search as a way to find shortest distances and shows that “shortest” can mean different things in different applications.

Key terms: breadth-first search, shortest distance

6.1 Introduction to graphs

The section uses a bus-route example to introduce shortest-path problems and the high-level role of graphs and breadth-first search in solving them.

Key terms: shortest-path problem, graph, breadth-first search

6.2 What is a graph?

This section introduces graphs as models of connections and explains nodes, edges, and direct in-neighbor and out-neighbor relationships using poker debts.

Key terms: graph, nodes, edges, in-neighbor, out-neighbor, directly connected

6.3 Breadth-first search

The section introduces breadth-first search as a graph search and uses a mango-seller example to show how it explores a network.

Key terms: breadth-first search, path, shortest path, node, list, friends, friends’ friends, mango seller

6.4 Finding the shortest path

This section explains why breadth-first search finds the closest mango seller and why checking people in the order they were added is necessary.

Key terms: breadth-first search, shortest path, first-degree connections, second-degree connections, search list, queue

6.5 Queues

Explains how a FIFO queue determines the order in which breadth-first search examines people on its search list.

Key terms: queue, enqueue, dequeue, FIFO, LIFO, stack

6.6 Implementing the graph

Shows how to represent a graph in Python with a hash table and distinguishes directed from undirected neighbor relationships.

Key terms: graph, node, out-neighbors, hash table, hash tables, key/value pairs, directed graph, undirected graph, in-neighbor, out-neighbor

6.7 Implementing the algorithm

This section shows how breadth-first search uses a queue to check people for a mango seller and a searched set to avoid repeated checks and loops.

Key terms: search queue, out-neighbors, person_is_seller, mango seller, searched, set

6.8 Running time

This section derives breadth-first search’s O(V+E) running time and introduces topological sorting and trees through graph exercises.

Key terms: breadth-first search, O(V+E), vertices, edges, queue, topological sort, depends on, tree, graph, nodes

6.9 Recap

Recaps how breadth-first search finds paths in graphs, why a queue preserves shortest-path results, and why checked nodes must not be revisited.

Key terms: breadth-first search, shortest path, graph, directed graph, undirected graph, queue, FIFO, stack, LIFO, search list

Where this is used later

Concepts explained in this chapter

← Chapter 5: Hash tables Chapter 7: Trees →

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.