Index / 001 EN
Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People cover

Computer Science

Computer Science

Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People

Aditya Y. Bhargava

An illustrated introduction to algorithms and the data structures they rely on, written for readers who can program but have no formal computer science background. Concepts are explained through drawings and small worked examples in Python, covering searching, sorting, recursion, hash tables, graphs, trees and common problem-solving strategies.

Prerequisites: Basic programming experience in any language and school-level algebra; the Python examples are easy to follow without prior Python knowledge.

Difficulty Level
Beginner
Academic Level
Undergraduate
Algorithmsdata structuresrecursiongraphspythonTextbook

01 / Reading Guide

Reading Guide

Citation

Bhargava, A. Y. (2024). Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People (2nd ed.). Manning Publications.

Chapter Summary

The second edition is a 320-page book in thirteen chapters, with a foreword by Daniel Zingaro, a preface and an "about this book" section on how to use the book, who should read it and how it is organized. The publisher describes over 400 illustrations, exercises and code samples in every chapter, code updated to Python 3, new coverage of trees (binary search trees, balanced trees, B-trees) and of NP-complete problems, and notes on data-structure performance on modern CPUs. Each chapter opens with a short list of what the reader will learn, and chapters close with a recap.

Chapter 1: Introduction to algorithms

The foundation: binary search as a first algorithm, running time and Big O notation, how running times grow at different rates, Big O as a worst-case measure, and a first look at the travelling salesperson problem as an example of a very slow running time.

Chapter 2: Selection sort

Starts with how memory works, then compares arrays and linked lists for reading, inserting into the middle and deleting, before introducing selection sort. The chapter states that it assumes Big O and logarithms from Chapter 1.

Chapter 3: Recursion

Base case and recursive case, the stack and the call stack, and how the call stack behaves with recursion. The author urges readers to run the code and to trace at least one recursive function by hand with pen and paper; the chapter also introduces pseudocode.

Chapter 4: Quicksort

Divide and conquer as a general problem-solving strategy, then quicksort as its first major example, Big O revisited, merge sort compared with quicksort, and the average case against the worst case.

Chapter 5: Hash tables

Hash functions and the common uses of hash tables (lookups, preventing duplicate entries, caching), followed by the internals that determine performance: collisions, performance and load factor.

Chapter 6: Breadth-first search

An introduction to graphs as a way of modelling networks, breadth-first search for finding the shortest path in terms of number of steps, queues, directed versus undirected graphs, and topological sort.

Chapters 7–8: Trees; Balanced trees

New in this edition. Chapter 7 defines rooted trees and binary trees using file directories, introduces depth-first search and contrasts it with breadth-first search, and covers Huffman coding as a compression algorithm built on trees. Chapter 8 introduces binary search trees and why balance matters, AVL trees and rotations, splay trees, and B-trees and their advantages.

Chapter 9: Dijkstra's algorithm

