from Hacker News

Ask HN: Looking for a book on algorithms and data structures

by OulaX on 11/14/21, 2:12 PM with 96 comments

I want to learn Algorithms & Data Structures from scratch then move on to doing LeetCode.

But I want a structured guide / textbook that I can follow which also contains exercises.

I have checked the CLRS book, but, it's more of a reference than a book that you can read from cover to cover. For example a basic Stack is explained in a page and a half or two, for me this is not enough, I want a book that can go into the details of each specific DS or Algorirhm.

*TL;DR:* Are there any books better than CLRS for DS & Algos?

  • by ioli on 11/14/21, 3:24 PM

    The Algorithm Design Manual by Steven Skiena

    Amazing book. Very readable. I highly recommend it. The book has a section call "War story" at the end of each chapter in which Skiena shares his real life experience of when the contents from that particular chapter came in handy for him.

    Go through it. You won't regret

    https://www.amazon.com/Algorithm-Design-Manual-Steven-Skiena...

  • by alanfranz on 11/14/21, 4:06 PM

    My personal favourite is Sedgewick & Wayne's _Algorithms_.

    https://algs4.cs.princeton.edu/home/

    You can even find an excellent two-part MOOC on Coursera:

    https://www.coursera.org/learn/algorithms-part1

    https://www.coursera.org/learn/algorithms-part2

    Maybe it's not the "purest" class or book, but it's engaging and it lets you understand how algos work AND how to use them in practice.

  • by dyeje on 11/14/21, 4:58 PM

    If your goal is to be good at LeetCode, then save yourself the time and just go straight to LeetCode. Nobody cares if you actually understand the stuff. They just want you to come in and do the dance (recognize the problem archetype, talk through your thought process for solving it, and adjust based on whatever twists they've thrown in).

    If you're interviewing for senior positions, it is worthwhile to read through Designing Data Intensive Systems. This will prepare you for System Design interviews where you do need to actually understand stuff.

  • by hiyer on 11/14/21, 3:10 PM

    Sedgewick's Algorithms [1] is pretty good. I found it way more approachable than CLRS. There's also a free course on Coursera[2] from him.

    1. https://www.amazon.com/Algorithms-4th-Robert-Sedgewick/dp/03...

    2. https://www.coursera.org/learn/algorithms-part1#syllabus

  • by WillDaSilva on 11/14/21, 3:45 PM

    I would recommend Open Data Structures: https://opendatastructures.org/

    It's available for free online, or if you'd like you can buy a physical copy. It's got 14 chapters that take you through a variety of data structures, introduces you to the theory behind them, and teaches you the basics of analyzing them.

    I've done many of the exercises from this book, and found them useful for learning the topic.

  • by stickyricky on 11/14/21, 3:46 PM

    "Algorithms Illuminated" by Roughgarden. Very accessible. Reads like a novel not a textbook. Has exercises throughout the chapter to test knowledge. I recommend it over your suggestion that you want a textbook. This book will serve you much better.
  • by e9 on 11/14/21, 3:44 PM

    Back when I was in high school I learned all leetcode algorithms from doing programming competitions. One of the books I used was: http://acm.cs.buap.mx/downloads/Programming_Challenges.pdf

    What really put me to the next level is this amazing free training program which I still recommend anyone who wants to nail the interviews. It teaches each algorithm and data structure and gives you appropriate challenging exercises so you really understand them: https://train.usaco.org/

  • by usrme on 11/14/21, 3:28 PM

    "A Common-Sense Guide to Data Structures and Algorithms, 2nd Edition" by Jay Wengrow is one I'd recommend every time. It really makes it easy enough to understand things I previously only considered to be the domain of math-oriented people. Not sure if it goes deep enough for you though...
  • by lr1970 on 11/14/21, 5:49 PM

    A great and quite unconventional book on Algorithms is "INTRODUCTION TO ALGORITHMS - A Creative Approach" by Udi Manber. After leaving academia the author was hired first by Amazon (A9.com) and then by Google. At some point around 2010 he was responsible for all search products at Google [0].

    The book not only describes the algorithms per se, but also teaches how to think about the algorithms and the problems they are solving. I enjoyed it very much as a complimentary reading to the classic Knuth volumes.

    [0] https://en.wikipedia.org/wiki/Udi_Manber

  • by bXVsbGVy on 11/14/21, 3:07 PM

    > I want a book that can go into the details of each specific DS or Algorirhm.

    I'm not aware of any book like that. At the uni, the process is repetitive and incremental.

    First you learn how to implement the algo, then you learn how to count (discrete math) and, finally, you learn how to analyze its complexity. Concurrently, you might see some more details on how those data structure are used in Operating System and learn how to implement them in some lecture about parallel computing.

    MIT has a great resource online. They have recorded lecture, exercises, lecture notes with reading suggestion, etc.

    Their intro to algo. https://ocw.mit.edu/courses/electrical-engineering-and-compu...

    Also, see what it the reading for similar lectures on other universities.

  • by thesuperbigfrog on 11/14/21, 2:56 PM

    "Foundations of Computer Science" by Aho and Ullman is an excellent book and freely available:

    http://i.stanford.edu/~ullman/focs.html

  • by curtainvase on 11/14/21, 3:08 PM

    I read CLRS in the fashion you described, mostly cover to cover, as a textbook with exercises. I found it extremely helpful. It gives both intuitive and mathematical proofs for each of the algorithms and data structures.
  • by MyAnonymousAcc on 11/14/21, 4:01 PM

    In case you want to go for something aimed at competitive programming have a look at the "Competitive Programmer’s Handbook" https://cses.fi/book/book.pdf
  • by rramadass on 11/15/21, 11:16 AM

    There are literally hundreds of threads with this topic on the Web. Most of them contain the same standard references which IMO are not always the best for people without much mathematical maturity and wishing to learn from first principles. That said, here are my recommendations for them (pdfs of almost all are available online and they can also be purchased cheaply as used books);

    * 1) Functions and Graphs and 2) The Method of Coordinates by I.M.Gelfand et. al. - These give you the basics on functions and how to visualize them which is crucial for understanding.

    * Introductory Logic and Sets for Computer Scientists by Nimal Nissanke - Nice succinct introduction to various required topics for Comp. Sci.

    * Compared To What?: Introduction to the Analysis of Algorithms by Gregory Rawlins - This is a classic book which takes you by hand and walks you through Algorithm Analysis and the reqd. mathematics. You will find this more approachable than most other text books and well worth a study.

    * How to Think about Algorithms by Jeff Edmonds - Practical techniques without too much mathematical formalism.

    * Data Structure Techniques by Thomas Standish - A Classic text; good adjunct to more modern texts.

    * Advanced Data Structures by Peter Brass - Nice succinct text but not necessarily "advanced".

    One final point; try to get a Algorithms and Data Structures book with code in the language with which you are most familiar. That way you understand the mapping from "Abstract Algorithm" to "Concrete Language Implementation" which is not always trivial.

  • by Someone on 11/14/21, 3:36 PM

    If you want basics and details, the art of computer programming is a good candidate.

    Unfortunately, it isn’t done yet (1), and the completed parts have are very basic and have parts that are a bit dated (multi-tape sorting, for example)

    (1) and likely never will be. Knuth is turning 84 in two months, and isn’t even halfway writing it, after almost 60 years

  • by dhanna on 11/14/21, 4:26 PM

    Yes there’s lots of algorithm books! Too many to learn them all. I had an anxiety attack and bought most of the introductory algo books mentioned in CLRS chapter 1.

    I liked ALGS4 by Sedgewick as my introduction to Algorithms. I still refer to the Coursera course. I actually started with CLRS and doubled back to ALGS4. I wish I did it in reverse order but it was still useful reading CLRS and refreshing my math skills.

    I would say grab a few and see which ones you like. CLRS, ALGS4, Algorithm Design Manual, and Algorithm Design by Kleinberg and Tardos are a good start. You can look in the bibliography of each books for more suggestions from the experts themselves.

  • by chris_l on 11/14/21, 3:57 PM

    Shameless plug: you might want to accompany a book with explained animations of Algorithms and Data Structures: https://www.chrislaux.com
  • by aabaker99 on 11/14/21, 4:59 PM

    I find the writing style in CLRS to be quite readble. Sure the book is huge but so maybe pick and choose chapters but each chapter can be read "cover to cover". Perhaps some of the stack details you are looking for are left to exercises in CLRS? Most good "textbooks" will do this. Perhaps you don't want a general textbook on data structures but want a deep dive article on stacks.

    I admit I haven't explored many other books on this topic but CLRS is very good in my opinion.

  • by jstx1 on 11/14/21, 2:29 PM

    > For example a basic Stack is explained in a page and a half or two, for me this is not enough

    Why not? It's a simple data structure. What more do you think can be said about it?

  • by annoying_code on 11/16/21, 7:53 AM

    I better recommend you to try your hands on code-forces, CP Algorithm etc. If you got stuck in any of the problems just read the discussion and given solution. If you continue this you several months then you quickly learning the data structures and algorithms. Because if you don't apply DSA which you will learn from any book then i guess it doesn't add much value to you. In the beginning problems might not able to solve but after giving time and looking solution you will start getting things. And I guess its just a myth or we can say a phobia that you need to know each and every piece of mathematics.

    And I think graph is a data structure that you should be work upon. For this you can refer this book Introduction to Graph Theory Subsequent Edition by Douglas Brent West. This book you can just read and practice while solving problems on above coding problem platforms.

    So, I recommend you first do code-forces DIV3, DIV2 problems, gain some confidence, and then pick any DSA book.

    Or one thing you can also do just filter out problems related to math,Combinatorics etc.on code-forces and try to solve them. Through this approach you will learn to apply the concepts.

    I hope this comment add some value to you! Happy coding.

  • by issirsq3 on 11/19/21, 9:40 AM

  • by tarsinge on 11/14/21, 8:11 PM

    I recommend to start with "Elements of Programming Interviews (in Java or Python)". It serves as a nice practical overview of the main algorithms and data structures and when to use them. Personally I found it more complete than the usually recommended "Cracking the Coding Interview" while being equally approachable.
  • by zamkam on 11/19/21, 2:26 PM

    Truth is, if you want to really learn algorithms and data structures, then you are the one who has to "go into the details". CLRS is the bible, I can't think of a better book, and you don't have to read it sequentially because most chapters are independent of each other.
  • by truly on 11/14/21, 9:22 PM

    It really depends. It is a bit difficult to answer this question without understanding your background first. What CS lectures have you already taken? What related courses (e.g., math) have you done?

    For example, the Stack data structure that you bring into discussion is quite simple (but interesting and important), so I am not surprised it is treated speedily. Introduction to Algorithms typically has more difficult content.

    Depending on this, I could recommend for example that you follow the lectures on the excellent MIT OCW YouTube channel (https://www.youtube.com/c/mitocw). But you could also do one of the numerous algorithms and data structures courses on coursera/edx. It all depends on what you know so far and what your goal is.

  • by stefanos82 on 11/14/21, 7:42 PM

    I visited a friend a while ago and she was reading this amazing book on her tablet, "The AgloDaily Book: Core Essentials"

    https://algodaily.gumroad.com/l/yYEqr

    I really loved its illustrations; it looks so easy to follow.

  • by jventura on 11/19/21, 3:21 PM

    I’m in the initial phase of structuring a book that aims to teach some simple algorithms and data structures using Python. The idea is that the reader should do the (guided) exercises to learn how to build those data structures. It’s basically “learn by doing”.

    For instance, to teach what a stack is I’ll explain the basic idea of a stack, then provide a base class and the reader must implement each method (init, push, pop, peek, etc) given the requirements and intended results of each functionality.

    I already have a kind-of “learn by doing” ebook about Python (https://github.com/joaoventura/full-speed-python), so it’s like a follow-up to that one.

    Don’t know if people are interested in a book like this, though..

  • by _448 on 11/14/21, 4:11 PM

    I usually don't complete online courses(I get bored watching educational videos) except for one. This course is on data structures and is conducted by University of California, San Diego. It is available both on edX[0] and Stepik[1]. The reason this online course is so engaging is because it uses the active-learning way of teaching. I was surprised how well that technique worked!

    [0] https://www.edx.org/course/data-structures-an-active-learnin...

    [1] https://stepik.org/579

  • by morty_s on 11/14/21, 9:38 PM

    CLRS does about 5 pages on linked lists. If you want more, my advice would be to implement one in your language of choice, then look at the API for the standard lib impl of a linked list in your language of choice (if there is one).

    Otherwise, for more reading on linked lists, read through adlist.c from redis, by antirez.

    Next, one of the best books (IMO) on algorithmic thinking is SICP. Get through a few chapters, do the exercises, grok recursion, etc.

    In no particular order:

    Algorithm Design Manual, Skiena

    Algorithm Design, by Kleinberg/Tardos

    Algorithms, Sedgwick 4th ed. (also Algorithms in C)

    If you find you need or would like more math:

    Discrete Mathematics (Epp is an easier read, Rosen seems more verbose)

    Mathematical Proofs, Charrand

    For interview prep:

    Elements of Programming Interviews (EPI)

    Designing Data Intensive Applications

  • by rajansaini on 11/18/21, 6:11 AM

    I'm only a student, but I personally really liked https://jeffe.cs.illinois.edu/teaching/algorithms/

    This is better for more advanced topics, like dynamic programming (well, advanced for me, anyway). I started out taking over an hour to solve the first problem in the problem sets, but they build on each other slowly, so I was soon able to see solutions within minutes. It took me a weekend to go through chapters I was struggling with, and I did really well on my coding interview the next day.

  • by bsuvc on 11/14/21, 6:12 PM

    CLRS is pretty accessible for beginners, at least as far as computer science textbooks go.

    Perhaps you would benefit from pairing it with a MOOC like MIT's Introduction to Algorithms. https://ocw.mit.edu/courses/electrical-engineering-and-compu...

  • by say_it_as_it_is on 11/15/21, 11:33 AM

    Are they any algorithms books that don't use data structures backed by linked lists but rather arrays or vectors? Linked data structures are barely used in practice. It can take a student weeks to go through Linked Lists and data structures using them, just to find that they're hardly ever used in practice. Contiguous data structures are the dominant types used today.
  • by wagslane on 11/15/21, 4:16 AM

    Not a book but I've been working on a project that sounds like it might be what you're looking for. It's algos and data structures courses in code-along style. It's free to audit, so do check it out even if you aren't interested in the certification stuff :)

    https://qvault.io

  • by rob_c on 11/14/21, 8:37 PM

    Best I can offer is for data structure think about working backwards if possible.

    Every time I've been involved in projects using 10TB+ data it's the processing that tends to expose the most bottlenecks. Nothing competes with making a pseudo dataset about 5%-10% of the size and running benchmarks which roughly match your expected workflow.

    Good luck

  • by MalayAgr on 11/19/21, 4:09 PM

    For algorithms, I can't think of a book better than CLRS, honestly, unless you want to read The Art of Computer Programming by Knuth. For data structures, a great book with a huge amount of detail is Data Structures Using C and C++ by Langsam and Tanenbaum.
  • by fsloth on 11/14/21, 5:52 PM

    "Foundations of Computer Science" by Aho and Ullman is my personal favourite.
  • by b20000 on 11/14/21, 3:24 PM

    just move on to leetcode. you just memorize the bullshit questions and their answers. that is what everyone seems to be doing. actual experience makes no difference. you have 2 minutes to produce the exact answer expected.
  • by jypepin on 11/14/21, 4:50 PM

    Algorithms in a nutshell by George T. Heineman helped me a lot. It's not too dense so it's easy to read, just enough to learn and understand how an algorithm works.
  • by eitland on 11/14/21, 3:13 PM

    I had "walls and mirrors". Recently I met someone who said that was the only book they kept from uni.

    I can't say if it is good or not, I lost mine well over a decade ago.

  • by aynyc on 11/14/21, 5:23 PM

    Segwick's book is far more useful in self-learning environment than other text books. He has videos and fully functioning code to help you understand.
  • by laxmena on 11/14/21, 6:14 PM

    Introduction to Algorithms -Thomas H. Cormen

    Grokking Algorithms -Aditya Bhargava

    Cracking the Coding Interview Book by Gayle Laakmann McDowell

  • by rpac0 on 11/14/21, 7:46 PM

    in case you want to code along, Ive been through some of the books mentioned here and solved the algorithms. You can check it out at https://github.com/realpacific/algorithms
  • by anuragbiyani on 11/14/21, 6:54 PM

    The good thing about Algorithm Design & Analysis as a topic is that there is no lack of great resources to learn from! And even better, a large portion of them are freely available online (in forms of lecture notes, or entire books, video lectures, programming challenges with online judging, etc).

    In addition to what others have mentioned, here are some example resources you might prefer for a beginner-intermediate level intro:

    1. (free online) Algorithms by Dasgupta, Papadimitriou, and Vazirani http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-...

    2. (free online) Algorithms by Jeff Erickson https://jeffe.cs.illinois.edu/teaching/algorithms/

    3. Algorithm design by Kleinberg & Tardos https://www.amazon.com/Algorithm-Design-Jon-Kleinberg/dp/032...

    4. Another one specifically for more applied view (esp., how they are used in programming contests such as ICPC) is Skiena & Revilla's "Programming Challenges" book (https://www.amazon.com/Programming-Challenges-Contest-Traini...). Note that this is different than Skiena's other popular book (Algorithm Design manual) which is also pretty good and has a "war story" based perspective to design of algorithms.

    5. There are also several resources where lecture notes from university Algorithm & DS courses are very useful. Here is an example from my previous Professor, David Kempe: http://david-kempe.com/teaching/DataStructures.pdf

    6. Several programming competition specific tutorials can be found on Topcoder: https://www.topcoder.com/thrive/tracks?track=Competitive%20P... (individual SRM archives are also good place to try problems first hand and then learn from other's approach). In general, if you search for ACM-ICPC resources, you will find a lot more targeted information/problems which will apply not only for leetcode, but also for detailed understanding of the theory too.

  • by uptheroots on 11/14/21, 4:57 PM

    CLRS is what I used in college
  • by iamsb on 11/14/21, 3:37 PM

    The Art of computer programming by Donald Knuth.
  • by salutis on 11/14/21, 7:25 PM

    I like:

    algorithms.wtf

    Algorithms by Jeff Erickson

  • by zamkam1031 on 11/21/21, 4:33 AM

    tl;dr: No