liveBook · Manning

Chapter 10: Greedy algorithms

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

The chapter introduction previews greedy strategies, NP-hard problems, and approximation algorithms.

After this chapter you will be able to

Section by section

Introduction

The chapter introduction previews greedy strategies, NP-hard problems, and approximation algorithms.

10.1 The classroom scheduling problem

The section solves the classroom scheduling problem by repeatedly choosing the compatible class that ends soonest, then uses that example to explain greedy algorithms.

Key terms: classroom scheduling problem, ends the soonest, greedy algorithm, locally optimal solution, globally optimal solution

10.2 The knapsack problem

This section uses a knapsack example to show that a simple greedy choice can miss the most valuable combination, while still being useful when a good-enough answer will do.

Key terms: greedy strategy, knapsack, optimal solution, greedy algorithms, good enough

10.3 The set-covering problem

The section defines the set-covering problem through a radio-station example and shows why finding the smallest exact solution becomes difficult as the number of stations grows.

Key terms: set-covering problem, stations, states, power set, subset, O(2n)

10.4 Approximation algorithms

The section gives a greedy approximation for covering all states with radio stations and explains how to judge it against an exact solution.

Key terms: greedy algorithm, states, station, approximation algorithm, optimal solution, O(n2)

10.5 Code for setup

The section sets up the data structures for a station-selection example and explains why sets are useful for representing states.

Key terms: set, dict, states, stations, list, duplicates

10.6 Calculating the answer

This section explains how to choose the station that covers the most states not yet covered.

Key terms: best_station, states_covered, uncovered states, for loop

10.7 Sets

This section introduces sets as duplicate-free collections and explains union, intersection, and difference.

Key terms: set union, set intersection, set difference, sets, lists, duplicates

10.8 Back to the code

Explains how the greedy set-covering loop chooses stations and updates uncovered states, then notes its speed-versus-exactness tradeoff.

Key terms: covered, states_needed, states_for_station, best_station, final_stations, greedy algorithm, exact answer, set-covering problem, NP-hard

10.9 Recap

This recap connects greedy algorithms’ local choices with their usefulness as fast, easy-to-write approximation algorithms for NP-hard problems.

Key terms: greedy algorithms, locally, global optimum, NP-hard problem, approximation algorithm

Where this is used later

Concepts explained in this chapter

← Chapter 9: Dijkstra’s algorithm Chapter 11: Dynamic programming →

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.