简体   繁体   English

查找两个字符串之间最短路径的数据结构

[英]Datastrutcture for finding shortest path between two strings

I am creating a program that will take a wordlist of 5 000 strings and find the shortest path from one string to another. 我正在创建一个程序,该程序将使用5000个字符串的单词列表,并找到从一个字符串到另一个字符串的最短路径。 For example abc -> bac could print "abc, bbc, bac". 例如,abc-> bac可以显示“ abc,bbc,bac”。

I am pretty sure about what I want to do, the only thing I'm not completely sure about is what datastructure should represent my wordlist. 我很确定自己想做什么,唯一不确定的是什么数据结构应该代表我的单词表。 The goal is for the search(BFS) to run as fast as possible, so to sacrifice some space is no problem. 目标是搜索(BFS)尽可能快地运行,因此牺牲一些空间是没有问题的。 I am thinking either a BST or an adjacency list, but since I'm no expert at datastrutcutres' timecomplexity I want to be certain before I start adjusting my code. 我在考虑BST或邻接表,但是由于我不是datastrutcutres的时间复杂性方面的专家,因此我想在开始调整代码之前先确定一下。 Can anyone recommend one of the structures over the other? 谁能推荐其中一个结构而不是另一个? Or have I perhaps missed a datastructure that is an obvious alternative for this? 还是我可能错过了一个显然可以替代这种情况的数据结构?

Looks like what you are looking for is the Levenshtein distance , here is the Rosetta code implementation , you should be able to change it to suit your need: 看起来您正在寻找的是Levenshtein距离这是Rosetta代码实现 ,您应该能够更改它以满足您的需要:

public class Levenshtein {

    public static int distance(String a, String b) {
        a = a.toLowerCase();
        b = b.toLowerCase();
        // i == 0
        int [] costs = new int [b.length() + 1];
        for (int j = 0; j < costs.length; j++)
            costs[j] = j;
        for (int i = 1; i <= a.length(); i++) {
            // j == 0; nw = lev(i - 1, j)
            costs[0] = i;
            int nw = i - 1;
            for (int j = 1; j <= b.length(); j++) {
                int cj = Math.min(1 + Math.min(costs[j], costs[j - 1]), a.charAt(i - 1) == b.charAt(j - 1) ? nw : nw + 1);
                nw = costs[j];
                costs[j] = cj;
            }
        }
        return costs[b.length()];
    }

    public static void main(String [] args) {
        String [] data = { "kitten", "sitting", "saturday", "sunday", "rosettacode", "raisethysword" };
        for (int i = 0; i < data.length; i += 2)
            System.out.println("distance(" + data[i] + ", " + data[i+1] + ") = " + distance(data[i], data[i+1]));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM