Presentation is loading. Please wait.

Presentation is loading. Please wait.

Boggle Game: Backtracking Algorithm Implementation CS 1501.

Similar presentations


Presentation on theme: "Boggle Game: Backtracking Algorithm Implementation CS 1501."— Presentation transcript:

1 Boggle Game: Backtracking Algorithm Implementation CS 1501

2 2015/4/222/14 Recursive Implementation Things to implementation for a recursive algorithm: –Find the common behavior in the algorithm, and implement that as a function. –Action: what to do for the current situation –Recursive call: how to trigger next step –Stop criteria: when to stop further recursion function Test (data) { if data satisfies stop criteria (1) return; do sometime for data (2) for all new_data from data Test (new_data); (3) }

3 2015/4/223/14 Recursive Boggle Each move on the board can be a recursive call –They accomplish similar tasks based on the string we have so far. function AdvanceStep (pos, string) { new_string = string + letter[pos]; if new_string is not prefix, not word (1) return; }

4 2015/4/224/14 Recursive Boggle Each move on the board can be a recursive call –They accomplish similar tasks based on the string we have so far. function AdvanceStep (pos, string) { new_string = string + letter[pos]; if new_string is not prefix, not word (1) return; if new_string is a word (2) output string }

5 2015/4/225/14 Recursive Boggle Each move on the board can be a recursive call –They accomplish similar tasks based on the string we have so far. function AdvanceStep (pos, string) { new_string = string + letter[pos]; if new_string is not prefix, not word (1) return; if new_string is a word (2) output string if new_string is a prefix (3) for all possible next step new_pos AdvanceStep (new_pos, new_string); return }

6 de la Briandias Tree: D ealing with string prefix CS 1501

7 2015/4/227/14 Note Structure DLB tree is used to organize key set (dictionary). A path from root to leaf represents a word. Child Pointer Char Sibling Pointer becadf abe ac adf $$$

8 2015/4/228/14 Node Structure Determine prefix? –The node has at least one child that is not a “end-of-string” sign. Determine word? –Node has a child with “end-of-string” sign. Child Pointer Char Sibling Pointer becadf abe ac adf $$$

9 2015/4/229/14 An Example Construct a DLB tree for the following words: abc, abe, abet, abx, ace, acid hives, iodin, inval, zoo, zool, zurich

10 2015/4/2210/14 Insert Operation INSERTION (tree pointer TREE, word WORD) { out = 0; i = 0; letter = word [at position i]; current = TREE; DO WHILE (( i <= length of WORD) and (out=0)) { IF (letter equals (current->key)) { //if we have a match i = i +1; letter = word[ at position i]; current = current->child_pointer; //follow the child } ELSE { //we have to check siblings IF (current->sibling_pointer does not equal Null) current = current->sibling; ELSE out=1; //no sibling, we add new node } } (end of DO WHILE) …… // insertion

11 2015/4/2211/14 Insert Operation INSERTION (tree pointer TREE, word WORD) { …….. // search // we're at the point of insertion of new node, // unless the word is already there: IF (out = 0) EXIT //if the word was already there, exit // otherwise add a new node with the current letter current->sibling_pointer = CREATE new NODE (letter); i = i + 1; // and move on to append the rest of the letters FOR (m=i ; m<= length of WORD; m++) { current.child_pointer = CREATE new NODE ( WORD[at position m]); current = current->child_pointer; } // now we just add the $ marker for end of word current.child_pointer = CREATE new TERMINAL $ MARKER NODE; }

12 2015/4/2212/14 Delete Operation DELETE (tree pointer TREE, word WORD) { current = TREE; // we start with the first node again out=0; i=0; letter = WORD [at position i]; DO WHILE (( i <= length of WORD) and (out=0)) { IF (letter is equal to current->key) current = current->child_pointer; //we move down one level i = i + 1; letter = WORD [at position i]; // we move onto next letter ELSE IF (there is a sibling node) { last_sibling = current; //store the last sibling current = current->sibling // move to the sibling node } IF (there is no sibling node) out =1; // EXIT with ERROR } DO WHILE …… // deleteion

13 2015/4/2213/14 Delete Operation DELETE (tree pointer TREE, word WORD) { ……. // search PUSH last_sibling->sibling_pointer onto STACK PUSH all the children of last_sibling->sibling_pointer onto STACK one by one, until the $ marker Set all of the pointers on STACK to Null. as you're POPPING them off the STACK }

14 2015/4/2214/14 Corner cases The tree is empty when insert a word The word is the last one in the tree …


Download ppt "Boggle Game: Backtracking Algorithm Implementation CS 1501."

Similar presentations


Ads by Google