What is the most space and time efficient way to implement a Trie in Mathematica?
Will it be practically faster than what is natively available in appropriate cases?
|
What is the most space and time efficient way to implement a Trie in Mathematica? Will it be practically faster than what is natively available in appropriate cases? |
||||
| show 1 more comment |
|
A combination of rules and recursion is able to produce rather powerful solutions. Here is my take on it:
The last function serves to collect the words from a tree, by performing a depth-first tree traversal and maintaining the stack of accumulated characters and words. Here is a short example:
I only constructed here a bare-bones tree, so you can only test whether or not the word is there, but not keep any other info. Here is a larger example:
I don't know which approach has been used for the built-in functionality, but the above implementation seems to be generally in the same calss for performance. The slowest part is due to the top-level tree-traversing code in EDIT For a really nice application of a Trie data structure, where it allows us to achieve major speed-up (w.r.t. using |
|||||
|
|
This might not give you the answer you expect, neither is this better than Leonid's solution, but. Since your fairly general question leaves a lot of room for answers and since I felt that it might be relevant, I gave it a go. Assuming, that we have a list of prefix representations of a string (e.g. from here), it can be plotted easily with
Now let's convert it to a graph. First, assign a unique integer to each leaf:
And then building the edge list of the graph by traversing all possible routes with
|
||||
|
|
treeortree-graphtag would be useful, as it would covere some questions like this, would fit nicely betweenlist-manipulationandgraphs-and-networks(though being a subset of the latter) and (as my major reason) would have helped me to find this post earlier. – István Zachar Mar 26 '12 at 12:39