Exponential backoff: I don't know if this is a published algorithm. Basically, background processes need to keep retrying an operation until it succeeds. (Make an API call to a server, upload a file, ect, ect.) If the retry interval is too small, you can DOS the remote server. (Server returns a 5xx error because there's a corner case that hits a defect.) But, if the retry interval is too large, your background proces…
Ask HN: What's your favorite elegant/beautiful algorithm?
351–360 of 507 posts
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#352Gosh, I have a few! Fistly, wavelet trees! Wavelet trees with something like RRR encoding for the bit vectors lets you work with massive datasets in positively tiny space and constant time. They're not even expensive to construct! My favorite introduction to the whole space of rank/select-friendly structures is Alex Bowe's tutorial: https://alexbowe.com/wavelet-trees/ I constantly agitate for people to realize that w…
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#353Binary search. Very simple, incredibly powerful; can search on data or math function. It's the basis for other CS concepts. JS implementation: https://gist.github.com/netgusto/90c8e0e7019a832cbf95eac58e1...
A cool thing about binary search is you can also use it in real life. It's great for troubleshooting.
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#354It generates perfect mazes of arbitrary size M*N, using only O(max(M, N)) memory.
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#355My favorite is the FLAC algorithm. It's fairly simple to explain - take a signal input, pattern match a generative signal that closely matches the original or part of it, and the output is a combination of that generative code and the difference between it and it and the input. Second favorite is the LISP REPL. Specifically the Eval function, thanks to this classic video of Sussman https://www.youtube.com/watch?v=0m6…
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#356Earlier quoted context omitted.
BogoSort is an algorithm. Not a very good algorithm, but an algorithm nevertheless.
No it absolutely is not. The lack of fundamental computer science knowledge in this thread is alarming.
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#357The Euclidean algorithm to compute the greatest common divisor of two numbers. In Javascript, if you have two numbers x and y that are both greater than zero, you compute the greatest common divisor xyGcd in one beautiful line of code: var xyGcd = function gcd(a,b){ return b ? gcd(b, a%b) : a; }(x, y); In my opinion this is the greatest one-liner ever.
I dunno, I really like Haskell's one-line infinite Fibonacci sequence:
> let fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
> take 7 fibs
[1,1,2,3,5,8,13]
> fibs !! 1000
70330367711422815821835254877183549770181269836358732742604905087154537118196933579742249494562611733487750449241765991088186363265450223647106012053374121273867339111198139373125598767690091902245245323403501
I don't think it's the most efficient solution, but it really showcases lazy evaluation with a simple example.Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#358Kosaraju two pass algorithm, this one blew me over when I first read it and I am still impressed by the ingenuity of this algorithm [2]
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#359Earlier quoted context omitted.
I like implementing type systems Could you expand on this? This is something I've just started reading about. I'd be interested in good resources to use to get started. At the moment I've just started reading TAPL.
Check out https://github.com/tomprimozic/type-systems there's been a few HN threads about it as well. I can also answer any specific questions you have, or if you want further resources I can try and find them... (there's a good online book I have in mind, but I've no idea how to find it right now!)
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#360It can take any two convex sets and tell you if they overlap, while converging on a separating axis or a penetration point, if it exists. All you need is a support function that returns a point in the set that is furthest along a given direction vector. The algorithm works in an arbitrary number of dimensions.
Basically, it operates on a new shape which is the Minkowski difference of the two intersected sets. If the difference-shape contains the origin, then the two shapes overlap. The algorithm iteratively constructs simplexes inside the Minkowski difference, trying to build one that encompasses the origin. This allows it to have essentially constant memory for a given dimension of problem.
It's a very elegant formulation of a very general problem. It's so pleasing that you can intersect cylinders with cones, or oriented cubes with frustums, or polytopes with superellipses all with the exact same single algorithm.
The Wikipedia writeup is terrible, so I may do my own with nice illustrations at some point.