A great book that makes algorithms accessible

Bob, Tue 03 January 2017, Books

algorithms, collections, data structures, performance

Grokking algorithms is a unique gem. I discovered it on episode 82 of Talk Python. Knowing algorithms is fundamental for programming and problem solving. This book presents the key algorithms in an accessible way using great examples and hundreds of illustrations with code samples in Python. Specially for self-taught programmers (like myself) this approach is awesome. After reading it I noted that I more easily grasp related topics in other books and I am now more confident picking up more advanced algorithms (which will be needed when learning ML).

Visual learning

This is a great summarizing video about some basic algorithms and the way the book teaches them:

Performance

The examples in the book are easy to follow. For example to explain the performance between an array (Python's list) and linked list (Python's deque) we are taken to the movies. What if you are 5 and a 6th friend joined? Possibly you have to relocate all 6 to find new seats if you are an array. Not so with a linked list, because the new friend can just sit 'anywhere' ( = linked to). This visualization stayed with me and I much better understand why inserts on arrays are slower.

And it does matter when your data set grows. Expert Python provides a nice snippet in chapter 12 that shows the performance of array (list) vs linked list (deque):

$ python3 -m timeit \
> -s 'sequence=list(range(10000))' \
> 'sequence.insert(0, 0); sequence.pop(0)'
100000 loops, best of 3: 9.12 usec per loop

$ python3 -m timeit \
> -s 'from collections import deque; 
> sequence=deque(range(10000))' \
> 'sequence.appendleft(0); sequence.popleft()'
10000000 loops, best of 3: 0.204 usec per loop

Another good example is binary search. Compared to selection sort the number of steps needed to search a (sorted) list goes from 4 billion down to 32. That demonstrates an important concept of Big O, quoting the book: "algorithm times are measured in terms of growth of an algorithm." Fascinating!

See Python's TimeComplexity wiki for performance details on all stdlib collections.

Cool use cases

Some cool stuff you can do with basic algorithms:

Conclusion

The explanation of Big O, array vs list and hash tables (meet Maggie!) are worth the price alone, but there is much more. If you are new to algorithms or need a refresher this is un unmissable book.


Keep Calm and Code in Python!

-- Bob

PyBites Python Tips

Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.

Get our Python Tips Book

"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H

"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B

"This is now my favourite first Python go-to reference." - Anthony L

"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S

Get the book