Pergunta

LD = Levenshtein Distância

Apenas fazendo alguns exemplos no papel, isso parece funcionar, mas alguém sabe se isso é sempre verdade?

Vamos dizer que eu tenho 3 cordas

BOT

BOB

BOM

LD (BOT, BOB) = 1

e

LD (BOB, BOM) = 1

então

LD (BOT, BOM) = max (LD (BOT, BOB), LD (BOB, DOM)) = 1

ou

BAAB

BBAB

BCCD

LD (BBAB, BAAB) = 1

e

LD (BBAB, BCCD) = 3

então

LD (BAAB, BCCD) = max (LD (BBAB, BAAB), LD (BBAB, BCCD)) = 3

Eu gostaria de saber se isso sempre se aplica.

Isto é,

LD (B, C) = max (LD (A, C), LD (A, B))


Editar - Adicionado em 2009/10/22 19:08 PST

Eu estou começando a pensar que isso vale para palavras do mesmo comprimento, caso contrário, você ainda pode fazê-lo, mas você tem que adicionar o valor absoluto da diferença no comprimento da palavra.

Em essência LD (B, C) = max (LD (A, C), LD (A, B)) + abs (comprimento (B) -length (C))

Foi útil?

Solução

não funciona.

LD("BOB", "BOT") == 1
LD("BOT", "BOB") == 1

LD("BOB", "BOB") == 0
max(LD("BOB", "BOT"), LD("BOT", "BOB")) == 1

0 != 1

há exemplos provavelmente mais difícil também ...

Outras dicas

Não, mas isso faz:

lev (a, c) <= lev (a, b) + lev (b, c) (a.k.a "triângulo desigualdade)

... e é muito usado como uma heurística pela VP-Árvores e BK-Trees.

Ser uma métrica a distância levenshtein segue a desigualdade triangular:
http://en.wikipedia.org/wiki/Triangle_inequality

Nada é melhor do que um teste. Se você conhece C # executá-lo por isso.

public Int32 CalculateDistance(String x, String y)
{
    Int32 xl = x.Length;
    Int32 yl = y.Length;
    Int32[,] matrix = new Int32[xl + 1, yl + 1];

    for (Int32 i = 0; i <= xl; i++)
    {
        matrix[i, 0] = i;
    }

    for (Int32 i = 0; i <= yl; i++)
    {
        matrix[0, i] = i;
    }

    for (Int32 j = 1; j <= yl; j++)
    {
        for (Int32 i = 1; i <= xl; i++)
        {                   
            if (x[i - 1] == y[j - 1])
            {
                matrix[i, j] = matrix[i - 1, j - 1];
            }
            else                    
            {
                matrix[i, j] = Min((matrix[i - 1, j] + 1), (matrix[i, j - 1] + 1), (matrix[i - 1, j - 1] + 1));
            }
        }
    }   

    return matrix[xl, yl];
}

Este é um problema comum de programação dinâmicas. O href="http://www.exampleproblems.com/wiki/index.php/Levenshtein_distance#Proof_of_correctness" rel="nofollow noreferrer"> entrada tem uma prova de correção parcial. Você está procurando algo mais?

Não são verdadeiras para este caso

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LevenshteinDistance
{
class Program
{
    static void Main(string[] args)
    {
        LevenshteinDistance ld = new LevenshteinDistance();
        string a="B";
        string b="Book";
        string c = "Sick";

        Console.WriteLine("{0} = Max( {1}, {2} )", ld.Compute(b, c), ld.Compute(a, c), ld.Compute(a, b)); 
        if (ld.Compute(b, c) == Math.Max(ld.Compute(a, c), ld.Compute(a, b)))
            Console.WriteLine("Equal");
        else
            Console.WriteLine("Not Equal");
        Console.ReadKey();

    }

}

class LevenshteinDistance
{
    //****************************
    // Get minimum of three values
    //****************************

    private int Minimum(int a, int b, int c)
    {
        int min;

        min = a;
        if (b < min)
        {
            min = b;
        }
        if (c < min)
        {
            min = c;
        }
        return min;

    }

    //*****************************
    // Compute Levenshtein distance
    //*****************************

    public int Compute(string s, string t)
    {
        int[,] matrix; // matrix
        int n; // length of s
        int m; // length of t
        int i; // iterates through s
        int j; // iterates through t
        char s_i; // ith character of s
        char t_j; // jth character of t
        int cost; // cost

        // Step 1
        n = s.Length;
        m = t.Length;
        if (n == 0)
        {
            return m;
        }
        if (m == 0)
        {
            return n;
        }
        matrix = new int[n + 1, m + 1];

        // Step 2

        for (i = 0; i <= n; i++)
        {
            matrix[i, 0] = i;
        }

        for (j = 0; j <= m; j++)
        {
            matrix[0, j] = j;
        }

        // Step 3

        for (i = 1; i <= n; i++)
        {

            s_i = s[(i - 1)];

            // Step 4

            for (j = 1; j <= m; j++)
            {

                t_j = t[(j - 1)];

                // Step 5

                if (s_i == t_j)
                {
                    cost = 0;
                }
                else
                {
                    cost = 1;
                }

                // Step 6

                matrix[i, j] = Minimum(matrix[i - 1, j] + 1, matrix[i, j - 1] + 1, matrix[i - 1, j - 1] + cost);

            }

        }

        // Step 7

        return matrix[n, m];

    }

}

}

scroll top