Live data from Hacker News

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

news.ycombinator.com

261–270 of 507 posts

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

#261
post #164

Levenshtein distance, a dynamic programming algorithm for determining the edit distance between two sequences - the number of insertions, deletions or substitutions required to convert one sequence to the other. https://en.wikipedia.org/wiki/Levenshtein_distance

Gosling used a variant of it to optimize screen updates in emacs.

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

#262
post #254

Quicksort ( especially implemented in a functional language like haskell ). It's a perfect mix of an elegant algorithm and a beautiful implementation. Simple, elegant, beautiful and powerful. What more can you want in an algorithm.

Came into the comments to find this, and I'm going to expand on your description: I feel like quicksort isn't [uniquely] beautiful except when used alongside bubble sort to explain computational complexity (both big-O "worst case" and big-Ω "best case"). These were the examples that really made the concepts "click" for me.

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

#263

Duff's device. Because you will first look at it in disgust, but then you realize how clever it really is. See: https://en.wikipedia.org/wiki/Duff%27s_device

Disgusting indeed: A way to write irreducible loops that doesn't use a goto. Good idea to measure the resulting code, given how poorly most optimizers deal with such loops.

Even sadder because the same effect can be achieved cleanly (and "optimizably")by using as "switch" followed by a "while".

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

#265
post #5

Euclid's GCD algorithm, and Russian peasant multiplication.

I like this version of the Euclid algorithm: Make a rectangle with side lengths of 2 positive real numbers. Put the biggest square inside (i.e. against a short side) that you can, add as many as fit. If there's still a gap, put the biggest square that fits in that, repeating until filled.. If you never fill the gap, the ratio is irrational. The number of squares of each size used gives the ratio's continued fraction.

in visual form (hold shift and move mouse): https://shaunlebron.github.io/ratios/

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

#266
post #207

Earlier quoted context omitted.

Nope. An algorithm has to be effective. You can find pathological cases for k-means such that it will never converge on anything useful. So if you set your termination case to be convergence it will never terminate and if you don't then it will never be effective.

I think you might be in the minority in this opinion. Many algorithms have pathological cases but are still considered algorithms

Minority? This is directly from Knuth.

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

#267
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 we now have an algorithm for O(n) generalized sorting. It's called discrimination sort, and while it's a complex subject its quite elegant once you internalize the algorithm. There's a talk by Prof. Henglein here: https://www.youtube.com/watch?v=sz9ZlZIRDAg

2017 saw the recommendation of a truly phenomenal approach to indexing data, using simple leaning structures. These indexes are poorly explored, there's still a ton of headroom here, but the paper is an incredibly cool read and very approachable: https://arxiv.org/abs/1712.01208

Another really cool family of algorithms and associated structures in distributed systems is CRDTs. They're poorly understood, but in research they have largely superseded operational transforms. The Wikipedia page branches off into lots of good research: https://en.wikipedia.org/wiki/Conflict-free_replicated_data_...

If we slightly broadened the lens of what is an algorithm, my favoriate category-theoretic approaches to solving problems include Recursion Schemes which are a technique to totally separate the specification of recursive algorithms over data structures from both the structures and the code that the algorithm runs to compute a result (Patrick from Github has a great series on them here: https://blog.sumtypeofway.com/). I also love the modern and rapidly growing "Ghosts of Forgotten Proofs" as a way to let the compiler assert you've properly built and/or terminated values: https://github.com/matt-noonan/gdp-paper/releases/download/j...

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

#268

Earlier quoted context omitted.

No. You can't prove that k-means does anything useful.

Is the definition of "algorithm" that you're using here useful?

It's one of the most fundamental concepts in computer science and underpins decades of research. You can decide if it's useful.

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

#270

The (fairly simple) nimber algorithm for optimal nim play which can then be applied to other games e.g. dots and boxes

I learned this while hanging out in the library (Martin Gardner’s column in Scientific American). I put it to good use years later in college: I decided to implement a Nim playing circuit (out of RTL logic gates, yuk!) for my digital design lab course final project in 1975. It was a complex sequential circuit, but the result was quite remarkable. It played perfect Nim with up to four piles of 1 to 16 tokens.
Post reply on HN