1 / 14

Boggle Game: Backtracking Algorithm Implementation

Boggle Game: Backtracking Algorithm Implementation. CS 1501. 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

eros
Download Presentation

Boggle Game: Backtracking Algorithm Implementation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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) }

  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; }

  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 }

  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 }

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

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

  8. Child Pointer Char b e c a d f $ $ $ Sibling Pointer 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. abe ac adf

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

  10. 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. 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. 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. 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. Corner cases • The tree is empty when insert a word • The word is the last one in the tree • …

More Related