r/computerscience • u/Numerous_Economy_482 • Dec 16 '25
Where can I learn algorithms by its real motivation first?
Sorry if I’m not clear. Like, most algorithms book start showing how is DFS , BFS. But I don’t see any utility on it, is there some course, book that start by the motivation problem first, like, why we need to find a X algorithm to solve this kind of problem?
It would be something like a math teacher ask how to minimize the volume , provoque and show students the importance and then teach calculus.
38
Upvotes
23
u/EatThatPotato Compilers, Architecture, but mostly Compilers and PL Dec 16 '25
What book are you using? In my experience most beginner textbooks do a pretty nice job of “imagine this situation, how do we do this? we could try this, or this, but have this problem. luckily we have this”
6
u/Big-Lawyer-3444 Dec 16 '25 edited Dec 16 '25
I don't know of anything like this. It's a good idea. I really think documentation and learning material needs to move to more of a story-first paradigm. At the moment a lot of it is marketing and explaining things as if they already existed. Possibly the phenomenon you've identified is related to "transmissionism" which is the fallacy of being able to transmit a mental model to someone else by describing it.
I've learned this stuff best by having it come up in real projects. An example: I used Tree-sitter to implement syntax highlighting for a code editor. Rendering syntax-highlighted code requires matching up two different types of data structures, the way I did it anyway. One is the rows/cols of characters and one is the syntax tree. You don't want to render the whole file every frame, so you start with the first visible line (from scrolling). You also need to get the syntax tree node associated with that row/col position, to know how to highlight the text. So if you want it to be efficient, you have to do a binary search of the tree.
Basically any time you need to make something better than a simple brute force algorithm for performance reasons, you'll need algos and data structures. I've also written an LRU cache (but ended up using a lib as I didn't get it right) and a few other interesting algos. I even invented my own version of find/replace based on Tree-sitter queries. Once you get a feel for a few things like this it'll be easier to see the motivation for other algos I'd imagine.
Writing a database would probably be another good example that requires a lot of algorithms. Leetcode is probably loosely driven by FAANG interviews which I'd hope are loosely driven by FAANG problems - stuff like efficiently analysing relationships between nodes in a graph database, for friend relationships etc. (But to your point, they are usually so removed from reality that it's hard to imagine where they actually came from.)
5
u/JellyTwank Dec 16 '25
I understand that it helps to know why you are doing a thing, and thia applies to algorithms too - it helps you to understand the motivation and application.
I dont know of any algorithm books that start with discussions of breadth and depth first searches, simce those apply to tree traversals, which are a data structure usually introduced at the same time. The books I know of almost all start with sorting an array because that is a basic data structure anyone studying algorithms should already be familiar with.
In most cases, the discussion of the algorithm states the problem being solved. A google search for something like "where is the convex hull algorithm used" brings up a number of use cases, and you can then read those to help understand the motivation for that algorithm. So just do a similar query for each algorithm you are learning.
For me, one of the best books for learning the basics for most common algorithms is "Introduction to Algorithms" by Cormen, Leiserson, Rivest and Stein. They include nice mathematical analyses of each algorthm too, as they introduce and define big-O notation.
A long time ago when I was self-teaching myself C, I used "Algorithms in C" by Sedgewick. Not sure if that is still on print or not.
3
u/jkingsbery Dec 16 '25
In a typical sequence, students have likely seen graphs in a couple classes before getting into an algorithms class, and probably have seen a bunch of examples:
As a result, for most texts rehashing this material again when they assume people know what a graph is for would make it more tedious for their audience.
If you're looking for some motivating examples, check out the book "Programming Challenges" by Skiena. He has some good exercises that are framed as concrete use cases. Chapter 9 covers graph traversal.
2
u/ivancea Dec 16 '25
My usual recommendation is by learning through making projects, instead of books or courses of any kind.
Quick projects, topics that interest you, deeply technical if possible. And investing in them the least required time, just until you do what you wanted to.
And don't think about projects like "an app to do X", but about POCs like "I never made a protocol, let's do a client-server", or bigger like "let's do a graph database with its query language".
In any case, things you never did, or you'll learn nothing. And if you're sure that you can do something, and you know its outcome, it's printable not worth either.
1
u/PhilNEvo Dec 18 '25
If you specifically want to know about stuff related to DFS and BFS, and some of their alternatives, I would recommend reading Chapter 3 "Solving Problems by Searching" in "Artifical Intelligence: A Modern Approach. Third Edition" by Russel and Norvig. You should be able to find it online with a little googling :b
That chapter imo takes a very motivational approach to that topic!
3
u/jeffgerickson Dec 17 '25
Check out Steven Skiena’s Algorithm Design Manual.
I have a bunch of free algorithms course materials (including a textbook) on my web site (https://jeffe.cs.illinois.edu/teaching/algorithms), but I honestly don’t know if it will scratch your “real motivation” itch. Different readers (and writers) are motivated by different things. Graph algorithms in particular have so many applications that trying to pick just one as “the” “real” motivation is futile.
2
u/lukkasz323 Dec 17 '25
Advent of Code
2
u/Zarathustrategy Dec 17 '25
You can do this year's advent of code maybe. Or maybe another year is better
1
u/Candid-Border6562 Dec 16 '25
“If all you have is a hammer, then everything looks like a nail.” You could just think of it as adding more tools to your toolbox and move forward.
On the other hand, the programming nuances between BFS and DFS occur in many other situations. In many ways, the techniques for understanding and building algorithms can be more important than the algorithms themselves, particularly when no algorithm exists for your problem.
But on the third hand, I appreciate your frustration. Many lessons are best learned through example. I have a suggestion that will come at this sideways. “Programming Pearls” by Jon Bentley might be good for you.
PS - During prehistoric times, we were taught several ways to “sort”. When I ran into pre packed quicksort routines in language libraries, I thought ”well, that was a waste, why not just teach us to use the library”. Then came the fateful day when we needed to sort a data set too large to fit into memory and too big to fit onto the available disk. Thank you, Knuth.
-1
u/PickledBrainJuice Dec 16 '25
I'm sorry, but how is it possible that you don't see the utility in DFS and BFS of all things? In my opinion, it just means that you didn't even bother trying to understand. I could understand if you didn't immediately realise the utility of Kosaraju-Sharir or Ford-Fulkerson when you initially get to them, for example, but searching? I'm not trying to provoke you, I just want to understand.
-3
u/SgtSausage Dec 16 '25
You can't "find the utility" in searching?
-2
u/Seefufiat Dec 16 '25
“Why do I need x algo to solve y problem” well, not surprisingly, by learning the algorithm that becomes pretty apparent. But we use x algo to solve y problem because of stability, thoroughness, or speed, depending on the application.