BK-tree

From HandWiki

A BK-tree is a metric tree suggested by Walter Austin Burkhard and Robert M. Keller[1] specifically adapted to discrete metric spaces. For simplicity, consider integer discrete metric [math]\displaystyle{ d(x,y) }[/math]. 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 [math]\displaystyle{ d(a,b) = k }[/math]. BK-trees can be used for approximate string matching in a dictionary.[2][example needed]

Example

An example of BK-tree

This picture depicts the BK-tree for the set [math]\displaystyle{ W }[/math] of words {"book", "books", "cake", "boo", "boon", "cook", "cake", "cape", "cart"} obtained by using the Levenshtein distance

  • each node [math]\displaystyle{ u }[/math] is labeled by a string of [math]\displaystyle{ w_u \in W }[/math];
  • each arc [math]\displaystyle{ (u,v) }[/math] is labeled by [math]\displaystyle{ d_{uv} = d(w_u,w_v) }[/math] where [math]\displaystyle{ w_u }[/math] denotes the word assigned to [math]\displaystyle{ u }[/math].

The BK-tree is built so that:

  • for all node [math]\displaystyle{ u }[/math] of the BK-tree, the weight assigned to its egress arcs are distinct;
  • for all arc [math]\displaystyle{ e=(u,v) }[/math] labeled by [math]\displaystyle{ k }[/math], each descendant [math]\displaystyle{ v' }[/math] of [math]\displaystyle{ v }[/math] satisfies the following equation: [math]\displaystyle{ d(w_u, w_{v'}) = k }[/math]:
    • Example 1: Consider the arc from "book" to "books". The distance between "book" and any word in {"books", "boo", "boon", "cook"} is equal to 1;
    • Example 2: Consider the arc from "books" to "boo". The distance between "books" and any word in {"boo", "boon", "cook"} is equal to 2.

Insertion

The insertion primitive is used to populate a BK-tree [math]\displaystyle{ t }[/math] according to a discrete metric [math]\displaystyle{ d }[/math].

Input:

  • [math]\displaystyle{ t }[/math]: the BK-tree;
    • [math]\displaystyle{ d_{uv} }[/math] denotes the weight assigned to an arc [math]\displaystyle{ (u, v) }[/math];
    • [math]\displaystyle{ w_u }[/math] denotes word assigned to a node [math]\displaystyle{ u }[/math]);
  • [math]\displaystyle{ d }[/math]: the discrete metric used by [math]\displaystyle{ t }[/math] (e.g. the Levenshtein distance);
  • [math]\displaystyle{ w }[/math]: the element to be inserted into [math]\displaystyle{ t }[/math];

Output:

  • The node of [math]\displaystyle{ t }[/math] corresponding to [math]\displaystyle{ w }[/math]

Algorithm:

  • If the [math]\displaystyle{ t }[/math] is empty:
    • Create a root node [math]\displaystyle{ r }[/math] in [math]\displaystyle{ t }[/math]
    • [math]\displaystyle{ w_r \leftarrow w }[/math]
    • Return [math]\displaystyle{ r }[/math]
  • Set [math]\displaystyle{ u }[/math] to the root of [math]\displaystyle{ t }[/math]
  • While [math]\displaystyle{ u }[/math] exists:
    • [math]\displaystyle{ k \leftarrow d(w_u, w) }[/math]
    • If [math]\displaystyle{ k = 0 }[/math]:
      • Return [math]\displaystyle{ u }[/math]
    • Find [math]\displaystyle{ v }[/math] the child of [math]\displaystyle{ u }[/math] such that [math]\displaystyle{ d_{uv} = k }[/math]
    • If [math]\displaystyle{ v }[/math] is not found:
      • Create the node [math]\displaystyle{ v }[/math]
      • [math]\displaystyle{ w_v \leftarrow w }[/math]
      • Create the arc [math]\displaystyle{ (u, v) }[/math]
      • [math]\displaystyle{ d_{uv} \leftarrow k }[/math]
      • Return [math]\displaystyle{ v }[/math]
    • [math]\displaystyle{ u \leftarrow v }[/math]

Lookup

Given a searched element [math]\displaystyle{ w }[/math], the lookup primitive traverses the BK-tree to find the closest element of [math]\displaystyle{ w }[/math]. The key idea is to restrict the exploration of [math]\displaystyle{ t }[/math] 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:

  • [math]\displaystyle{ t }[/math]: the BK-tree;
  • [math]\displaystyle{ d }[/math]: the corresponding discrete metric (e.g. the Levenshtein distance);
  • [math]\displaystyle{ w }[/math]: the searched element;
  • [math]\displaystyle{ d_{max} }[/math]: the maximum distance allowed between the best match and [math]\displaystyle{ w }[/math], defaults to [math]\displaystyle{ +\infty }[/math];

Output:

  • [math]\displaystyle{ w_{best} }[/math]: the closest element to [math]\displaystyle{ w }[/math] stored in [math]\displaystyle{ t }[/math] and according to [math]\displaystyle{ d }[/math] or [math]\displaystyle{ \perp }[/math] if not found;

Algorithm:

  • If [math]\displaystyle{ t }[/math] is empty:
    • Return [math]\displaystyle{ \perp }[/math]
  • Create [math]\displaystyle{ S }[/math] a set of nodes to process, and insert the root of [math]\displaystyle{ t }[/math] into [math]\displaystyle{ S }[/math].
  • [math]\displaystyle{ (w_{best}, d_{best}) \leftarrow (\perp, d_{max}) }[/math]
  • While [math]\displaystyle{ S \ne \emptyset }[/math]:
    • Pop an arbitrary node [math]\displaystyle{ u }[/math] from [math]\displaystyle{ S }[/math]
    • [math]\displaystyle{ d_u \leftarrow d(w, w_u) }[/math]
    • If [math]\displaystyle{ d_u \lt d_{best} }[/math]:
      • [math]\displaystyle{ (w_{best}, d_{best}) \leftarrow (w_u, d_u) }[/math]
    • For each egress-arc [math]\displaystyle{ (u, v) }[/math]:
      • If [math]\displaystyle{ |d_{uv} - d_u| \lt d_{best} }[/math]: (cut-off criterion)
        • Insert [math]\displaystyle{ v }[/math] into [math]\displaystyle{ S }[/math].
  • Return [math]\displaystyle{ w_{best} }[/math]

Example of the lookup algorithm

Consider the example 8-node B-K Tree shown above and set [math]\displaystyle{ w= }[/math]"cool". [math]\displaystyle{ S }[/math] is initialized to contain the root of the tree, which is subsequently popped as the first value of [math]\displaystyle{ u }[/math] with [math]\displaystyle{ w_u }[/math]="book". Further [math]\displaystyle{ d_u=2 }[/math] since the distance from "book" to "cool" is 2, and [math]\displaystyle{ d_{best}=2 }[/math] 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 [math]\displaystyle{ |1-2|=1 }[/math] is less than [math]\displaystyle{ d_{best}=2 }[/math], the node containing "books" is inserted into [math]\displaystyle{ S }[/math] for further processing. The next arc, from "book" to "cake," has weight 4, and since [math]\displaystyle{ |4-2|=2 }[/math] is not less than [math]\displaystyle{ d_{best}=2 }[/math], the node containing "cake" is not inserted into [math]\displaystyle{ S }[/math]. 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 [math]\displaystyle{ c }[/math] 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 [math]\displaystyle{ c }[/math] (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 [math]\displaystyle{ S }[/math] and now [math]\displaystyle{ d_u=3 }[/math], the distance from "cool" to "books." As [math]\displaystyle{ d_u \gt d_{best} }[/math], [math]\displaystyle{ d_{best} }[/math] remains set at 2 and the single outgoing arc from the node containing "books" is considered. Next, the node containing "boo" is popped from [math]\displaystyle{ S }[/math] and [math]\displaystyle{ d_u=2 }[/math], the distance from "cool" to "boo." This again does not improve upon [math]\displaystyle{ d_{best} = 2 }[/math]. Each outgoing arc from "boo" is now considered; the arc from "boo" to "boon" has weight 1, and since [math]\displaystyle{ |2-1|=1 \lt d_{best}=2 }[/math], "boon" is added to [math]\displaystyle{ S }[/math]. Similarly, since [math]\displaystyle{ |2-2|=0 \lt d_{best} }[/math], "cook" is also added to [math]\displaystyle{ S }[/math].

Finally each of the two last elements in [math]\displaystyle{ S }[/math] are considered in arbitrary order: suppose the node containing "cook" is popped first, improving [math]\displaystyle{ d_{best} }[/math] 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 [math]\displaystyle{ w_{best} }[/math] with [math]\displaystyle{ d_{best}=1 }[/math].

See also

References

External links

  • A BK-tree implementation in Common Lisp with test results and performance graphs.
  • An explanation of BK-Trees and their relationship to metric spaces [3]
  • An explanation of BK-Trees with an implementation in C# [4]
  • A BK-tree implementation in Lua [5]
  • A BK-tree implementation in Python [6]