简体   繁体   中英

Breadth First Search Not Working

I'm building a game where I build a hashtable of words (a dictionary) and then take two strings and an int from the user. I try to permute the first string into the second string. This is done by permuted one letter at a time and putting the new word into a tree structure as a child a node holding the original word. This is done until the original word is successfully permuted into the second word or until the number permutations exceeds the int given by the user. In my basic test case I giving the program cat and cot and 3 hops. This doesn't work. I've tried several things but at this point I really can't figure out anything more and can't more specific. Here is the code

public static void permute(HashTable dict, Queue<TNode> nodes, ArrayList<String> oldWords, String destination, int hops, int hopCounter) {
    char[] alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', //array with every letter in the alphabet will be used for permutations 
            'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
    Queue<TNode> nextGen = new Queue<TNode>(); //holds the deepest generation of nodes
    TNode goalNode = null;//node to hold a successful node
    hopCounter++;
    boolean found = false;
    while (!nodes.empty()) {
        TNode parent = nodes.dequeue();
        oldWords.add(parent.getWord());
        for (int q = 0; q < parent.getWord().length(); q++) {
            char oldLet = parent.getWord().charAt(q);
            for (int i = 0; i < alphabet.length; i++) {
                String test = parent.getWord().replace(oldLet, alphabet[i]);
                if (dict.contains(test) && !oldWords.contains(test)) {
                    nextGen.enqueue(new TNode(test, parent, hopCounter));
                    found = test.equals(destination); //determine if successful permutation 
                    if (found) {
                        goalNode = new TNode(test);
                    }
                }

            }
        }
    }

    if (hopCounter < hops && !found) {
        permute(dict, nextGen, oldWords, destination, hops, hopCounter);
    } else {
        if (hopCounter == hops) {
            System.out.println("Unable to permute to " + destination + " in " + hops + " hops.");
        } else { //Successful, found = true
            StringBuilder path = new StringBuilder(goalNode.getWord());
            TNode currentNode = goalNode.getParent();
            for (int i = goalNode.getDepth() - 1; i > 0; i--) {
                path.insert(0, currentNode.getWord() + "==>");
            }
            System.out.println("Successful!");
            System.out.println(path.toString());
        }
    }
}

A TNode is simply a node that has a String, a pointer to the parent and an int for the node's depth in the tree. hops is the number given by the user hopCounter holds the current hop. The original queue being passed holds a single node with the original word. oldWords contains all the permutation that have already been created so I can avoid duplicates.

I may be going about this all wrong but there isn't a good way to test if it would actual work. If there are better ways to test and debug in loops that run this many times that would be helpful. I've used debuggers but they aren't that helpful in this. Any help is really appreciated!

Well, for one, parent.getWord().replace(oldLet, alphabet[i]) replaces all occurences of oldLet with a different letter, not just at position q .

Additionally, when outputting the results, you're not updating the currentNode . I'd write something like

while (currentNode != null) {
    path.insert(0, currentNode.getWord() + "==>");
    currentNode = currentNode.getParent();
}

This, of course, assumes the parent of the root node is null .

As a bit of a side remark, in the interest of performance, I'd use a HashTable for oldWords , as ArrayList.contains is an O(n) operation.

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