简体   繁体   中英

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.

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 .

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.

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. Remove any words in the subset that's in the used set.

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.

Keep iterating until done, or list is empty (no path found).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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