简体   繁体   English

两根弦之间的最短路径

[英]shortest path between two strings

I am having to do a problem which creates the adjacency matrix the finds the shortest path between to strings that the user enters. 我必须做一个创建邻接矩阵的问题,该矩阵找到用户输入的字符串之间的最短路径。 I have already read the data file full of strings and have built the adjacency matrix if the shortest path between strings is one. 如果字符串之间的最短路径是一个,我已经读取了充满字符串的数据文件,并建立了邻接矩阵。 I am confused on how to do this if the shortest path is 2,3,4,5, etc. The way to tell if the strings are connected is if the first words last three character, two characters, or last character matches the second word's first three characters, first two character, or first three characters. 如果最短路径是2,3,4,5等,我对如何执行此操作感到困惑。判断字符串是否已连接的方法是,第一个单词的后三个字符,两个字符或最后一个字符是否与第二个字符匹配单词的前三个字符,前两个字符或前三个字符。

An example I was given is "everyday" and "daytime" because the last and first three match. 我得到的一个例子是“每天”和“白天”,因为最后三个和前三个匹配。

If the last two and first two match an example is "brother" and "eraser". 如果后两个和前两个匹配,则示例为“兄弟”和“橡皮擦”。

If the last character and first character matches an example is "scorpion" and "night". 如果最后一个字符和第一个字符匹配,则示例为“蝎子”和“夜”。

    int i,j;
    String[] s = new String[sizOfFile];
int[][] a = new int[sizeOfFile][sizeOfFile]; 
for(i=0;i<n;i++)
{
    for(j=0;j<n;j++)
    {
        if((s[i].charAt(s[i].length()-3) == s[j].charAt(0) && s[i].charAt(s[i].length()-2) == s[j].charAt(1) && s                [i].charAt(s[i].length()-1) == s[j].charAt(2)))
            {
                a[i][j]=1;
            }
            else if(s[i].charAt(s[i].length()-1) == s[j].charAt(1) && s[i].charAt(s[i].length()-2) == s[j].charAt(0))
            {
                a[i][j]=1;
            }
            else if(s[i].charAt(s[i].length()-1) == s[j].charAt(0))
            {
                a[i][j]=1;
            }
            else
            {
                a[i][j]=0;
            }
            //Prints adjacency matrix
        }
    }

First, to find the shortest path without wasted effort, you need to do a breadth-first search, not a depth-first search. 首先,要找到最短的路径而不浪费精力,您需要进行广度优先搜索,而不是深度优先搜索。

Eg to illustrate what I mean, imagine a directory search for a file by name. 例如,为了说明我的意思,请想象一个目录按名称搜索文件。 You search the current directory and for each sub-directory you call the search method recursively. 您搜索当前目录,并为每个子目录递归调用搜索方法。 That is a depth-first search, because it'll search the full hierarchy of the first sub-directory before continuing, and that's not what you want. 这是深度优先的搜索,因为它将在继续之前搜索第一个子目录的整个层次结构,而这不是您想要的。

Instead, collect all the sub-directory names, then if file not found yet, iterate the list to search each sub-directory, collecting the list of sub-sub-directories, and if file still not found, repeat using new list. 而是,收集所有子目录名称,然后,如果尚未找到文件,则迭代该列表以搜索每个子目录,收集子子目录的列表,如果仍然找不到文件,则使用新列表重复。

That's is what you want. 那就是你想要的。 Collect a list of all the words that are "connected" to the current word, then for each word in that list, build a new list of all words connected to those words, and repeat until target word found. 收集所有与当前单词“连接”的单词的列表,然后针对该列表中的每个单词,建立与这些单词连接的所有单词的新列表,并重复进行直到找到目标单词。


That was the overall algorithm. 那就是整体算法。 The search for connected words should be done by adding all words to a TreeSet . 通过将所有单词添加到TreeSet可以完成对关联单词的搜索。

For each suffix (1-, 2-, or 3-letter), call subSet(suffix, nextSuffix) , where nextSuffix is the same as suffix except last character incremented by one. 对于每个后缀( subSet(suffix, nextSuffix)或3个字母),请调用subSet(suffix, nextSuffix) ,其中nextSuffixsuffix相同,只是最后一个字符加一。

Check each subset for the target word and stop if found. 检查目标词的每个子集,如果找到则停止。

Keep a separate HashSet called used of words found so far, to prevent infinite looping and to prevent using longer paths to a word that was already found using a shorter path. 保留一个单独的HashSetHashSet used到目前为止已找到的单词,以防止无限循环,并防止使用较长的路径指向已经使用较短的路径找到的单词。 Remove any words in the subset that's in the used set. 删除used集中的子集中的所有单词。

Add unused words in subset to used set, and (together with "path" of words traveled to get to the word) add to list of words to process on next iteration. 将子集中未使用的单词添加到used集合中,(以及与到达该单词的单词的“路径”一起)添加到单词列表,以在下一次迭代中进行处理。

Keep iterating until done, or list is empty (no path found). 一直进行迭代直到完成,或者列表为空(找不到路径)。

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

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