Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

imaonemanarmy's blog

By imaonemanarmy, history, 6 years ago, In Russian

I understand how it works. But the question is why it works. This data structure is based on string metrics, it can use the Levenstein distance metric, for example. Here's the article talking about this: http://blog.notdot.net/2007/4/Damn-Cool-Algorithms-Part-1-BK-Trees

My question is this. The author of the article says: "Call the resultant distance d. Because we know the triangle inequality holds, all our results must have at most distance d + n and at least distance d - n from test."

I don't understand why the search segment is [d - n, d + n].

I'd really appreciate if someone explained.

I know that from the triangle inequality we have:

a + b ≥ c
a + c ≥ b → c ≥ b - a
b + c ≥ a → c ≥ a - b

Combining the last 2, we get

c ≥ |a - b|

And then get this:

|a - b| ≤ c ≤ a + b

But I don't understand what to associate a, b, c with. And why does the author say that we need to search in the segment [d - N, d + N] instead of [|d - N|, d + N].

  • Vote: I like it
  • -11
  • Vote: I do not like it

| Write comment?