boggle game: backtracking algorithm implementation
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
Boggle Game:
Backtracking Algorithm Implementation
CS 1501
2023.04.20 2/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)}
2023.04.20 3/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;}
2023.04.20 4/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}
2023.04.20 5/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}
de la Briandias Tree:
Dealing with string prefix
CS 1501
2023.04.20 7/14
Note Structure
• DLB tree is used to organize key set (dictionary).• A path from root to leaf represents a word.
ChildPointer Char Sibling
Pointerb
e
c
a
d
f
abeacadf
$
$
$
2023.04.20 8/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.
ChildPointer Char Sibling
Pointerb
e
c
a
d
f
abeacadf
$
$
$
2023.04.20 9/14
An Example
• Construct a DLB tree for the following words:abc, abe, abet, abx, ace, acidhives, iodin, inval, zoo, zool, zurich
2023.04.20 10/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
2023.04.20 11/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;
}
2023.04.20 12/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
2023.04.20 13/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
}
2023.04.20 14/14
Corner cases
• The tree is empty when insert a word• The word is the last one in the tree• …