TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: What are your favorite algorithms?

163 pointsby EFruitover 8 years ago
Post any algorithms you think are cool, inventive, or useful. PDF links or blog posts are appreciated.<p>My current favorite is SWIM. https:&#x2F;&#x2F;www.cs.cornell.edu&#x2F;~asdas&#x2F;research&#x2F;dsn02-swim.pdf

46 comments

theemathasover 8 years ago
The Fastest and Shortest Algorithm for All Well-Defined Problems: <a href="https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;cs&#x2F;0206022" rel="nofollow">https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;cs&#x2F;0206022</a><p>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&#x27;s speed-up theorem by ignoring programs without correctness proof. M has broader applicability and can be faster than Levin&#x27;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.<p>The catch is that the constants in the big-O are enormous.
评论 #13519441 未加载
评论 #13519160 未加载
DDR0over 8 years ago
Given the head of a linked list, how do you determine if it loops? eg, an l-shaped list is easy to determine - you simply process each element in the list until you find one without a subsequent element. But what if it&#x27;s a 9-shaped linked list? You&#x27;ll never run out of elements, so the best you could seem to do would be to store a reference to each element and check against all references to see if you&#x27;ve found a duplicate.<p>There&#x27;s a way to do it in O(1) space, though.<p>If you start off two runners in the list, and each &quot;step&quot; move one twice and the other only once, if there&#x27;s a loop they will eventually run into each other and be processing the same element. Simple, elegant, and nothing I&#x27;d have ever thought of. :)
评论 #13519380 未加载
评论 #13519234 未加载
评论 #13519229 未加载
评论 #13519250 未加载
评论 #13529833 未加载
评论 #13520545 未加载
评论 #13519218 未加载
评论 #13519349 未加载
wmuover 8 years ago
1. Lempel-Ziv compression algorithms (both LZ77 and LZ78). They are practical, even if one code their basic versions will get good compression ratio. Moreover, they opened the whole big chapter of data compression (LZW, LZSS and more). 2. The algorithm behind rsync. Nice one. 3. Jarvis algorithm for finding convex hull. 4. Speaking of data structures I like xor-linked list, it&#x27;s neat.<p>[1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;LZ77_and_LZ78" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;LZ77_and_LZ78</a> [2] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Rsync#Algorithm" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Rsync#Algorithm</a> [3] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Gift_wrapping_algorithm" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Gift_wrapping_algorithm</a> [4] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;XOR_linked_list" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;XOR_linked_list</a>
评论 #13520721 未加载
评论 #13519957 未加载
cpercivaover 8 years ago
I feel I have to mention the algorithm for matching with mismatches which I published in my thesis (<a href="http:&#x2F;&#x2F;www.daemonology.net&#x2F;papers&#x2F;thesis.pdf" rel="nofollow">http:&#x2F;&#x2F;www.daemonology.net&#x2F;papers&#x2F;thesis.pdf</a>), simply because it gave me a legitimate opportunity to use the phrase &quot;Fourier Transform of the FreeBSD kernel&quot;.
toomanybeersiesover 8 years ago
As basic as it is, and completely uninventive, I&#x27;ve always loved Dijkstra&#x27;s algorithm.<p>It was probably the algorithm that cemented my love of computer science, it&#x27;s such an elegant solution to a problem and so simple once you understand it.
评论 #13519344 未加载
评论 #13519126 未加载
daurnimatorover 8 years ago
The burrows-wheeler transform is pretty interesting <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Burrows%E2%80%93Wheeler_transform" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Burrows%E2%80%93Wheeler_transf...</a>. It&#x27;s behind the bzip compression format.
评论 #13520160 未加载
kul_over 8 years ago
I have never seen anything more elegant than Disjoint Set Datastructure <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Disjoint-set_data_structure" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Disjoint-set_data_structure</a>
评论 #13520908 未加载
评论 #13519618 未加载
jnpatelover 8 years ago
Bloom filters: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bloom_filter" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bloom_filter</a>
评论 #13528104 未加载
ewwertee2454over 8 years ago
Metropolis-Hastings:<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Metropolis%E2%80%93Hastings_algorithm" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Metropolis%E2%80%93Hastings_al...</a><p><a href="http:&#x2F;&#x2F;michaeljflynn.net&#x2F;2015&#x2F;06&#x2F;01&#x2F;my-favorite-algorithm-metropolis-hastings&#x2F;" rel="nofollow">http:&#x2F;&#x2F;michaeljflynn.net&#x2F;2015&#x2F;06&#x2F;01&#x2F;my-favorite-algorithm-me...</a>
评论 #13518995 未加载
elihuover 8 years ago
ROAM: Real-time Optimally Adapting Meshes <a href="https:&#x2F;&#x2F;graphics.llnl.gov&#x2F;ROAM&#x2F;roam.pdf" rel="nofollow">https:&#x2F;&#x2F;graphics.llnl.gov&#x2F;ROAM&#x2F;roam.pdf</a><p>Whitted&#x27;s recursive ray tracing algorithm <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Ray_tracing_(graphics)#Recursive_ray_tracing_algorithm" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Ray_tracing_(graphics)#Recursi...</a><p>PageRank
110011over 8 years ago
Here&#x27;s a rather surprising one. There&#x27;s an algorithm to generate a uniform random integer in the range 1..n with a KNOWN factorization. That means the algorithm is like a regular random number generator except it will also tell you how to factor it! Magic? yes! This always surprises people and i love results like this.<p>Search for the paper by Adam Kalai on how to generate random factorized integers easily. It uses log^2 n primality tests an average. The result is originally by Eric Bach who managed it using only log n primality tests but using a more complex algorithm.
gwicks56over 8 years ago
Diffie-Hellman<p>One of those algorithms where you go &quot;Shit so obvious and also so completely genius that I could never have thought of it&quot;<p>Also, the internet as we know it would not work without it
aserafiniover 8 years ago
Ukkonen&#x27;s algorithm for linear time suffix tree construction. Knuth called it &#x27;the algorithm of 1973&#x27;.
rbrittonover 8 years ago
Token Bucket. Very lightweight way to implement rate limiting.<p>PHP implementation: <a href="https:&#x2F;&#x2F;github.com&#x2F;bandwidth-throttle&#x2F;token-bucket" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bandwidth-throttle&#x2F;token-bucket</a>
评论 #13518968 未加载
zelahover 8 years ago
Linear Hybrid Cellular Automata: <a href="https:&#x2F;&#x2F;pdfs.semanticscholar.org&#x2F;7835&#x2F;161f253ab117a3666fa8e74e008782a789cd.pdf" rel="nofollow">https:&#x2F;&#x2F;pdfs.semanticscholar.org&#x2F;7835&#x2F;161f253ab117a3666fa8e7...</a><p>Parallelizable and efficient method for producing high quality pseudo random bits.<p>Designed to achieve maximal period (with 500 bit state the period of repetition is one less than 2^500).<p>Can be run backwards or forwards. Running it backwards undoes running it forwards and reproduces the pseudo random bits in reverse order.
NTDF9over 8 years ago
Tomasulo&#x27;s algorithm: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Tomasulo_algorithm" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Tomasulo_algorithm</a><p>A modified version of it is used in most CPU register out-of-order execution
gjvcover 8 years ago
Great question. Without a doubt, my favourite is the Burrows-Wheeler transform (as used by bzip2)<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Burrows%E2%80%93Wheeler_transform" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Burrows%E2%80%93Wheeler_transf...</a><p><a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=4n7NPk5lwbI" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=4n7NPk5lwbI</a>
akkartikover 8 years ago
When I first read of HashLife I had the most extraordinarily vivid dreams of breaking all complexity barriers just by finding the right memoization scheme: <a href="http:&#x2F;&#x2F;www.drdobbs.com&#x2F;jvm&#x2F;an-algorithm-for-compressing-space-and-t&#x2F;184406478" rel="nofollow">http:&#x2F;&#x2F;www.drdobbs.com&#x2F;jvm&#x2F;an-algorithm-for-compressing-spac...</a>
ryandrakeover 8 years ago
R* tree for indexing spatial data [1]. Also, if you enjoy spatial data structures, you can&#x27;t go wrong with Samet&#x27;s[2] _ The Design and Analysis of Spatial Data Structures_ and Applications of Spatial Data Structures_.<p>1: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;R*_tree" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;R*_tree</a><p>2: <a href="https:&#x2F;&#x2F;www.cs.umd.edu&#x2F;~hjs&#x2F;design.html" rel="nofollow">https:&#x2F;&#x2F;www.cs.umd.edu&#x2F;~hjs&#x2F;design.html</a>
pedrorijo91over 8 years ago
Randomized algorithms, specifically Monte Carlo (probably because it was the first I explored), have fascinated me since I met them. The idea of having a very simple&#x2F;efficient method to look for a solution, instead of a very complicated one, and still be able to have a near optimal solution is amazing.<p>(<a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Monte_Carlo_algorithm" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Monte_Carlo_algorithm</a>)
bananicornover 8 years ago
I really like Fleury&#x27;s algorithm[0] - I used it to generate more efficient paths to draw on a canvas, for the purpose of displaying simple wireframe models. It may not be the most efficient, but I really like the simplicity.<p>[0] <a href="http:&#x2F;&#x2F;www.geeksforgeeks.org&#x2F;fleurys-algorithm-for-printing-eulerian-path&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.geeksforgeeks.org&#x2F;fleurys-algorithm-for-printing-...</a>
110011over 8 years ago
There are a lot of great answers here. Let me add another one I really love: the Knuth Morris Pratt algorithm for string matching. There are many features about it I especially love. First of all it takes a simple idea (when a mismatch occurs it&#x27;s often possible to shift the pattern a lot more than just by one position) and takes it to it&#x27;s logical conclusion (that of defining and computing the so called border lengths of prefixes). Second, the analysis of the precomputation step (the border lengths) is tricky, and amortised, but plainly obvious once you see it.<p>It shows the importance of making a good definition and thinking differently (for the run time analysis I thought for a while and failed to come up with an argument why it runs in linear time, but once you see it differently it&#x27;s obvious :))<p>I only recently had the chance to understand this algorithm. Here&#x27;s a little (annotated) gist I made after understanding it: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;quantumelixir&#x2F;3c410dd4a0afe0f2514e0f554cddab05" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;quantumelixir&#x2F;3c410dd4a0afe0f2514e0f...</a>
pmalyninover 8 years ago
Number Theoretic Transform aka Discrete Fourier Transform.<p>The algorithms for computing it are beautiful and have enabled much of the technology we see around us today.
评论 #13526545 未加载
haidraliover 8 years ago
I really like majority voting algorithm to get majority number in array which appears more then half of the elements, Its the most simplest and elegant algorithm i have studied<p><a href="http:&#x2F;&#x2F;www.geeksforgeeks.org&#x2F;majority-element&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.geeksforgeeks.org&#x2F;majority-element&#x2F;</a>
评论 #13519310 未加载
drewmateover 8 years ago
Ford-Fulkerson algorithm for finding the maximum flow across a graph: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Ford%E2%80%93Fulkerson_algorithm" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Ford%E2%80%93Fulkerson_algorit...</a><p>The algorithm itself is interesting enough, but when applied to bipartite graphs, you can solve some tough problems very efficiently. For instance, one of my favorites is how Santa could possibly match a million kids with a million different gifts in order to maximize happiness. It turns out you structure the problem as a bipartite graph with nodes on one side for gifts and children on the other, and Ford-Fulkerson can guarantee the best matching in polynomial time (no small feat, given how many possible combinations there are!)
syntaxingover 8 years ago
I&#x27;ve been diving into a bunch of the newer (computational) optimization algorithms but the old school ones that they teach in school is still extremely interesting and relevant to me. I&#x27;ve always been a huge fan of &quot;Combinatorial optimization&quot; [1]. I saw someone else mention this but the simplex algorithm is awesome as well! [2]<p>[1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Combinatorial_optimization" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Combinatorial_optimization</a> [2] <a href="http:&#x2F;&#x2F;gizmodo.com&#x2F;5934150&#x2F;the-algorithm-that-controls-your-life" rel="nofollow">http:&#x2F;&#x2F;gizmodo.com&#x2F;5934150&#x2F;the-algorithm-that-controls-your-...</a>
deepnotderpover 8 years ago
Also, fft gets an obligatory mention
评论 #13519378 未加载
jones1618over 8 years ago
Cuckoo Hashing: <a href="http:&#x2F;&#x2F;www.lkozma.net&#x2F;cuckoo_hashing_visualization&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.lkozma.net&#x2F;cuckoo_hashing_visualization&#x2F;</a><p>&quot;An elegant method for resolving collisions in hash tables... It has the rare distinction of being easy to implement and efficient both in theory and practice.&quot;<p>It does an amazing job of packing values into N hash tables so there&#x27;s very little dead space but lookups are very fast.
ww520over 8 years ago
<a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Extendible_hashing" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Extendible_hashing</a>.
sAbakumoffover 8 years ago
1. Karatsuba algorithm for fast multiplication. 2. Heap&#x27;s algorithm for generating permutations.<p>mostly because they clearly demo the power of human brain.
评论 #13519508 未加载
jettiover 8 years ago
My favorite is the Hindley-Milner type system algorithm: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Hindley%E2%80%93Milner_type_system#Towards_an_algorithm" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Hindley%E2%80%93Milner_type_sy...</a><p>It is what is used in ML based languages to infer type and apparently Haskell uses it a bit too
taericover 8 years ago
Knuth&#x27;s DLX algorithm. Uses what he calls dancing links to perform backtracking in basically constant space with a clever trick on doubly linked lists.<p>He recently updated his implementation. Is very fun to read his notes on his implementation. In particular, he discusses things he had wrong.<p>If you like poor JavaScript implementations, I can post mine again.
Mr_Pover 8 years ago
Simplex. If you can transform your NP hard optimization problem into an LP, simplex can often work like magic.
评论 #13519533 未加载
santaclausover 8 years ago
Kalman filter!
notforgotover 8 years ago
It looks like SWIM scales better than Raft. Are there well-known production systems that use it?
评论 #13519235 未加载
评论 #13519034 未加载
rurbanover 8 years ago
Cheney&#x27;s two finger garbage collector with forwarding pointers. Still beating every other GC out there by factor of 10, if you can afford 2x memory
useanaliasover 8 years ago
Contrastive divergence for RBMs. Incredibly simple, but absolutely beautiful how it modifies the energy surface. Also, just in general a cool use of Gibbs sampling.
nojvekover 8 years ago
Convolutional neural nets and backprop. That stuff is magical.<p>I also was fascinated as shit when I learnt about minimax and alpha beta search.<p>Game theory and neural nets have always fascinated me.
kovrikover 8 years ago
HyperLogLog
评论 #13519529 未加载
wfunctionover 8 years ago
I find the DC3&#x2F;skew algorithm for linear-time suffix array construction mindblowingly clever!
pizzaover 8 years ago
AIXI
评论 #13519461 未加载
wwwhatcrackover 8 years ago
I generally appreciate divide and conquer algorithms of any type
jakeoghover 8 years ago
Lamport&#x27;s bakery algorithm is pretty neat.
CalChrisover 8 years ago
Someone already mentioned Tomosulo.<p>RSA.
deepnotderpover 8 years ago
Backpropagation
chaotic-goodover 8 years ago
Sequitur