Weighted graphs and shortest paths when edges have travel times, worked through a "trading for a piano" example, with negative-weight edges (where Dijkstra's algorithm fails) and an implementation.

Chapter 10: Greedy algorithms

The greedy strategy (classroom scheduling), the knapsack and set-covering problems, approximation algorithms, and how to recognize NP-hard problems that have no known fast exact solution.

Chapter 11: Dynamic programming

Revisits the knapsack problem to show how dynamic programming builds a solution from subproblems in a grid, with a set of questions on how the grid changes, and shows through examples how to devise a dynamic programming solution to a new problem.

Chapter 12: k-nearest neighbors

A first taste of machine learning: classification with k-nearest neighbors, a recommendation system, feature extraction, regression, and the uses and limitations of the method.

Chapter 13: Where to go next

Brief overviews of ten algorithms not covered in the book, including linear regression, inverted indexes, the Fourier transform, parallel algorithms and MapReduce, with pointers for further reading.

Key Concepts

Chapters 1–4: Running time, sorting and recursion

  • Big O notation: how an algorithm's running time grows with input size, quoted for the worst case.
  • Binary search: repeatedly halving a sorted list, O(log n) instead of O(n).
  • Arrays versus linked lists: fast reading versus fast insertion and deletion, tied to how memory works.
  • Recursion and the call stack: base case, recursive case and the stack of pending calls.
  • Divide and conquer: reducing a problem to smaller instances, as in quicksort.

Chapters 5–9: Hash tables, graphs and trees

  • Hash tables: constant-time lookup on average, with collisions and load factor governing performance.
  • Breadth-first search: shortest path in an unweighted graph using a queue.
  • Depth-first search and Huffman coding: tree traversal and a tree-based compression algorithm.
  • Balanced trees: AVL trees, splay trees and B-trees, which keep search fast as data changes.
  • Dijkstra's algorithm: shortest paths in graphs with non-negative edge weights.

Chapters 10–13: Hard problems and beyond

  • Greedy algorithms and approximation: locally optimal choices that give good, fast answers to NP-hard problems.
  • Dynamic programming: solving subproblems first and building up to the full problem.
  • k-nearest neighbors: classification and regression by similarity of features.

Critical Analysis

Strengths

  • Built around pictures and examples. The publisher cites over 400 illustrations, and the public chapter openings show the style: each idea starts from a concrete scene (a locked suitcase in an attic for recursion, a grocery store for hash tables, a bus route across San Francisco for breadth-first search) before the algorithm is stated.
  • Wide reach for a short book. In 320 pages it runs from binary search to NP-hard problems, approximation algorithms, dynamic programming and k-nearest neighbors, and the final chapter points to further topics.
  • Updated second edition. The new chapters on trees and balanced trees fill a gap in the first edition, and the code is in Python 3, with source code on GitHub.
  • Encourages active reading. The recursion chapter, for example, asks readers to run the code and trace a recursive function by hand, and the publisher lists exercises in every chapter.

Limitations

  • Intuition rather than proof. The publisher's "no advanced math required" approach means correctness and running times are explained by example rather than derived; students in a university course will still need a textbook with proofs.
  • Brief treatment of each topic. Thirteen chapters in 320 heavily illustrated pages leave limited room per algorithm; Chapter 13 explicitly only surveys its ten topics.
  • Python only. All code is in Python, so readers working in other languages must translate it themselves.

Where It Fits in the Course

This is the course's most visual entry point, and it pairs with Wengrow's A Common-Sense Guide to Data Structures and Algorithms. Wengrow spends more time on counting steps and analysing everyday code and provides solutions to every exercise; Bhargava covers more algorithmic ground, including balanced trees, Dijkstra's algorithm, NP-hard problems and k-nearest neighbors, with lighter analysis. A reader who finds one book's explanation hard can switch to the other.

After these two, Goodrich, Tamassia and Goldwasser's Data Structures and Algorithms in Java and Pat Morin's free Open Data Structures give the full university treatment with implementations and proofs. Peter Brass's Advanced Data Structures and Chris Okasaki's Purely Functional Data Structures are specialist books for much later. The greedy and dynamic programming chapters also make a gentle preview for an algorithms course.

How to Use This Book

The book suits readers who can program a little, in any language, and want an intuitive introduction to algorithms: self-taught programmers, students before or during a first data structures course, and people preparing for technical interviews. Python experience helps but the examples are short.

A practical plan:

  • Core: Chapters 1–6 in order. Each builds on the previous one: Big O, arrays and lists, recursion, divide and conquer, hash tables, then graphs.
  • Trees and graphs: Chapters 7–9. Chapter 8's balanced trees can be read lightly on a first pass, but Chapter 9 completes the shortest-path story begun in Chapter 6.
  • Problem-solving strategies: Chapters 10–11, best read together because Chapter 11 revisits the knapsack problem from Chapter 10.
  • Optional: Chapter 12 for readers interested in machine learning, and Chapter 13 as a reading list.

Run the Python code from the GitHub repository and change the inputs, and trace recursive functions and graph searches by hand as the book suggests. Do each chapter's exercises before moving on. For a course, follow each block with the matching chapter of a formal textbook to see the proofs and analysis this book leaves out.