简体   繁体   中英

Computing the shortest path between two words?

I need to calculate the distance of two words that are found in a list. And by distance I mean the number of words that are found in between the source and target word. ex. dog -> cog -> cot -> cat Therefore the path distance would be three, as the edit distance between each word in the path is one. But I don't know how to deal with words with edit distances greater then one.

This might help you:

    List<String> animals = new ArrayList<String>();
    Boolean done=false;
    Boolean found=false;
    int dist=0;
    string begin="dog";
    string end="cat";

    // add 4 different values to list
    animals.add("dog");
    animals.add("cog");
    animals.add("cot");
    animals.add("cat");
    int i = 0;
    while (i < animals.size()&&!done) {
        if(animals.get(i).equals(begin)) found=true;
        if(found){
            dist++;
            if(animals.get(i).equals(end))done=true;
        }
        i++;
    }
    System.out.println(dist);

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