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

时间:2018-10-07 23:18:29

标签: java data-structures word-list

我正在创建一个程序,该程序将使用5000个字符串的单词列表,并找到从一个字符串到另一个字符串的最短路径。例如,abc-> bac可以显示“ abc,bbc,bac”。

我很确定自己想做什么,我唯一不完全确定的是什么数据结构应该代表我的单词表。目标是搜索(BFS)尽可能快地运行,因此牺牲一些空间是没有问题的。我在考虑BST或邻接表,但是由于我不是datastrutcutres的时间复杂性方面的专家,因此我想在开始调整代码之前先确定一下。谁能推荐其中一个结构而不是另一个?还是我可能错过了一个明显的替代数据结构?

1 个答案:

答案 0 :(得分:0)

看起来您正在寻找的是Levenshtein distancehere is the Rosetta code implementation,您应该可以根据需要对其进行更改:

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]));
    }
}
相关问题