Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Also look at whether you can solve your problem with a different algorithm, one that is more friendly to external storage.

One classic example is sorting. When your data is too big for RAM, quicksort's performance is horrible and mergesort's performance is fine (even if your data is on magnetic tape).

Another classic example is taking a situation where you build a hash (or dictionary) and using sorted lists instead.

Let's say your task is to take some text and put <b></b> tags around a word but only the first time it occurs. The obvious solution is to scan through the text, building a hash as you encounter words so you can check if you've seen it before. Great until your hash doesn't fit in RAM.

The sort-based solution is to scan through the input file, break it into words, and output { word, byte_offset } tuples into a file. Then sort that file by word using a stable sort (or both fields as sort key). Now that all occurrences of each word are grouped together, make a pass through the sorted data and flag the first occurrence of each word, generating { byte_offset, word, is_first_occurrence } tuples. Then sort that by byte_offset. Finally you can make another pass through your input text and basically merge it with your sorted temp file, and check the is_first_occurrence flags. All of this uses O(1) RAM.

I believe this is basically what databases do with merge joins, but the point is you can apply this general type of thinking to your own programs as well.



I believe this is a problem that CS has already solved: http://dimacs.rutgers.edu/~graham/pubs/papers/cmsoft.pdf

The Count-Min Sketch is a data structure consisting of a fixed array of counters.

This is a short and easily accessible paper, which isn't very heavy on math or obscure notation or concepts, so I would recommend it to anyone with even a cursory interest in the subject.

Here's an implementation in Python: https://github.com/barrust/count-min-sketch/blob/master/pyth...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: