Ask HN: What are your favorite algorithms?
Post any algorithms you think are cool, inventive, or useful.
PDF links or blog posts are appreciated.
My current favorite is SWIM. https://www.cs.cornell.edu/~asdas/research/dsn02-swim.pdf
My current favorite is SWIM. https://www.cs.cornell.edu/~asdas/research/dsn02-swim.pdf
92 comments
[ 2.9 ms ] story [ 181 ms ] threadhttps://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transf...
https://www.youtube.com/watch?v=4n7NPk5lwbI
Parallelizable and efficient method for producing high quality pseudo random bits.
Designed to achieve maximal period (with 500 bit state the period of repetition is one less than 2^500).
Can be run backwards or forwards. Running it backwards undoes running it forwards and reproduces the pseudo random bits in reverse order.
A modified version of it is used in most CPU register out-of-order execution
https://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_al...
http://michaeljflynn.net/2015/06/01/my-favorite-algorithm-me...
Also best (only?) practical use of the (inverse) Ackermann function!
That said, many combinatorial optimization problems that look quite similar to NP hard problems have very nice and efficient LP formulations, and for many NP hard problems, integer programming-based methods (which in the end mostly solve LP relaxations) are among the best algorithms. For example, planar TSP can be solved for tens or even hundreds of thousands of nodes using the LP relaxation, branch, and cut tool set.
Modern LP solvers don't just use the Simplex method though. I'm very partial to the Simplex method myself, but in practice, Interior Point Methods are often used.
The one downside is that you have to pre-process the string, which takes O(n) time and between 5n and 9n space depending on exactly how you do it. But after that, you can do as many searches as you want practically "for free".
There's a paper outlining the various algorithms available here: (PDF) http://www.cosc.canterbury.ac.nz/research/reports/HonsReps/2...
PHP implementation: https://github.com/bandwidth-throttle/token-bucket
http://nginx.org/en/docs/http/ngx_http_limit_req_module.html
The algorithms for computing it are beautiful and have enabled much of the technology we see around us today.
Whitted's recursive ray tracing algorithm https://en.wikipedia.org/wiki/Ray_tracing_(graphics)#Recursi...
PageRank
Abstract: An algorithm M is described that solves any well-defined problem p as quickly as the fastest algorithm computing a solution to p, save for a factor of 5 and low-order additive terms. M optimally distributes resources between the execution of provably correct p-solving programs and an enumeration of all proofs, including relevant proofs of program correctness and of time bounds on program runtimes. M avoids Blum's speed-up theorem by ignoring programs without correctness proof. M has broader applicability and can be faster than Levin's universal search, the fastest method for inverting functions save for a large multiplicative constant. An extension of Kolmogorov complexity and two novel natural measures of function complexity are used to show that the most efficient program computing some function f is also among the shortest programs provably computing f.
The catch is that the constants in the big-O are enormous.
Everything is scheduled so it's asymptotically optimal. However, there's an added cost to search for algorithms. This cost is exponential in the proof length, but constant for each problem since it is independent from the inputs. Of course, this constant is ridiculously huge.
It was probably the algorithm that cemented my love of computer science, it's such an elegant solution to a problem and so simple once you understand it.
RSA.
He recently updated his implementation. Is very fun to read his notes on his implementation. In particular, he discusses things he had wrong.
If you like poor JavaScript implementations, I can post mine again.
One of those algorithms where you go "Shit so obvious and also so completely genius that I could never have thought of it"
Also, the internet as we know it would not work without it