Live data from Hacker News

Ask HN: What are your favorite algorithms?

news.ycombinator.com

31–40 of 93 posts

Re: Ask HN: What are your favorite algorithms?

#32

The Fastest and Shortest Algorithm for All Well-Defined Problems: https://arxiv.org/abs/cs/0206022 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,…

I vote twice for this one!

Re: Ask HN: What are your favorite algorithms?

#33
post #29

As basic as it is, and completely uninventive, I've always loved Dijkstra's algorithm. 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.

Why "uninventive?"

I believe GP meant that picking Dijkstra's is an uninventive answer to the question of a favorite algorithm, since it's very well-known.

Re: Ask HN: What are your favorite algorithms?

#34
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's a 9-shaped linked list? You'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've found a duplicate.

There's a way to do it in O(1) space, though.

If you start off two runners in the list, and each "step" move one twice and the other only once, if there's a loop they will eventually run into each other and be processing the same element. Simple, elegant, and nothing I'd have ever thought of. :)

Re: Ask HN: What are your favorite algorithms?

#35
post #34

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's a 9-shaped linked list? You'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've found a dupl…

Well... that's rather Ingenious.

Re: Ask HN: What are your favorite algorithms?

#37
post #34

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's a 9-shaped linked list? You'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've found a dupl…

This can also be used to determine the period of Linear Congruential PRNGs, i.e those in form of

    X(n+1) = (A * X(n) + B) mod C
which happens to be what many stdlib versions of rand() are.

https://en.wikipedia.org/wiki/Linear_congruential_generator

Re: Ask HN: What are your favorite algorithms?

#38
post #34

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's a 9-shaped linked list? You'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've found a dupl…

This algorithm is popularly known as "Floyd's tortoise and the hare algorithm".

Re: Ask HN: What are your favorite algorithms?

#39

It looks like SWIM scales better than Raft. Are there well-known production systems that use it?

SWIM is in no way an alternative to Raft, and vice-versa. SWIM is a membership algorithm, Raft is a consensus algorithm. They are often used together. (a SWIM alternative is plain Gossip and a Raft alternative is Paxos)
Post reply on HN