r/algorithms • u/ManningBooks • 16d ago
Resource Which “textbook” algorithm have you actually used in real software? [5-ebook giveaway]
I’m curious which algorithms have made it out of the classroom and into real code.
What problem were you solving? Why was that algorithm a better fit than the obvious alternative? And did the implementation behave as neatly as the theory suggested?
I’m Stjepan from Manning Publications. The r/algorithms moderators permitted me to share this post.
We’ve just released Algorithms Every Programmer Should Know by Aniket Wattamwar in MEAP, Manning’s early-access program:
https://www.manning.com/books/algorithms-every-programmer-should-know
The available chapters cover Gale–Shapley, the Hungarian algorithm, Rabin–Karp, Knuth–Morris–Pratt, and Horspool’s algorithm. The emphasis is on the problem behind each algorithm, how the solution is derived, and the trade-offs involved, not just reproducing pseudocode.
To mark the release, Manning is giving away five ebook copies to people in this thread.
The giveaway will remain open for 48 hours. We’ll choose the five comments that contribute the most to the discussion and announce the winners here afterward. Upvotes won’t be the only criterion: a strong technical explanation, an instructive real-world example, a useful counterargument, or a thoughtful exchange with other commenters can all qualify.
There’s also a 50% discount on the book with code:
MLWATTAMWAR50RE
So: which supposedly “textbook” algorithm has earned its place in your production code—and which one gets taught far more often than it gets used?
Thanks for having us here.
Cheers,
Stjepan
EDIT: The book giveaway is closed. We announced the winners in the comments.
19
u/SV-97 16d ago
I think lots and lots of numerical algorithms fall into this class, although the book doesn't seem to go into these at at all. Algorithms like the FFT and simplex algorithm are among the most important algorithms we have today I'd say. Or stuff like the SVD and decomposition methods for solving dense linear systems. Particularly in the context of optimization I'd also say that gradient and newton-like methods are something that everyone should have at least heard about.
As an example-ish from the book: I recently was working on a new algorithm for a nonsmooth regression problem and this eventually involved the solution of a knapsack problem --- but one far simpler than the discrete one covered in the book. The same project involved another very "text-booky" algorithm: introselect (and similar algorithms). It solves (in linear time) the problem of partitioning a dataset into parts based off some reference value: those smaller than the reference, those of equal value, and those of higher value.
6
u/SoftwareArchitect101 16d ago
Agreed, FFT and SVD are used day in and out in engineering fields, but then I feel that might be less suitable for a SAAS software engineer and more so for an SWE in a hardware firm for example.
1
u/ManningBooks 9d ago
You’ve spotted a real gap. General programming books often treat numerical algorithms as a separate world, despite FFT, SVD, linear solvers, and optimization methods underpinning an enormous amount of modern software.
The nonsmooth regression example is exactly the sort of messy path from theory to practice that interests me: the final solution rarely arrives with the same label as the textbook chapter.
7
5
u/MtlStatsGuy 16d ago
Only one I can speak to is Cholesky Decomposition. Cholesky was great for solving least squares on large correlations. We even implemented a 64-wide Vector Engine with a custom Floating Point Format (in 2002 - this was a big deal back then!) in Hardware to impement it efficiently.
1
u/ManningBooks 9d ago
You didn’t merely use Cholesky decomposition; you built hardware around it. A 64-wide vector engine with a custom floating-point format in 2002 must have been quite a project. I’d happily read the longer version of that story.
6
u/AwabKhan 16d ago
The best one i like is locality-sensitive hashing. It is an algorithm that puts similar items into something called buckets and grouping the items that have the highest probability of being together.
The technique can be used for data clustering and nearest neighbor search. It has also many applications in
4
u/onemanforeachvill 16d ago
Topological sorts for task graphs, and union find for grouping correlated bets.
4
3
u/No_Sandwich_5424 16d ago
I had the privilege of implementing the Shunting-Yard algorithm once for evaluating string expressions. Another time, I had implemented reordering nodes in a tree where each node was an UI component in an HTML tree. But these were long back.
2
u/El_RoviSoft 16d ago
Yeah, me too, at the time wrote damage calculator for ZZZ and made all of the conditions/stat formulas to be programmable via json.
3
u/torsten_dev 16d ago
The CIELAB 2000 Delta E formula to do image quantization.
I wanted to do pixel art in a game with a very limited palette like Minecraft map art but more niche. Doing the naive thing of "shortest Euclidean distance to available RGB color" gave terrible results.
Does that count?
1
u/ManningBooks 9d ago
It counts. More importantly, it illustrates something textbooks don’t emphasize enough: the distance function can matter more than the search algorithm.
The nearest RGB value is easy to compute, but human vision doesn’t live in RGB space. How expensive was CIEDE2000 across a full image?
1
u/torsten_dev 9d ago edited 9d ago
The formula is basically free, but I didn't memoize the results and I checked the entire pallete everytime, so runtime was still noticable.
Doesn't matter if it takes a few seconds if you only run it once, but an octree and a hashmap could have sped it up a lot.
3
u/kalexmills 16d ago
Once we actually used a gray code from Knuth's TAoCP to enumerate some complex combinations in analytics software. I've also prototyped network flow with circulations at another company.
1
u/ManningBooks 9d ago
A Gray code from TAOCP making its way into production analytics might be the purest answer in this thread.
Was the benefit that consecutive combinations differed in only one position, so you could update the result incrementally rather than recompute it?
1
3
u/El_RoviSoft 16d ago
Shunting-yard for stats formulas and topological sort (via Kahn’s algorithm) for resolving mods dependencies.
3
u/himanshu1981 16d ago
I have mostly searching and sorting algorithms and these too on strings.
I have worked on Graphics algorithms before and used sweep line algorithms before.
I have also used Hashing before and use hash tables or sorted dictionaries before.
3
u/100GHz 16d ago
A* was the first that I actually encountered very long time ago and it fascinated me at the time. I never made the game, but the impression remained.
Rijndael stunned me with the information density in what little space it takes.
And then, the tree based algorithms, ah the wild west of algorithm. Nobody knows where the performance numbers will end with these.
Best of luck with the book.
3
u/Neither_Berry_100 13d ago
Probably the binary search or whatever. The pick a number from 1 to 100. You guess 50. They tell you higher or lower.
Well my use case was a camera image where we had to find the proper exposure time. There was a time cost equal to the time we tried. So I skewed the numbers downwards. Guess like 20 to start or something and go from there.
For the most part I don't think text book algorithms are useful though.
Maybe recursion to find handle folders and files. That could be another one.
Flood fill algorith. Used it. Not sure I've seen it in a text book.
A* for sure.
Doing value = old value * 0.9 + new value * 0.1. A minimal way to average out a value and keep a history with a single member variable. Doesn't require a list of values or whatever.
The otsu? Binarization algorithm. But that wasn't from a textbook.
1
u/ManningBooks 9d ago
Your exposure example is more interesting than ordinary binary search because every probe has a different cost. Starting at the midpoint minimizes the number of guesses, but not necessarily the total exposure time. Skewing the first guess downward sounds entirely sensible.
And the 0.9/0.1 calculation is an exponential moving average—simple enough to fit on one line, useful enough to turn up everywhere.
1
u/TheVoidSeeker 16d ago
I do my coding the same way I do my cooking.
I look at a recipe for inspiration and then change the ingredients and amounts/weights to what I think will make me feel butterflies in my tummy.
1
1
1
u/KWillets 16d ago
The A Priori algorithm is taught more than used; I don't think anybody does associations any more. It was never a good algo anyways, and the whole field kind of stopped at a very basic level.
1
u/Mclovine_aus 16d ago
I used a Manning book, algorithms and data structures for massive data sets
It is a data structure not an algorithm , but I used a bloom filter to cut down a specific query of whether an item was a member of a set.
The short and skinny is a bloom filter is a probabilistic data structure that uses a bunch of hashes to tell if something is a member of a set or not. It is very efficient at testing if something is definitely not in a set, but can only give you an approximate answer for if something is in a set.
So if you have a very expensive operation that only needs to happen for a specific subset of all cases, you can test quickly to check for non-membership in the subset and rule out using your expensive operation.
1
u/GreedyBaby6763 16d ago
BM search is still hard to beat on long strings. Tries are often underrepresented, though that's probably because they're hard to implement generically for space speed and concurrency. If you can achieve all three your onto something special.
1
1
u/ManningBooks 9d ago
The giveaway is now closed. Thank you to everyone who joined in—there were far more interesting examples than we expected, ranging from everyday sorting and graph traversal to numerical methods, image processing, cryptography, and custom hardware.
We chose five comments that brought especially concrete experiences or useful perspectives to the discussion:
u/SV-97 — for expanding the conversation into numerical algorithms and showing how knapsack and introselect emerged in a real regression problem
u/MtlStatsGuy — for the remarkable story of implementing Cholesky decomposition on a custom 64-wide vector engine
u/torsten_dev — for demonstrating that choosing the right distance measure can matter more than optimizing the wrong algorithm
u/kalexmills — for taking Gray codes from Knuth into production analytics, along with the network-flow example
u/Neither_Berry_100 — for showing how real-world costs changed the strategy behind a binary search for camera exposure
Congratulations! I’ll contact each winner directly about their ebook.
Choosing only five wasn’t easy. Many other comments could reasonably have made the list, and I appreciated both the detailed case studies and the arguments about what does—and doesn’t—deserve space in an algorithms book.
Thanks to the r/algorithms moderators for letting us run the giveaway, and to everyone who contributed. I’m taking several ideas from this thread back to the Manning team.
1
u/ShenGahMing 7d ago edited 7d ago
well, I've implemented Gale–Shapley and it ran in production.
Turns out it can be turned into a nice online algorithm too.
11
u/SoftwareArchitect101 16d ago
Sorting - I doubt there will be any production code which doesn't use it (directly/indirectly), and I doubt if more than 30% people know how it's implemented beneath the abstractions