Compsci 201 Recitation 10 Professor Peck Jimmy Wei 11/1/2013.

Slides:



Advertisements
Similar presentations
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Advertisements

CS50 WEEK 6 Kenny Yu.
Boggle Game: Backtracking Algorithm Implementation
Binary Trees. DCS – SWC 2 Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure.
Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.
Tries Search for ‘bell’ O(n) by KMP algorithm O(dm) in a trie Tries
Compsci 201 Recitation 6 Professor Peck Jimmy Wei 2/14/
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
Higher Order Tries Key = Social Security Number.   9 decimal digits. 10-way trie (order 10 trie) Height
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Grade 12 Computer Studies HG
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Compsci 201 Recitation 12 Professor Peck Jimmy Wei 11/15/2013.
CS125 Exam Review Winter Some Exam Info Tuesday (22nd) at 4:00-6:30pm in the PAC CHECK YOUR SEAT!!! Read Final Exam Information on website –Practice.
Information and Computer Sciences University of Hawaii, Manoa
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Compsci 201 Recitation 11 Professor Peck Jimmy Wei 11/8/2013.
Chapter 8: Arrays.
Implementation of the Hangman Game in C++
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
CPS 100, Fall GridGame APT How would you solve this problem using recursion?
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
Recitation 2 James Wei Professor Peck 1/17/2013. Covered in this Recitation Arrays Variable Scoping Local variables Instance variables Class variables.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
Higher Order Tries Key = Social Security Number.   9 decimal digits. 10-way trie (order 10 trie) Height
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
COMP 103 Hashing. 2 RECAP-TODAY RECAP Bitmaps are a fast way to implement Sets of integers, characters, etc TODAY  Hashing is a similar idea  Detecting.
Model Checking Lock-Free Binary Search Tree Presented by Joanna Helga.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
Dynamic Programming & Memoization. When to use? Problem has a recursive formulation Solutions are “ordered” –Earlier vs. later recursions.
Week 14 - Wednesday.  What did we talk about last time?  Heapsort  Timsort  Counting sort  Radix sort.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Lab 6 Problem 1: DNA. DNA Given a string with length N, determine the number of occurrences of some given substrings (with length K) in that string. For.
Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.
02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)
Contents What is a trie? When to use tries
Generic Trees—Trie, Compressed Trie, Suffix Trie (with Analysi
Tries 4/16/2018 8:59 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Data Structures and Analysis (COMP 410)
Computing with C# and the .NET Framework
Data Structure By Amee Trivedi.
Higher Order Tries Key = Social Security Number.
Agenda Warmup AP Exam Review: Litvin A2
Mark Redekopp David Kempe
ENEE150 Discussion 13 Section 0101 Adam Wang.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Tries A trie is another type of tree structure. The word “trie” comes from the word “retrieval,” but is usually pronounced like “try.” For our purposes,
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
CSE 373 Data Structures and Algorithms
COMPUTER 2430 Object Oriented Programming and Data Structures I
Building Java Programs
Data Structures & Algorithms
Lecture No.02 Data Structures Dr. Sohail Aslam
Recursive Objects Singly Linked Lists.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Presentation transcript:

Compsci 201 Recitation 10 Professor Peck Jimmy Wei 11/1/2013

In this Recitation Boggle warmup! – Intro – Tries – Recursion Submit via form:

Boggle Intro What is Boggle? – You will be writing recursive code to find all words on a Boggle Board which looks like this:

Boggle Intro What’s in Boggle? – BoggleBoard: represents a 2D grid of board values – BoardCell: represents a single cell on the board—does NOT hold cell value, only its coordinates – LexStatus: enumeration representing if a String is a WORD, PREFIX, or NOT_WORD – ILexicon: represents a lexicon, i.e. all valid words; think of it as our dictionary We will work with the TrieLexicon implementation today – IWordOnBoardFinder: searches for all valid words on a given board; used by computer after player completes turn We will work with the GoodWordOnBoardFinder implementation today – Other classes not needed for this recitation but important for the actual assignment…

Boggle Intro Two questions we will address: – How can we represent our lexicon, i.e. the list of all valid words? – How can we recursively search the board to find all valid words on the board?

Boggle Tries First question: How to represent the lexicon? Consider a trie, utilized in TrieLexicon.java: This trie represents: – Dog – Dot – Doting – Drag – Drastic – Top – Torn – Trap

Boggle Tries This trie supports queries (add, contains, delete) in O(w) time for words of length w. Each node in a trie has one subtrie for every letter than can possibly follow it – For instance, to add ‘duke’ we would add a ‘u’ child to the existing ‘d’ node Red dots indicate nodes holding the final letter of a word

Boggle Trie Add boolean add(String s) { Node t = myRoot; Node t = myRoot; for (int k=0; k<s.length(); k++) { for (int k=0; k<s.length(); k++) { char ch = s.charAt(k); char ch = s.charAt(k); Node child = t.children.get(ch); Node child = t.children.get(ch); if (child == null) { if (child == null) { child = new Node(ch, t); child = new Node(ch, t); t.children.put(ch, child); t.children.put(ch, child); } t = child; t = child; } if (!t.isWord) { if (!t.isWord) { t.isWord = true; // mark as word t.isWord = true; // mark as word mySize++; mySize++; return true; return true; } return false; //word already in trie return false; //word already in trie} boolean add(String s) { Node t = myRoot; Node t = myRoot; for (int k=0; k<s.length(); k++) { for (int k=0; k<s.length(); k++) { char ch = s.charAt(k); char ch = s.charAt(k); Node child = t.children.get(ch); Node child = t.children.get(ch); if (child == null) { if (child == null) { child = new Node(ch, t); child = new Node(ch, t); t.children.put(ch, child); t.children.put(ch, child); } t = child; t = child; } if (!t.isWord) { if (!t.isWord) { t.isWord = true; // mark as word t.isWord = true; // mark as word mySize++; mySize++; return true; return true; } return false; //word already in trie return false; //word already in trie}

Boggle Trie WordStatus LexStatus wordStatus(String s) { Node t = myRoot; Node t = myRoot; for (int k=0; k<s.length(); k++) { for (int k=0; k<s.length(); k++) { char ch = s.charAt(k); char ch = s.charAt(k); t = t.children.get(ch); t = t.children.get(ch); if (t == null) { if (t == null) { return LexStatus.NOT_WORD; return LexStatus.NOT_WORD; } } if (t.isWord) { if (t.isWord) { return LexStatus.WORD; return LexStatus.WORD; } else { } else { return LexStatus.PREFIX; return LexStatus.PREFIX; }} LexStatus wordStatus(String s) { Node t = myRoot; Node t = myRoot; for (int k=0; k<s.length(); k++) { for (int k=0; k<s.length(); k++) { char ch = s.charAt(k); char ch = s.charAt(k); t = t.children.get(ch); t = t.children.get(ch); if (t == null) { if (t == null) { return LexStatus.NOT_WORD; return LexStatus.NOT_WORD; } } if (t.isWord) { if (t.isWord) { return LexStatus.WORD; return LexStatus.WORD; } else { } else { return LexStatus.PREFIX; return LexStatus.PREFIX; }}

Boggle Tries – Answer #1 LexStatus wordStatus(String s) { Node t = myRoot; Node t = myRoot; for (int k=0; k<s.length(); k++) { for (int k=0; k<s.length(); k++) { char ch = s.charAt(k); char ch = s.charAt(k); t = t.children.get(ch); t = t.children.get(ch); if (t == null) { if (t == null) { return LexStatus.NOT_WORD; return LexStatus.NOT_WORD; } } if (t.isWord) { if (t.isWord) { return LexStatus.WORD; return LexStatus.WORD; } else { } else { return LexStatus.PREFIX; return LexStatus.PREFIX; }} LexStatus wordStatus(String s) { Node t = myRoot; Node t = myRoot; for (int k=0; k<s.length(); k++) { for (int k=0; k<s.length(); k++) { char ch = s.charAt(k); char ch = s.charAt(k); t = t.children.get(ch); t = t.children.get(ch); if (t == null) { if (t == null) { return LexStatus.NOT_WORD; return LexStatus.NOT_WORD; } } if (t.isWord) { if (t.isWord) { return LexStatus.WORD; return LexStatus.WORD; } else { } else { return LexStatus.PREFIX; return LexStatus.PREFIX; }} boolean add(String s) { Node t = myRoot; Node t = myRoot; for (int k=0; k<s.length(); k++) { for (int k=0; k<s.length(); k++) { char ch = s.charAt(k); char ch = s.charAt(k); Node child = t.children.get(ch); Node child = t.children.get(ch); if (child == null) { if (child == null) { child = new Node(ch, t); child = new Node(ch, t); t.children.put(ch, child); t.children.put(ch, child); } t = child; t = child; } if (!t.isWord) { if (!t.isWord) { t.isWord = true; // mark as word t.isWord = true; // mark as word mySize++; mySize++; return true; return true; } return false; //word already in trie return false; //word already in trie} boolean add(String s) { Node t = myRoot; Node t = myRoot; for (int k=0; k<s.length(); k++) { for (int k=0; k<s.length(); k++) { char ch = s.charAt(k); char ch = s.charAt(k); Node child = t.children.get(ch); Node child = t.children.get(ch); if (child == null) { if (child == null) { child = new Node(ch, t); child = new Node(ch, t); t.children.put(ch, child); t.children.put(ch, child); } t = child; t = child; } if (!t.isWord) { if (!t.isWord) { t.isWord = true; // mark as word t.isWord = true; // mark as word mySize++; mySize++; return true; return true; } return false; //word already in trie return false; //word already in trie}

Boggle Tries – Answer #2 public class Node { String info; String info; boolean isWord; boolean isWord; Map children; Map children; Node parent; Node parent; Node(char ch, Node p) { Node(char ch, Node p) { info = “”+ch; info = “”+ch; isWord = false; isWord = false; children = new TreeMap (); children = new TreeMap (); parent = p; parent = p; }} public class Node { String info; String info; boolean isWord; boolean isWord; Map children; Map children; Node parent; Node parent; Node(char ch, Node p) { Node(char ch, Node p) { info = “”+ch; info = “”+ch; isWord = false; isWord = false; children = new TreeMap (); children = new TreeMap (); parent = p; parent = p; }}

Boggle Tries – Answer #3 private Node copyTrie(Node root) { if (root == null) { if (root == null) { return null; return null; } Node copy = new Node(root.info.charAt(0), null); Node copy = new Node(root.info.charAt(0), null); copy.isWord = root.isWord; copy.isWord = root.isWord; // more code to copy rest of trie // more code to copy rest of trie return copy; return copy;} private Node copyTrie(Node root) { if (root == null) { if (root == null) { return null; return null; } Node copy = new Node(root.info.charAt(0), null); Node copy = new Node(root.info.charAt(0), null); copy.isWord = root.isWord; copy.isWord = root.isWord; // more code to copy rest of trie // more code to copy rest of trie return copy; return copy;}

Boggle Tries – Answer #4 private int wordCount(Node root) { if (root == null) { if (root == null) { return 0; return 0; } // more code to count words in trie // more code to count words in trie // use the isWord field to check each Node // use the isWord field to check each Node} private int wordCount(Node root) { if (root == null) { if (root == null) { return 0; return 0; } // more code to count words in trie // more code to count words in trie // use the isWord field to check each Node // use the isWord field to check each Node}

Boggle Tries – Answer #5 Look at the compressed trie to the right: – We have combined chains of single pointers into single Nodes – We want to figure out how to perform this compression

Boggle Recursion Second question: How to recurse over board? Look at the following code for implementing GoodWordOnBoardFinder.java: List cellsForWord(BoggleBoard board, String word) { ArrayList list = new ArrayList (); ArrayList list = new ArrayList (); for (int r=0; r<board.size(); r++) { for (int r=0; r<board.size(); r++) { for (int c=0; c<board.size(); c++) { for (int c=0; c<board.size(); c++) { if (findHelper(word, 0, r, c, board, list)) { if (findHelper(word, 0, r, c, board, list)) { return list; return list; } } } list.clear(); list.clear(); return list; return list;} List cellsForWord(BoggleBoard board, String word) { ArrayList list = new ArrayList (); ArrayList list = new ArrayList (); for (int r=0; r<board.size(); r++) { for (int r=0; r<board.size(); r++) { for (int c=0; c<board.size(); c++) { for (int c=0; c<board.size(); c++) { if (findHelper(word, 0, r, c, board, list)) { if (findHelper(word, 0, r, c, board, list)) { return list; return list; } } } list.clear(); list.clear(); return list; return list;}

Boggle Recursion Here is the header for the helper method: /** * Returns true if and only if the substring of word starting at index can be * Returns true if and only if the substring of word starting at index can be * formed starting at (row, col) on the board provided without using any cells * formed starting at (row, col) on the board provided without using any cells * stored in list --- and extending list to include all cells on which substring is * stored in list --- and extending list to include all cells on which substring is * found when true is returned. * found when true is returned. word is searched for word (e.g. entered by user) word is searched for word (e.g. entered by user) index is where in word substring starts, e.g. all chars before index index is where in word substring starts, e.g. all chars before index * have been found and cells are in list * have been found and cells are in list row is starting point for search row is starting point for search col is starting point for search col is starting point for search board is board on which word is searched board is board on which word is searched list is cells on board where word is found list is cells on board where word is found true if substring found on board without reusing board cells in list true if substring found on board without reusing board cells in list */ */ private boolean findHelper(String word, int index, int row, int col, BoggleBoard board, List list); /** * Returns true if and only if the substring of word starting at index can be * Returns true if and only if the substring of word starting at index can be * formed starting at (row, col) on the board provided without using any cells * formed starting at (row, col) on the board provided without using any cells * stored in list --- and extending list to include all cells on which substring is * stored in list --- and extending list to include all cells on which substring is * found when true is returned. * found when true is returned. word is searched for word (e.g. entered by user) word is searched for word (e.g. entered by user) index is where in word substring starts, e.g. all chars before index index is where in word substring starts, e.g. all chars before index * have been found and cells are in list * have been found and cells are in list row is starting point for search row is starting point for search col is starting point for search col is starting point for search board is board on which word is searched board is board on which word is searched list is cells on board where word is found list is cells on board where word is found true if substring found on board without reusing board cells in list true if substring found on board without reusing board cells in list */ */ private boolean findHelper(String word, int index, int row, int col, BoggleBoard board, List list);

Boggle Recursion An example: – Say we are searching for “PEST” – String word = “PEST” – Int index = 3 – Int row = 2 – Int col = 1 – BoggleBoard board = [“NIEF”, “PTTO”, “ESWE”, “MRNO”] This String array is held as an instance variable of a BoggleBoard object – List list = [(1,0); (2,0); (2,1)] Each item in this list is actually a BoardCell object with two instance variables, int row and int col

Boggle Recursion – Answer #6-11 // Returns true if and only if the substring of word starting at index can be formed // starting at (row, col) on the board provided without using any cells stored in list // Must extend list to include all cells on which substring is found when true word is searched for word (e.g. entered by user) index is where substring starts row is starting point for search col is starting point for search board is board on which word is searched list is cells on board where word is found true if substring found on board without reusing board cells in list private boolean findHelper(String word, int index, int row, int col, BoggleBoard board, List list); // Returns true if and only if the substring of word starting at index can be formed // starting at (row, col) on the board provided without using any cells stored in list // Must extend list to include all cells on which substring is found when true word is searched for word (e.g. entered by user) index is where substring starts row is starting point for search col is starting point for search board is board on which word is searched list is cells on board where word is found true if substring found on board without reusing board cells in list private boolean findHelper(String word, int index, int row, int col, BoggleBoard board, List list); List cellsForWord(BoggleBoard board, String word) { ArrayList list = new ArrayList<>(); ArrayList list = new ArrayList<>(); for (int r=0; r<board.size(); r++) for (int r=0; r<board.size(); r++) for (int c=0; c<board.size(); c++) for (int c=0; c<board.size(); c++) if (findHelper(word, 0, r, c, board, list)) { return list; } if (findHelper(word, 0, r, c, board, list)) { return list; } list.clear(); list.clear(); return list; return list;} List cellsForWord(BoggleBoard board, String word) { ArrayList list = new ArrayList<>(); ArrayList list = new ArrayList<>(); for (int r=0; r<board.size(); r++) for (int r=0; r<board.size(); r++) for (int c=0; c<board.size(); c++) for (int c=0; c<board.size(); c++) if (findHelper(word, 0, r, c, board, list)) { return list; } if (findHelper(word, 0, r, c, board, list)) { return list; } list.clear(); list.clear(); return list; return list;}

Have a good weekend! Don’t forget to submit!