java - Breadth First Search Not Working -


i'm building game build hashtable of words (a dictionary) , take 2 strings , int user. try permute first string second string. done permuted 1 letter @ time , putting new word tree structure child node holding original word. done until original word permuted second word or until number permutations exceeds int given user. in basic test case giving program cat , cot , 3 hops. doesn't work. i've tried several things @ point can't figure out more , can't more specific. here 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 every letter in alphabet used permutations              'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };     queue<tnode> nextgen = new queue<tnode>(); //holds deepest generation of nodes     tnode goalnode = null;//node hold successful node     hopcounter++;     boolean found = false;     while (!nodes.empty()) {         tnode parent = nodes.dequeue();         oldwords.add(parent.getword());         (int q = 0; q < parent.getword().length(); q++) {             char oldlet = parent.getword().charat(q);             (int = 0; < 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 permute " + destination + " in " + hops + " hops.");         } else { //successful, found = true             stringbuilder path = new stringbuilder(goalnode.getword());             tnode currentnode = goalnode.getparent();             (int = goalnode.getdepth() - 1; > 0; i--) {                 path.insert(0, currentnode.getword() + "==>");             }             system.out.println("successful!");             system.out.println(path.tostring());         }     } } 

a tnode node has string, pointer parent , int node's depth in tree. hops number given user hopcounter holds current hop. original queue being passed holds single node original word. oldwords contains permutation have been created can avoid duplicates.

i may going wrong there isn't way test if actual work. if there better ways test , debug in loops run many times helpful. i've used debuggers aren't helpful in this. appreciated!

well, one, parent.getword().replace(oldlet, alphabet[i]) replaces all occurences of oldlet different letter, not @ position q.

additionally, when outputting results, you're not updating currentnode. i'd write like

while (currentnode != null) {     path.insert(0, currentnode.getword() + "==>");     currentnode = currentnode.getparent(); } 

this, of course, assumes parent of root node null.

as bit of side remark, in interest of performance, i'd use hashtable oldwords, arraylist.contains o(n) operation.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -