5 ms·
At this point, everyone knows about bloom filters simply because it’s the thing to write about any time someone asks “lesser known data structures/algorithms?”
by ComputerGuru 4y ago
At this point, everyone knows about bloom filters simply because it’s the thing to write about any time someone asks “lesser known data structures/algorithms?” - it’s hard to find a list that doesn’t feature them!
Still some good entries in that list. I would add skip lists, not because they’re structurally anything special but because they open your mind to the possibly entirely-new-to-the-reader world of probabilistic data structures.
As someone that’s been around the block, something I’ve learned to be less surprised by is how often simply adding an element of randomness to a difficult problem can sometimes give you results comparable to a 10- or 100-times more complicated solution, with the advantage of being more maintainable, making better use of the i$, and avoiding a lot of work. (Eg compare a cache dropping randomly selected items to LFU or LRU caches.)
- Jensson 4y agoRandomness is very useful to handle edge cases, quicksort is the poster child here, it has some edge cases were it runs extremely slowly but randomising how you apply the algorithm means that you can ignore them since they are so unlikely. If you try to use deterministic code then those rare edge cases often comes up in the real world.
- PartiallyTyped 4y ago> I’ve learned to be less surprised by is how often simply adding an element of randomness to a difficult problem can sometimes give you results comparable to a 10- or 100-times more complicated solution We tend to use "Las Vegas" and "Monte Carlo" to classify randomised algorithms, where the former means using randomness to get a speedup, and the latter means using randomness to get an approximately correct solution. An example of the former is QuickSort, where we shuffle the elements, giving the Expected nlogn time complexity* . An example of the latter is bloom filters. Other examples are found all over approximation algorithms like taking random cuts off graphs. * Assuming something like Yates shuffling with O(N) complexity is used.
- chii 4y agoand once in a while, the shuffle puts the elements in a pattern that makes quicksort perform worst case (n^2). But i guess that is rare, the larger the list! So in practice, it works.
- PartiallyTyped 4y agoThe larger the list it gets exponentially less likely that this occurs if the shuffling occurs at every iteration. We have 2/(n!) probability that the shuffle sorts the list either way. The probability that it happens on the second level is 2/((n-1)!). So the probability that it happens twice is 4/(1^2 * 2^2 … (n-1)^2 * n) Which ends up being prod_{i=1} ^N i ^ i It’s so astronomically unlikely, that it just never does.
- pfdietz 4y agoAnother place where randomness comes in handy is testing. Suppose you partition your test input set into N different partitions of equal size, and you want, for each partition, to have a test input from that partition. If you just generate random tests then on average after generating N log N tests you will have hit each partition at least once. And this is true not just of the partitions you choose, but ANY N partitions of equal size.
- chii 4y ago> Eg compare a cache dropping randomly selected items to LFU or LRU caches I think it's not that randomly (no pun intended) adding randomness to an existing algorithm or solution could improve it, but that sometimes a random sampling process mimics the underlying problem. For caching, the assumption that LRU item should be ejected is that - an assumption. If you checked this assumption and found that it was wrong, but that instead the access patterns are random, then dropping random items _do_ mimic the problem domain, and that makes it a better solution!
- ComputerGuru 4y agoIt’s not quite that simple. It’s also probabilistically arriving at the asymptote of the ideal solution for many cases. If an item is accessed often, it’ll be re-inserted quickly when it is accidentally removed but only at risk of eviction at a rate of 1/n. On average, the items you use often will be in the cache and the items you don’t, won’t. It has nothing to do with specifically actually random access pattern. Because you access it a lot, the fact that it got evicted one of those n accesses is completely amortized by the other accesses (even more so when you take into account that you never evict the item that you are currently accessing). The solution fails for specific non-random access scenarios. Eg you access an item exactly every n-1 accesses: in an LRU, it’ll always be in the cache but in a probabilistic eviction scenario, it’s most likely not going to be in there.