BK-tree explained

A BK-tree is a metric tree suggested by Walter Austin Burkhard and Robert M. Keller specifically adapted to discrete metric spaces.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 a dictionary.

Example

This picture depicts the BK-tree for the set

W

of words obtained by using the Levenshtein distance

u

is labeled by a string of

wu\inW

;

(u,v)

is labeled by

duv=d(wu,wv)

where

wu

denotes the word assigned to

u

.

The BK-tree is built so that:

u

of the BK-tree, the weight assigned to its egress arcs are distinct;

e=(u,v)

labeled by

k

, each descendant

v'

of

v

satisfies the following equation:

d(wu,wv')=k

:

Insertion

The insertion primitive is used to populate a BK-tree

t

according to a discrete metric

d

.

Input:

t

: the BK-tree;

duv

denotes the weight assigned to an arc

(u,v)

;

wu

denotes word assigned to a node

u

);

d

: the discrete metric used by

t

(e.g. the Levenshtein distance);

w

: the element to be inserted into

t

;

Output:

t

corresponding to

w

Algorithm:

t

is empty:

r

in

t

wr\leftarroww

r

u

to the root of

t

u

exists:

k\leftarrowd(wu,w)

k=0

:

u

v

the child of

u

such that

duv=k

v

is not found:

v

wv\leftarroww

(u,v)

duv\leftarrowk

v

u\leftarrowv

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);

w

: the searched element;

dmax

: the maximum distance allowed between the best match and

w

, defaults to

+infty

;

Output:

wbest

: the closest element to

w

stored in

t

and according to

d

or

\perp

if not found;

Algorithm:

t

is empty:

\perp

S

a set of nodes to process, and insert the root of

t

into

S

.

(wbest,dbest)\leftarrow(\perp,dmax)

S\ne\emptyset

:

u

from

S

du\leftarrowd(w,wu)

du<dbest

:

(wbest,dbest)\leftarrow(wu,du)

(u,v)

:

|duv-du|<dbest

: (cut-off criterion)

v

into

S

.

wbest

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

wu

="book". Further

du=2

since the distance from "book" to "cool" is 2, and

dbest=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

dbest=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

dbest=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

du=3

, the distance from "cool" to "books." As

du>dbest

,

dbest

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

du=2

, the distance from "cool" to "boo." This again does not improve upon

dbest=2

. Each outgoing arc from "boo" is now considered; the arc from "boo" to "boon" has weight 1, and since

|2-1|=1<dbest=2

, "boon" is added to

S

. Similarly, since

|2-2|=0<dbest

, "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

dbest

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

wbest

with

dbest=1

.

See also

References

External links