Autocomplete using Tries
dibaiee.ir
dibaiee.ir
The problem is that a trie is a very cache-loose data structure. Every node is connected to every other by pointers, and they may be spread all over main memory. Finding the first node usually involves chasing several pointers; enumerating each successive one involves chasing several more. Meanwhile, if you just had a sorted array, all subsequent results would be on the same page at least, and many would be on the same cache line.
YMMV, based on your data's size and shape. Sorted arrays usually work best when the full data set is small and dense (eg. stock tickers), while tries may work better for sparse data (eg. sentences typed in by users). Always benchmark before committing to an implementation.
That being said, it really depends on the problem at hand and the overall workload of the database.
I'm not aiming for speed in this tutorial, it's just a tutorial to teach how Tries work, I knew arrays are faster in this kind of situation, even an array.filter can do the job, so you're right; but Tries are more flexible in my opinion, they can easily be customized for any use.
Anyway, I always like it when I get criticized, I learn something, thank you nostrademons!
A common approach to solve this is to store in each internal node the maximum score among all its descendants, and sort the children of each node by maximum score. This way by doing a best-first search with a heap you can efficiently retrieve the top-k completions by score.
If you then have millions of strings, it becomes tricky to do this in a space-efficient and cache-efficient way. I wrote a paper a few years ago with different space-time tradeoffs: http://www.di.unipi.it/~ottavian/files/topk_completion_www13...
http://blog.faroo.com/2012/06/07/improved-edit-distance-base...
By "constrained CLI", I mean the kind that controls a router or other piece of equipment which has a well-defined (um, somewhat) syntax, as opposed to the somewhat more free-form aspect of the shell.
I ask, because one of my jobs had me occasionally working in the CLI declarations for a type of router. The cli commands could be in the form "interface foo enable", where "interface" could be shortened its shortest non-ambiguous form ("int" IIRC), and "foo" was a string corresponding to one of the interfaces on the system. Pressing TAB after "int" would automatically suggest the list of interfaces that exist on the system. This command had an opposite in the form "no interface foo enable" (yeah, prefixing "no" instead of postfixing "disable", though to be fair that may have been possible as well). A declaration of such a syntax was done with something along the line of:
node(id=interface, type=keyword, keyword=interface, no_prefix=yes, next=int_name);
node(id=int_name, type=string, next=enable);
node(id=enable, type=keyword, keyword=enable);
(with several extra bells and whistles to account for the int_name string referencing the existing interfaces on the system and to indicate that the command was only complete with "enable" at the end).There were hundreds of declaration files containing lines like that, it was parsed by a humongous Perl script that output a 7MB source file containing the data structures fed to libinput. It was easily the slowest part of our compilation process, and Emacs would choke on that 7MB sourcefile.
It was terrible, but I never found examples of how to do it better. Anyone have any experience?
Do you have any speed comparisons vs traditional autocomplete methods?