HOME

TheInfoList



OR:

A BK-tree is a
metric tree A metric tree is any tree data structure specialized to index data in metric spaces. Metric trees exploit properties of metric spaces such as the triangle inequality to make accesses to the data more efficient. Examples include the M-tree, vp-t ...
suggested by Walter Austin Burkhard and Robert M. Keller specifically adapted to discrete
metric space In mathematics, a metric space is a set together with a notion of ''distance'' between its elements, usually called points. The distance is measured by a function called a metric or distance function. Metric spaces are the most general settin ...
s. For simplicity, consider integer discrete metric d(x,y). Then, BK-tree is defined in the following way. An arbitrary element ''a'' is selected as root node. The root node may have zero or more subtrees. The ''k-th'' subtree is recursively built of all elements ''b'' such that d(a,b) = k. BK-trees can be used for
approximate string matching In computer science, approximate string matching (often colloquially referred to as fuzzy string searching) is the technique of finding strings that match a pattern approximately (rather than exactly). The problem of approximate string matching i ...
in a dictionary.


Example

This picture depicts the BK-tree for the set W of words obtained by using the
Levenshtein distance In information theory, linguistics, and computer science, the Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is the minimum number of single-charact ...
* each node u is labeled by a string of w_u \in W; * each
arc ARC may refer to: Business * Aircraft Radio Corporation, a major avionics manufacturer from the 1920s to the '50s * Airlines Reporting Corporation, an airline-owned company that provides ticket distribution, reporting, and settlement services * ...
(u,v) is labeled by d_ = d(w_u,w_v) where w_u denotes the word assigned to u. The BK-tree is built so that: * for all node u of the BK-tree, the weight assigned to its egress arcs are distinct; * for all arc e=(u,v) labeled by k, each descendant v' of v satisfies the following equation: d(w_u, w_) = k: ** ''Example 1:'' Consider the arc from "book" to "books". The distance between "book" and any word in is equal to 1; ** ''Example 2:'' Consider the arc from "books" to "boo". The distance between "books" and any word in is equal to 2.


Insertion

The insertion primitive is used to populate a BK-tree t according to a discrete metric d. Input: * t: the BK-tree; ** d_ denotes the weight assigned to an arc (u, v); ** w_u denotes word assigned to a node u); * d: the discrete metric used by t (e.g. the
Levenshtein distance In information theory, linguistics, and computer science, the Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is the minimum number of single-charact ...
); * w: the element to be inserted into t; Output: * The node of t corresponding to w Algorithm: * If the t is empty: ** Create a root node r in t ** w_r \leftarrow w ** Return r * Set u to the root of t * While u exists: ** k \leftarrow d(w_u, w) ** If k = 0: *** Return u ** Find v the child of u such that d_ = k ** If v is not found: *** Create the node v *** w_v \leftarrow w *** Create the arc (u, v) *** d_ \leftarrow k *** Return v ** u \leftarrow v


Lookup

Given a searched element w, the lookup primitive traverses the BK-tree to find the closest element of w. The key idea is to restrict the exploration of t to nodes that can only improve the best candidate found so far by taking advantage of the BK-tree organization and of the triangle inequality (cut-off criterion). Input: * t: the BK-tree; * d: the corresponding discrete metric (e.g. the
Levenshtein distance In information theory, linguistics, and computer science, the Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is the minimum number of single-charact ...
); * w: the searched element; * d_: the maximum distance allowed between the best match and w, defaults to +\infty; Output: * w_: the closest element to w stored in t and according to d or \perp if not found; Algorithm: * If t is empty: ** Return \perp * Create S a set of nodes to process, and insert the root of t into S. * (w_, d_) \leftarrow (\perp, d_) * While S \ne \emptyset: ** Pop an arbitrary node u from S ** d_u \leftarrow d(w, w_u) ** If d_u < d_: *** (w_, d_) \leftarrow (w_u, d_u) ** For each egress-arc (u, v): *** If , d_ - d_u, < d_: (cut-off criterion) **** Insert v into S. * Return w_


Example of the lookup algorithm

Consider the example 8-node B-K Tree shown above and set w="cool". S is initialized to contain the root of the tree, which is subsequently popped as the first value of u with w_u="book". Further d_u=2 since the distance from "book" to "cool" is 2, and d_=2 as this is the best (i.e. smallest) distance found thus far. Next each outgoing arc from the root is considered in turn: the arc from "book" to "books" has weight 1, and since , 1-2, =1 is less than d_=2, the node containing "books" is inserted into S for further processing. The next arc, from "book" to "cake," has weight 4, and since , 4-2, =2 is not less than d_=2, the node containing "cake" is not inserted into S. Therefore, the subtree rooted at "cake" will be pruned from the search, as the word closest to "cool" cannot appear in that subtree. To see why this pruning is correct, notice that a candidate word c appearing in "cake"s subtree having distance less than 2 to "cool" would violate the triangle inequality: the triangle inequality requires that for this set of three numbers (as sides of a triangle), no two can sum to less than the third, but here the distance from "cool" to "book" (which is 2) plus the distance from "cool" to c (which is less than 2) cannot reach or exceed the distance from "book" to "cake" (which is 4). Therefore, it is safe to disregard the entire subtree rooted at "cake". Next the node containing "books" is popped from S and now d_u=3, the distance from "cool" to "books." As d_u > d_, d_ remains set at 2 and the single outgoing arc from the node containing "books" is considered. Next, the node containing "boo" is popped from S and d_u=2, the distance from "cool" to "boo." This again does not improve upon d_ = 2. Each outgoing arc from "boo" is now considered; the arc from "boo" to "boon" has weight 1, and since , 2-1, =1 < d_=2, "boon" is added to S. Similarly, since , 2-2, =0 < d_, "cook" is also added to S. Finally each of the two last elements in S are considered in arbitrary order: suppose the node containing "cook" is popped first, improving d_ to distance 1, then the node containing "boon" is popped last, which has distance 2 from "cool" and therefore does not improve the best result. Finally, "cook" is returned as the answer w_ with d_=1.


See also

*
Levenshtein distance In information theory, linguistics, and computer science, the Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is the minimum number of single-charact ...
– the distance metric commonly used when building a BK-tree *
Damerau–Levenshtein distance In information theory and computer science, the Damerau–Levenshtein distance (named after Frederick J. Damerau and Vladimir I. Levenshtein.) is a string metric for measuring the edit distance between two sequences. Informally, the Damerau–Lev ...
– a modified form of Levenshtein distance that allows transpositions


References

*
W. Burkhard and R. Keller. Some approaches to best-match file searching, CACM, 1973
* R. Baeza-Yates, W. Cunto, U. Manber, and S. Wu. Proximity matching using fixed queries trees. In M. Crochemore and D. Gusfield, editors, 5th Combinatorial Pattern Matching, LNCS 807, pages 198–212, Asilomar, CA, June 1994. *
Ricardo Baeza-Yates and Gonzalo Navarro. Fast Approximate String Matching in a Dictionary. Proc. SPIRE'98


External links

* A BK-tree implementation i
Common Lisp
with test results and performance graphs. * An explanation of BK-Trees and their relationship to metric space

* An explanation of BK-Trees with an implementation in C

* A BK-tree implementation in Lua (programming language), Luabr>
* A BK-tree implementation in Python (programming language), Pythonbr>
{{CS-Trees Trees (data structures)