Grokking Algorithms
An illustrated guide for programmers and other curious people
Aditya Y. Bhargava
  • May 2016
  • ISBN 9781617292231
  • 256 pages
  • printed in black & white

This book does the impossible: it makes math fun and easy!

Sander Rossel, COAS Software Systems

Grokking Algorithms is a fully illustrated, friendly guide that teaches you how to apply common algorithms to the practical problems you face every day as a programmer. You'll start with sorting and searching and, as you build up your skills in thinking algorithmically, you'll tackle more complex concerns such as data compression and artificial intelligence. Each carefully presented example includes helpful diagrams and fully annotated code samples in Python.

Learning about algorithms doesn't have to be boring! Get a sneak peek at the fun, illustrated, and friendly examples you'll find in Grokking Algorithms on YouTube

Table of Contents detailed table of contents

1. Introduction To Algorithms

1.1. Introduction

1.1.1. What you’ll learn about performance

1.1.2. What you’ll learn about solving problems

1.2.2. Running time

1.3. Big-O-notation

1.3.1. Algorithm running times grow at different rates

1.3.2. Visualizing dfferent Big O run times

1.3.3. Big O establishes a worst-case runtime

1.3.4. Some common Big O run times

1.3.5. The traveling salesperson

1.4. Recap

2. Selection Sort

2.1. How Memory Works

2.2. Arrays And Linked Lists

2.2.1. Linked Lists

2.2.2. What arrays are good for

2.2.3. Terminology

2.2.4. More insertions and deletions

2.2.5. Deletions

2.3. Selection Sort

2.4. Recap

3. Recursion

3.1. Recursion

3.2. Base Case And Recursive Case

3.3. The Stack

3.3.1. The call stack

3.3.2. The call stack with recursion

3.4. Recap

4. Quicksort

4.1. Divide And Conquer

4.2. Quicksort

4.3. Big O Notation Revisited

4.3.1. Merge sort vs. quicksort

4.3.2. Average case vs. worst case

4.4. Recap

5. Hash Tables

5.1. Hash Functions

5.2. Use Cases

5.2.1. Using hash tables for lookups

5.2.2. Preventing duplicate entries

5.2.3. Using hash tables as a cache

5.2.4. Recap

5.3. Collisions

5.4. Performance

5.4.1. Load factor

5.4.2. A good hash function

5.5. Recap

6. Breadth-first Search

6.1. Introduction To Graphs

6.2. What is a graph?

6.3.1. Finding the shortest path

6.3.2. Queues

6.4. Implementing The Graph

6.5. Implementing The Algorithm

6.5.1. Running time

6.6. Recap

7. Dijkstra’s Algorithm

7.1. Working with Dijkstra’s Algorithm

7.2. Terminology

7.3. Trading For A Piano

7.4. Negative Weight Edges

7.5. Implementation

7.6. Recap

8. Greedy Algorithms

8.1. The Classroom Scheduling Problem

8.2. The Knapsack Problem

8.3. The Set-Covering Problem

8.3.1. Approximation Algorithms

8.4. NP Complete Problems

8.4.1. How do you tell if a problem is NP-Complete?

8.5. Recap

9. Dynamic Programming

9.1. The Knapsack Problem

9.1.1. The simple solution

9.1.2. Dynamic programming

9.2. Knapsack Problem Faq

9.2.1. What happens if we add an item?

9.2.2. What happens if we change the order of the rows?

9.2.3. Can you fill in the grid column-wise instead of row-wise?

9.2.4. What happens if we add a smaller item?

9.2.5. Can you steal fractions of an item?

9.2.6. Optimizing your travel itinerary

9.2.7. Handling items that depend on each other

9.2.8. Is it possible that the solution will require more than 2 sub-knapsacks?

9.2.9. Is it possible that the best solution doesn't fill the knapsack completely?

9.3. Longest Common Substring

9.3.1. Making the grid

9.3.2. Filling in the grid

9.3.3. The solution

9.3.4. Longest common subsequence

9.3.5. Longest common subsequence — solution

9.4. Recap

10. K Nearest Neighbors

10.1. Classifying Oranges Vs Grapefruit

10.2. Building A Recommendations System

10.2.1. Feature Extraction

10.2.2. Regression

10.2.3. Picking good features

10.3. Introduction To Machine Learning

10.3.1. OCR

10.3.2. Building a spam filter

10.3.3. Predicting the stock market

10.4. Recap

11. Where To Go Next

11.1. Trees

11.2. Inverted Indexes

11.3. The Fourier Transform

11.4. Parallel Algorithms

11.5. Map Reduce

11.5.1. Why are distributed algorithms useful?

11.5.2. The "map" function

11.5.3. The "reduce" function

11.6. Bloom Filters And Hyperloglog

11.6.1. Bloom Filters

11.6.2. Hyperloglog

11.7. The Sha Algorithms

11.7.1. Comparing files

11.7.2. Checking passwords

11.8. Locality Sensitive Hashing

11.9. Diffie-hellman Key Exchange

11.10. Linear Programming

11.11. Epilogue

About the Technology

An algorithm is nothing more than a step-by-step procedure for solving a problem. The algorithms you’ll use most often as a programmer have already been discovered, tested, and proven. If you want to understand them but refuse to slog through dense multipage proofs, this is the book for you. This fully illustrated and engaging guide makes it easy to learn how to use the most important algorithms effectively in your own programs.

About the book

Grokking Algorithms is a friendly take on this core computer science topic. In it, you’ll learn how to apply common algorithms to the practical programming problems you face every day. You’ll start with tasks like sorting and searching. As you build up your skills, you’ll tackle more complex problems like data compression and artificial intelligence. Each carefully presented example includes helpful diagrams and fully annotated code samples in Python. By the end of this book, you will have mastered widely applicable algorithms as well as how and when to use them.

What's inside

  • Covers search, sort, and graph algorithms
  • Over 400 pictures with detailed walkthroughs
  • Performance trade-offs between algorithms
  • Python-based code samples

About the reader

This easy-to-read, picture-heavy introduction is suitable for self-taught programmers, engineers, or anyone who wants to brush up on algorithms.

About the author

Aditya Bhargava is a Software Engineer with a dual background in Computer Science and Fine Arts. He blogs on programming at

  • combo $44.99 pBook + eBook
  • eBook $35.99 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks

Do you want to treat yourself to learning algorithms in the same way that you would read your favorite novel? If so, this is the book you need!

Sankar Ramanathan, IBM Analytics

In today’s world, there is no aspect of our lives that isn’t optimized by some algorithm. Let this be the first book you pick up if you want a well-explained introduction to the topic.

Amit Lamba, Tech Overture, LLC

Algorithms are not boring! This book was fun and insightful for both my students and me.

Christopher Haupt, Mobirobo, Inc