Algorithm for autocomplete?

For (heh) awesome fuzzy/partial string matching algorithms, check out Damn Cool Algorithms:

These don’t replace tries, but rather prevent brute-force lookups in tries – which is still a huge win. Next, you probably want a way to bound the size of the trie:

  • keep a trie of recent/top N words used globally;
  • for each user, keep a trie of recent/top N words for that user.

Finally, you want to prevent lookups whenever possible…

  • cache lookup results: if the user clicks through on any search results, you can serve those very quickly and then asynchronously fetch the full partial/fuzzy lookup.
  • precompute lookup results: if the user has typed “appl”, they are likely to continue with “apple”, “apply”.
  • prefetch data: for instance, a web app can send a smaller set of results to the browser, small enough to make brute-force searching in JS viable.

Leave a Comment