AnagramSolver Based on slides by Ethan Apter

Slides:



Advertisements
Similar presentations
© Chinese University, CSE Dept. Software Engineering / Software Engineering Topic 1: Software Engineering: A Preview Your Name: ____________________.
Advertisements

Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
CSC 2400 Computer Systems I Lecture 3 Big Ideas. 2 Big Idea: Universal Computing Device All computers, given enough time and memory, are capable of computing.
Chapter Chapter Goals Describe the layers of a computer system Describe the concept of abstraction and its relationship to computing Describe.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Runtime alignment system SOFTWARE DESIGN IDEAS Wed 4 th May 2005 P Coe.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
The History of Programming Languages CS 170b Benjamin Gaska, much help from William Mitchell.
CS 415: Programming Languages Chapter 1 Aaron Bloomfield Fall 2005.
Chapter 01 Nell Dale & John Lewis.
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.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need.
ICAPRG301A Week 4Buggy Programming ICAPRG301A Apply introductory programming techniques Program Bugs US Navy Admiral Grace Hopper is often credited with.
Famous Firsts in Computation MSE 2400 Evolution & Learning
Chapter 1 The Big Picture.
Java Introduction to JNI Prepared by Humaira Siddiqui.
CSE 143 Lecture 11 Maps Grammars slides created by Alyssa Harding
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Building Java Programs Chapter 11 Lecture 11-1: Sets and Maps reading:
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
National Diploma Unit 4 Introduction to Software Development Introduction to Programming Languages.
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
Software Basics. Some Pioneers Charles Babbage Analytical Engine Countess Ada Lovelace First Programmer ? John Von Neumann storing instructions in memory.
Sets, Maps and Hash Tables. RHS – SOC 2 Sets We have learned that different data struc- tures have different advantages – and drawbacks Choosing the proper.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 12/6/2006 Lecture 24 – Profilers.
CPS 100, Fall GridGame APT How would you solve this problem using recursion?
CSE 143 Lecture 14 (B) Maps and Grammars reading: 11.3 slides created by Marty Stepp
ICAPRG301A Week 2 Strings and things Charles Babbage Developed the design for the first computer which he called a difference engine, though.
Stored Programs In today’s lesson, we will look at: what we mean by a stored program computer how computers store and run programs what we mean by the.
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Charles Babbage Invented the first computer (depending on what you consider a computer is). On two occasions I have been asked by members of.
CPS120: Introduction to Computer Science Variables and Constants.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Week 9 - Friday.  What did we talk about last time?  Collisions  Open addressing ▪ Linear probing ▪ Quadratic probing ▪ Double hashing  Chaining.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
CS154 A History of Computation Thaddeus Aid Department of Computer Science San Jose State University Spring 2016 Creative Commons Attribution-ShareAlike.
CSE 373: Data Structures and Algorithms Lecture 16: Hashing III 1.
CSE 143 Lecture 11: Sets and Maps reading:
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Software Engineering Algorithms, Compilers, & Lifecycle.
Programming Language Paradigms ITSK2314 Lecture 3.
History. Development Driven by Function Functions of a Programming Language –To describe computation for use by computers –To describe computation and.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Artificial intelligence and creativity
Chapter 1 The Big Picture
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
CSCI 104 Backtracking Search
Application Development Theory
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Road Map CS Concepts Data Structures Java Language Java Collections
Problem Solving: Brute Force Approaches
Lesson 2: Building Blocks of Programming
What Is a Program? A program is like an algorithm, but describes a process that is ready or can be made ready to run on a real computer Retrieved from:
TCSS 342, Winter 2006 Lecture Notes
Exercise: Dice roll sum
Maps Grammars Based on slides by Marty Stepp & Alyssa Harding
slides created by Ethan Apter
slides created by Ethan Apter
Von Neumann Architecture
slides created by Ethan Apter
slides created by Alyssa Harding
Road Map - Quarter CS Concepts Data Structures Java Language
CS2013 Lecture 7 John Hurley Cal State LA.
slides created by Ethan Apter
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
What is Computer Science at Duke?
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Presentation transcript:

AnagramSolver Based on slides by Ethan Apter CSE 143 Lecture 19 AnagramSolver Based on slides by Ethan Apter

Ada Lovelace (1815-1852) <http://en.wikipedia.org/wiki/Ada_lovelace> Ada Lovelace is considered the first computer programmer for her work on Charles Babbage’s analytical engine She was a programmer back when computers were still theoretical!

Alan Turing (1912-1954) <http://en.wikipedia.org/wiki/Alan_turing> Alan Turing made key contributions to artificial intelligence (the Turing test) and computability theory (the Turing machine) He also worked on breaking Enigma (a Nazi encryption machine)

Grace Hopper (1906-1992) Grace Hopper developed the first compiler <http://en.wikipedia.org/wiki/Grace_hopper> Grace Hopper developed the first compiler She was responsible for the idea that programming code could look like English rather than machine code She influenced the languages COBOL and FORTRAN

