Live data from Hacker News

Ask HN: What are your favorite algorithms?

news.ycombinator.com

51–60 of 93 posts

Re: Ask HN: What are your favorite algorithms?

#51
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…

I like my solution better:

The list node contains a pointer and some data, most likely another pointer, or a primitive type, or a struct of primitive types. Which means, that on all modern systems, a list node is aligned to at least 16 bits, much more likely 32 bits. That means that the pointer to the node always has the last bit set to zero. So:

    bool containsCycle(node_t *root)
    {
        node_t *p = root;
        bool ret = false;
        if (!root) return false; // Empty list

        while (p->next && !ret)
        {
            if ((uintptr_t)p->next & 1)
            {
                ret = true;
                break;
            }
            node_t *q = p->next;
            p->next = (uintptr_t)p->next | 1;
            p = q;
        }
    
        // Reset all pointers
        p = root;
        while (p->next && ((uintptr_t)p->next & 1))
        {
            p->next = (uintptr_t)p->next & ~1;
            p = p->next;
        }
        return ret;
    }
:)

Re: Ask HN: What are your favorite algorithms?

#53
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's neat.

[1] https://en.wikipedia.org/wiki/LZ77_and_LZ78 [2] https://en.wikipedia.org/wiki/Rsync#Algorithm [3] https://en.wikipedia.org/wiki/Gift_wrapping_algorithm [4] https://en.wikipedia.org/wiki/XOR_linked_list

Re: Ask HN: What are your favorite algorithms?

#54
Ford-Fulkerson algorithm for finding the maximum flow across a graph: https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorit...

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!)

Re: Ask HN: What are your favorite algorithms?

#55

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,…

It is only an existential proof, the algorithm has never been implemented. The closest that comes to it is the proof searcher in Isabelle. (blast method)

Re: Ask HN: What are your favorite algorithms?

#56
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's often possible to shift the pattern a lot more than just by one position) and takes it to it'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.

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's obvious :))

I only recently had the chance to understand this algorithm. Here's a little (annotated) gist I made after understanding it: https://gist.github.com/quantumelixir/3c410dd4a0afe0f2514e0f...

Re: Ask HN: What are your favorite algorithms?

#58
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…

I like my solution better: The list node contains a pointer and some data, most likely another pointer, or a primitive type, or a struct of primitive types. Which means, that on all modern systems, a list node is aligned to at least 16 bits, much more likely 32 bits. That means that the pointer to the node always has the last bit set to zero. So: bool containsCycle(node_t *root) { node_t *p = root; bool ret = false;…

An implementation-specific hack. Neat, but hack nonetheless.

Re: Ask HN: What are your favorite algorithms?

#59
Here's a rather surprising one. There'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.

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.

Re: Ask HN: What are your favorite algorithms?

#60

1. Karatsuba algorithm for fast multiplication. 2. Heap's algorithm for generating permutations. mostly because they clearly demo the power of human brain.

I recently made the observation that Karatsuba found his multiplication algorithm roughly ten years ahead of Strassen's matrix multiplication alg. And given that the crucial idea (using identities to save one multiplication) is already beautifully captured in Karatsuba's it takes away a little from the latter :)
Post reply on HN