18.04.2013 Views

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

War Story: What's Past is Prolog<br />

Figure: Two different tries for the given set of rule heads<br />

``In order to speed up unification, we want to preprocess the set of rule heads so that we can quickly<br />

determine which rules match a given goal. We must organize the rules in a trie data structure for fast<br />

unification.''<br />

Tries are extremely useful data structures in working with strings, as discussed in Section . Every leaf<br />

of the trie represents one string. Each node on the path from root to leaf is labeled with exactly one<br />

character of the string, with the ith node of the path corresponding to the ith character of the string.<br />

``I agree. A trie is a natural way to represent your rule heads. Building a trie on a set of strings of<br />

characters is straightforward - just insert the strings one after another starting from the root. So what is<br />

your problem?'' I asked.<br />

``<strong>The</strong> efficiency of our unification algorithm depends very much on minimizing the number of edges in<br />

the trie. Since we know all the rules in advance, we have the freedom to reorder the character positions in<br />

the rules. Instead of the root node always representing the first argument in the rule, we can choose to<br />

have it represent the third argument. We would like to use this freedom to build a minimum-size trie for<br />

the set of rules.''<br />

He showed me the example in Figure . A trie constructed according to the original string position<br />

order (1, 2, 3) uses a total of 12 edges. However, by permuting the character order to (2, 3, 1), we can<br />

obtain a trie with only 8 edges.<br />

``Why does the speed of unification depend on minimizing the number of edges in the trie?''<br />

``An open goal is one with all distinct variables in its arguments, like p(X,Y,Z). Open goals match<br />

everything. In unifying an open goal against a set of clause heads, each symbol in all the clause heads<br />

will be bound to some variable. By doing a depth-first traversal of this minimum-edge trie, we minimize<br />

file:///E|/BOOK/BOOK2/NODE51.HTM (2 of 4) [19/1/2003 1:28:54]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!