Alan Kay (1940) Alan Kay worked on Object-Oriented Programming <http://en.wikipedia.org/wiki/Alan_Kay> Alan Kay worked on Object-Oriented Programming He designed SmallTalk, a programming language in which everything is an object He also worked on graphical user interfaces (GUIs)

John McCarthy (1927) <http://en.wikipedia.org/wiki/John_McCarthy_(computer_scientist)> <http://en.wikipedia.org/wiki/Lisp_(programming_language)> <http://www-formal.stanford.edu/jmc/jmcbw.jpg> John McCarthy designed Lisp (“Lisp” is short for “List Processing”) He invented if/else Lisp is a very flexible language and was popular with the Artificial Intelligence community

Anagrams anagram: a rearrangement of the letters from a word or phrase to form another word or phrase Consider the phrase “word or phrase” one anagram of “word or phrase” is “sparrow horde” Some other anagrams: “Alyssa Harding”  “darling sashay” “Ethan Apter”  “ate panther” w o r d p h a s e s p a r o w h d e

AnagramSolver Your next assignment is to write a class named AnagramSolver AnagramSolver finds all the anagrams for a given word or phrase (within the specified dictionary) it uses recursive backtracking to do this AnagramSolver may well be either the easiest or hardest assignment this quarter easy: it’s similar to 8 Queens, it’s short (approx. 50 lines) hard: it’s your first recursive backtracking assignment

AnagramSolver Consider the phrase “Ada Lovelace” Some anagrams of “Ada Lovelace” are: “ace dale oval” “coda lava eel” “lace lava ode” We could think of each anagram as a list of words: “ace dale oval”  [ace, dale, oval] “coda lava eel”  [coda, lava, eel] “lace lava ode”  [lace, lava, ode]

AnagramSolver Consider also the small dictionary file dict1.txt: We’re going to use only the words from this dictionary to make anagrams of “Ada Lovelace” ail gnat run alga lace rung angular lain tag ant lava tail coda love tan eel lunar tang gal nag tin gala natural urinal giant nit urn gin ruin

AnagramSolver Which is the first word in this list that could be part of an anagram of “Ada Lovelace” ail no: “Ada Lovelace” doesn’t contain an “i” alga no: “Ada Lovelace” doesn’t contain a “g” angular no: “Ada Lovelace” doesn’t contain an “n”, a “g”, a “u”, or an “r” ant no: “Ada Lovelace” doesn’t contain an “n” or a “t” coda yes: “Ada Lovelace” contains all the letters in “coda”

AnagramSolver This is just like making a choice in recursive backtracking: Which could be the first word in our anagram? ail alga angular ant coda eel etc... Which could be the second word in our anagram? ail alga angular ant coda eel etc...

AnagramSolver At each level, we go through all possible words etc... but the letters we have left to work with changes! Which could be in an anagram of “Ada Lovelace”? ail alga angular ant coda eel etc... Which could be in an anagram of “a Lvelae”? ail alga angular ant coda eel etc...

Low-Level Details Clearly there are some low level details here in deciding whether one phrase contains the same letters as another Just like 8 Queens had the Board class for its low-level details, we’ll have a class that handles the low-level details of AnagramSolver This low-level detail class is called LetterInventory as you might have guessed, it keeps track of letters You implemented this for HW #2

LetterInventory LetterInventory has the following methods: (described further in the HW #2 write-up): LetterInventory(String s) LetterInventory add(LetterInventory li) boolean isEmpty() int size() LetterInventory subtract(LetterInventory li) String toString()

AnagramSolver AnagramSolver has a lot in common with 8 Queens I can’t stress this enough! If you understand 8 Queens, writing AnagramSolver shouldn’t be too hard Key questions to ask yourself on this assignment: When am I done? for 8 Queens, we were done when we reached column 9 If I’m not done, what are my options? for 8 Queens, the options were the possible rows for this column How do I make and un-make choices? for 8 Queens, this was placing and removing queens

AnagramSolver You must include two optimizations in your assignment because backtracking is inefficient, we need to gain some speed where we can You must preprocess the dictionary into LetterInventorys you’ll store these in a Map specifically, in a HashMap, which is slightly faster than a TreeMap You must prune the dictionary before starting the recursion by “prune,” we mean remove all the words that couldn’t possibly be in an anagram of the given phrase you need do this only once (before starting the recursion)

Maps Recall that Maps have the following methods: // adds a mapping from the given key to the given value void put(K key, V value) // returns the value mapped to the given key (null if none) V get(K key) // returns true if the map contains a mapping for the given key boolean containsKey(K key) // removes any existing mapping for the given key remove(K key) A HashMap can perform all of these operations in O(1) that’s really fast! this makes HashMaps really useful for many applications