Live data from Hacker News

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

news.ycombinator.com

221–230 of 507 posts

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

#221
post #149

Earlier quoted context omitted.

It is absolutely an algorithm in the sense of "a set of rules to be followed". I think you mean that it doesn't guarantee an optimal solution. That just means it's a heuristic algorithm, same as simulated annealing is a heuristic algorithm for solving optimisation problems.

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.

BogoSort is an algorithm. Not a very good algorithm, but an algorithm nevertheless.

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

#224
post #8

Diffie–Hellman. I know that cryptography can get much fancier and more clever, but Diffie–Hellman took a concept my intuition told me was impossible and showed that it's possible in a really simple, elegant way. Learning about it was the first time I realized how beautiful the math behind computer science is. It's also a great insight into just how fundamental the concept of computational complexity is.

+1 for Diffie-Hellman... and the paint analogy.

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

#225
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 process can run too slowly when it encounters transient errors. (Temporary network glitch, database under high load because of a cache flush.)

So, we pick an ideal small retry interval, and the maximum tolerable interval. (In my case, it's usually 30 seconds and 15 minutes.) Then, when errors happen, the retry interval keeps doubling until it hits the largest interval. So, the operation is retried at 30 seconds, 1 minute, 2 minutes, 4 minutes, 8 minutes, and then every 15 minutes until it succeeds.

The result is that transient errors don't cause large delays. Situations where a defect prevents a request from submitting don't DOS, because the retry interval throttles itself longer and longer.

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

#227
My first programming project was a tic-tac-toe game with a computer opponent. I painstakingly copy-pasted about a hundred nested `if` statements to check for a winner & decide the computer’s next move.

Several years later I saw a Matlab demo that did this by indexing the grid using values from a 3x3 magic square[1]. In a magic square, every row, column, and diagonal has the same sum. So checking for a winner was just checking if a player’s three moves added up to 15. Finding a space that would make the computer win, or block the human opponent from winning, was subtracting two moves from 15 and checking if that spot was available.

[1]: https://en.wikipedia.org/wiki/Magic_square

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

#228
post #226

I don't know if it can be considered an algorithm, but i found bezier curves generation super useful and simple

Well, on the one hand, it's "just" nested linear interpolation with some control points, and traditionally, interpolation falls under mathematics. On the other, the wiki page mentions computer contexts first[0].

I think most of us would agree that there is a fuzzy boundary between mathematics and algorithms, or even that the latter is a subset of the former. Bresenham's Line Algorithm[1] is an algorithm, but it can also be interpreted as a kind of mathematics to determine points on a grid based on a path through that grid.

More importantly though: yes, it is a very useful and simple tool in many contexts.

[0] https://en.wikipedia.org/wiki/B%C3%A9zier_curve

[1] https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm

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

#229
Hashcash, a proof-of-work system adapted by Bitcoin:

https://bitcoinmagazine.com/articles/genesis-files-hashcash-...

1. It solves an extremely practical problem (spam) using a very simple idea (provide a value that when appended to a message gives a hash value within an acceptable target range).

2. It can be understood by non-experts who simply understand (or accept) the one-way nature of cryptographic hash functions.

3. It can be implemented manually, given a working hash function.

4. It sat around for a decade in obscurity until someone dusted it off to build a system most thought was impossible.

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

#230
I loved this article on the elegance of deflate http://www.codersnotes.com/notes/elegance-of-deflate/ finishes with this:

It's not one algorithm but two, that dance and weave together in harmony. You can pick dictionary matching, or entropy coding, and you can select between them on a per-byte basis with no overhead. That's what I find so clever about it - not that it can do one thing or the other, but that it can choose either, and yet represent them using the same language. No extra markers, nothing to say "oh, now we're switching to entropy mode", nothing to get in the way.

Post reply on HN