Live data from Hacker News

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

news.ycombinator.com

181–190 of 507 posts

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

#181
I'm surprised no one mentioned PageRank (or maybe I missed it).

Another favorite is RAFT, simply because of how elegant and simple it is and how easy it is to understand.

Also - I used to work in a company that did P2P video streaming (pre WebRTC) and the way the protocol leveraged Reed Solomon Coding was just awesome. https://en.wikipedia.org/wiki/Reed%E2%80%93Solomon_error_cor...

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

#182
I'm a fan of the three parameter sign fit https://ieeexplore.ieee.org/document/469117. Sorry for the paywall. Essentially if you want to fit a sinusoid to some data, and know the frequency, you can transform it into a linear least squares problem. If anyone is interested I can type up the math when I'm not on mobile, it's pretty straightforward.

Honorable mention: Kahan summation algorithm https://en.m.wikipedia.org/wiki/Kahan_summation_algorithm

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

#183
post #136
post #96

It would be hard to beat Floyd–Warshall algorithm. Think about the problem of finding shortest paths between all pairs of nodes in graph. Sounds trivial? Would you believe if someone told you it can be done in literally 5 lines of code? When I saw that for the first time I was in the disbelief. The elegance comes from these fact, - Very non-trivial problem - Just 5 lines of code - Probably the most language agnostic…

> Probably the most language agnostic algorithm Are you sure about this point? I'd like to see a purely functional implementation of this, e.g. in Haskell without monoids. I believe it's significantly harder than a C implementation (which is indeed a few lines)

Not really, if you wanted to do a version using only immutable arrays:

  import Data.Array
  
  fw :: Int -> Array (Int, Int) Int -> Array (Int, Int) Int
  fw 0 graph0 = graph0
  fw k graph0 =
    let fw' = fw (k - 1) graph0 in array (bounds graph0)
    [ ((i, j), min (fw' ! (i, j)) ((fw' ! (i, k)) + (fw' ! (k, j))))
    | (i, j) 
The only difference with a mutable implementation is that you use a new array for every k, whereas in a mutable version you would reuse these.

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

#185
post #73

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

I've used it as a very simple equation solver in a pinch. Yes, it's a very naive approach for most equations, but getting started solving it satisfactory with a solution given is very welcome.

...wait, how? I’ve never heard of a use like this.

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

#186
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=0m6hoOelZH8

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

#187
post #136
post #96

It would be hard to beat Floyd–Warshall algorithm. Think about the problem of finding shortest paths between all pairs of nodes in graph. Sounds trivial? Would you believe if someone told you it can be done in literally 5 lines of code? When I saw that for the first time I was in the disbelief. The elegance comes from these fact, - Very non-trivial problem - Just 5 lines of code - Probably the most language agnostic…

> Probably the most language agnostic algorithm Are you sure about this point? I'd like to see a purely functional implementation of this, e.g. in Haskell without monoids. I believe it's significantly harder than a C implementation (which is indeed a few lines)

IIRC you can express it with matrix multiplication, but there might be some big caveats on that.

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

#189
I like Fortune's algorithm for Voronoi regions. Especially if it is being visualized - watching the beach line sweep through and the polygon regions form is kind of hypnotic.

I also have spent way too much time trying to get an implementation of it right, so there's a little stockholm syndrome.

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

#190
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.

And here is an elegant explanation of the Diffie-Hellman protocol https://www.youtube.com/watch?v=YEBfamv-_do
Post reply on HN