Live data from Hacker News

Ask HN: What's your favorite elegant/beautiful algorithm?

news.ycombinator.com

351–360 of 507 posts

Re: Ask HN: What's your favorite elegant/beautiful algorithm?

#351

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…

I tend to use a min/max equation that acts similar to a fibonacci backoff, allowing for a couple rapid retries before the exponential backoff kicks in. Provides a little extra speed for the transient problems.

Re: Ask HN: What's your favorite elegant/beautiful algorithm?

#352

Gosh, 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…

Do you have any real-world examples where wavelet trees are used? They look really interesting, I'm just not sure where I'd ever use them!

Re: Ask HN: What's your favorite elegant/beautiful algorithm?

#353

Binary 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.

`git bisect` is an indispensible git trick. You give it commit where you know the bug exists, and one where you know it doesn't and it'll use binary search to help you find where the bug came from. It's absolutely wonderful.

Re: Ask HN: What's your favorite elegant/beautiful algorithm?

#355

My 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…

That video, appropriately enough, also contains a definition and explanation of what the Y Combinator is.

Re: Ask HN: What's your favorite elegant/beautiful algorithm?

#356

Earlier 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.

Negative 2 points on a post saying that a computational method that possibly never terminates is not an algorithm... Oh dear...

Re: Ask HN: What's your favorite elegant/beautiful algorithm?

#357
post #242

The 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.

> 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?

#358
Euclidean method to calculate GCD using visualization [1]

Kosaraju 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]

[1] - https://www.youtube.com/watch?v=kiFfp-HAu64&t=326

[2] - https://en.wikipedia.org/wiki/Kosaraju%27s_algorithm

Re: Ask HN: What's your favorite elegant/beautiful algorithm?

#359
post #201
post #158

Earlier 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!)

Have you read TAPL? I'm curious how you think that stacks up to readily-available online resources.

Re: Ask HN: What's your favorite elegant/beautiful algorithm?

#360
The Gilbert-Johnson-Keerthi convex shape intersection algorithm.

It 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.

Post reply on HN