Presentation is loading. Please wait.

Presentation is loading. Please wait.

Boggle Game: Backtracking Algorithm Implementation

Similar presentations


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

1 Boggle Game: Backtracking Algorithm Implementation
CS 1501

2 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) } 2017/4/12

3 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; } 2017/4/12

4 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 } 2017/4/12

5 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 } 2017/4/12

6 de la Briandias Tree: Dealing with string prefix
CS 1501

7 Note Structure DLB tree is used to organize key set (dictionary).
A path from root to leaf represents a word. a Child Pointer Char Sibling b c d e $ f $ $ abe ac adf 2017/4/12

8 Node Structure Determine prefix? Determine word? a Char b c d e $ f $
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. a Child Pointer Char Sibling b c d e $ f $ $ abe ac adf 2017/4/12

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

10 Insert Operation INSERTION (tree pointer TREE, word WORD) { out = 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 2017/4/12

11 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; 2017/4/12

12 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 2017/4/12

13 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 } 2017/4/12

14 Corner cases The tree is empty when insert a word
The word is the last one in the tree 2017/4/12


Download ppt "Boggle Game: Backtracking Algorithm Implementation"

Similar presentations


Ads by Google