Boggle Game: Backtracking Algorithm Implementation

Slides:



Advertisements
Similar presentations
Can you tell the letter each picture begins with?
Advertisements

Introduction to Algorithms Lecture 12 Prof. Constantinos Daskalakis CLRS
Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Boggle Game: Backtracking Algorithm Implementation
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Hashing – Part III CS 367 – Introduction to Data Structures.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Chapter 8, Part I Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
CompSci 100e Program Design and Analysis II March 3, 2011 Prof. Rodger CompSci 100e, Spring
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Backtracking What is backtracking?
November 10, 2009Introduction to Cognitive Science Lecture 17: Game-Playing Algorithms 1 Decision Trees Many classes of problems can be formalized as search.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Depth-first search COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar to pre-order.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
THE RENJU GAME BY ABHISHEK JAIN, PRANSHU GUPTA & RHYTHM DAS PCLUB SUMMER PROJECT PRESENTATION JUNE, L7 IIT KANPUR MENTOR – SANIL JAIN.
Backtracking.
Depth-First Search Lecture 24 COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2011 See online syllabus (also available through BlueLine): Course goals:
Indian Institute of Technology Hyderabad SOS - The Game A Project by Paritosh J Chandran Prakhar Bharadwaj Ganesh Mahidhar Nagarjun Srinivasan.
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
CS 615: Design & Analysis of Algorithms Chapter 5: Searching Brassard & Bratley Chap.: Chapter 9 Page &
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 See online syllabus (also available through BlueLine): Course goals:
Boggle instructions Boggle is played with 16 random letters, you will play with randomly generated letters. Players have three minutes (shown by the countdown.
Graphs – Part II CS 367 – Introduction to Data Structures.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2006 See online syllabus (also available through Blackboard): Course goals:
CompSci 100e 6.1 Plan for the week l More recursion examples l Backtracking  Exhaustive incremental search  When we a potential solution is invalid,
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Backtracking & Brute Force Optimization Intro2CS – weeks
CompSci Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the.
February 11, 2016Introduction to Artificial Intelligence Lecture 6: Search in State Spaces II 1 State-Space Graphs There are various methods for searching.
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the “right-hand”
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.
Breadth-first and depth-first traversal CS1114
2-dimensional Arrays A 2-dimensional array has rows and columns It is actually an “array of arrays” A Tic-Tac-Toe board is an example of a 3 by 3 2-d array.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Backtracking Algorithm Depth-First Search Text Read Weiss, § 9.6 Depth-First Search and § 10.5 Backtracking Algorithms.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
CSC 427: Data Structures and Algorithm Analysis
CSC 321: Data Structures Fall 2016
BackTracking CS255.
Depth-First Search N-Queens Problem Hamiltonian Circuits
Data Structures Graphs - Terminology
CSC 321: Data Structures Fall 2015
Dynamic Memory Allocation
Csc 2720 Instructor: Zhuojun Duan
Fundamentals of Programming II Backtracking with Stacks
Maze Implementation, Analysis and Design to find Shortest Paths
Data Structures and Algorithms for Information Processing
Comp 245 Data Structures Graphs.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Tree Searching.
COMP171 Depth-First Search.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Backtracking, Search, Heuristics
Presentation transcript:

Boggle Game: Backtracking Algorithm Implementation CS 1501

Boggle Game Read the assignment requirement carefully. http://www.cs.pitt.edu/~kirk/cs1501/Ramirez/Summer2009/assigs/boggle/assig1a.html 2017/4/23

Boggle Game Read the assignment requirement carefully. http://www.cs.pitt.edu/~kirk/cs1501/Ramirez/Summer2009/assigs/boggle/assig1a.html The boggle game is to form as many 3+ letter words as possible from a 2-dimensional board of letters. We want to form words by starting at any letter and traversing over adjacent cubes vertically, horizontally or diagonally. Any cube may appear at most once in a word. Only real words (from dictionary) are valid. Words must be at least 3 letters long to be valid. F R O Y I E S L D N T A 2017/4/23

Boggle Game Read the assignment requirement carefully. http://www.cs.pitt.edu/~kirk/cs1501/Ramirez/Summer2009/assigs/boggle/assig1a.html The boggle game is to form as many 3+ letter words as possible from a 2-dimensional board of letters. We want to form words by starting at any letter and traversing over adjacent cubes vertically, horizontally or diagonally. Any cube may appear at most once in a word. Only real words (from dictionary) are valid. Words must be at least 3 letters long to be valid. F R I E N D F R O Y I E S L D N T A 2017/4/23

Boggle Game Read the assignment requirement carefully. http://www.cs.pitt.edu/~kirk/cs1501/Ramirez/Summer2009/assigs/boggle/assig1a.html The boggle game is to form as many 3+ letter words as possible from a 2-dimensional board of letters. We want to form words by starting at any letter and traversing over adjacent cubes vertically, horizontally or diagonally. Any cube may appear at most once in a word. Only real words (from dictionary) are valid. Words must be at least 3 letters long to be valid. R O S T E R F R O Y I E S L D N T A 2017/4/23

Boggle Game Read the assignment requirement carefully. http://www.cs.pitt.edu/~kirk/cs1501/Ramirez/Summer2009/assigs/boggle/assig1a.html The boggle game is to form as many 3+ letter words as possible from a 2-dimensional board of letters. We want to form words by starting at any letter and traversing over adjacent cubes vertically, horizontally or diagonally. Any cube may appear at most once in a word. Only real words (from dictionary) are valid. Words must be at least 3 letters long to be valid. E N D F I N E R E A L F R O Y I E S L D N T A 2017/4/23

Boggle Game Read the assignment requirement carefully. http://www.cs.pitt.edu/~kirk/cs1501/Ramirez/Summer2009/assigs/boggle/assig1a.html The boggle game is to form as many 3+ letter words as possible from a 2-dimensional board of letters. We want to form words by starting at any letter and traversing over adjacent cubes vertically, horizontally or diagonally. Any cube may appear at most once in a word. Only real words (from dictionary) are valid. Words must be at least 3 letters long to be valid. X D E A D X F Y L D E R E F R O Y I E S L D N T A 2017/4/23

Boggle Game …… Wild card * F R O Y I E S L * N T A F R O Y I E S L A N A * can represent any letter in the alphabet. Wild card essentially causes more word possibilities in a given board. F R O Y I E S L * N T A F R O Y I E S L A N T F R O Y I E S L B N T A F R O Y I E S L D N T A F R O Y I E S L Z N T A …… 2017/4/23

Requirement Read board from text file. Calculate all possible words (of length at least 3). Allow the user to type words from the keyboard and tell him if each word is “valid” or not. Show the user all of the words for the board by printing out all of the words in the board word list in alphabetical order. 2017/4/23

Board Search Start from any cube on the board F R O Y I E S L D N T A Track where we are (current position) Track where we were (history) F R O Y I E S L D N T A 2017/4/23

Board Search Start from any cube on the board j F R O Y I E S L D N T Track where we are (current position) (i, j) Determine where we can go (next position) (i-1, j-1) (i-1, j) (i-1, j+1) (i, j-1) curr (i, j+1) (i+1, j-1)(i+1, j)(i+1, j+1) Track where we were (history) j F R O Y I E S L D N T A (0,0) (0,1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3) (3,0) (3,1) (3,2) (3,3) i 2017/4/23

Backtracking Example Start from any cube on the board j F R O Y I E S Start from D At (2, 1) Next step can be: (1, 0) (1, 1) (1, 2) (2, 0) (2, 2) (3, 0) (3, 1) (3, 2) Try (1, 0) – DY Advance to (1, 0) j F R O Y I E S L D N T A (0,0) (0,1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3) (3,0) (3,1) (3,2) (3,3) i 2017/4/23

Backtracking Example Start from any cube on the board j F R O Y I E S Now at Y (1, 0)-(2,1) At (1, 0) Next step can be: (0, 0) (0, 1) (1, 1) (2, 2) (2, 0) (2, 1) is not allowed! Try all possible steps and find no valid prefix or words Backtrack to D (2, 1) j F R O Y I E S L D N T A (0,0) (0,1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3) (3,0) (3,1) (3,2) (3,3) i 2017/4/23

Backtracking Example Start from any cube on the board j F R O Y I E S Return to D At (2, 1) Next step can be: (1, 1) (1, 2) (2, 0) (2, 2) (3, 0) (3, 1) (3, 2) (1, 0) has failed. Try (1, 1) – DI Advance to (1, 1) …. j F R O Y I E S L D N T A (0,0) (0,1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3) (3,0) (3,1) (3,2) (3,3) i 2017/4/23

New Step Situations When we try a new step on the board, the composition with the new letter can have 4 possible situations: Not a prefix, not a word Give up, backtrack to the previous step (pruning) Not a prefix, is a word Save the word, backtrack to the previous step Is a prefix, not a word Explore the next possible step Is a prefix, is a word Save the word, explore the next possible step The function to check a prefix or a word are provided. 2017/4/23

Big Picture Proceed forward to a solution until it becomes apparent that no solution can be achieved along the current path. At that point UNDO the solution (backtrack) to a point where we can again proceed forward Some hints: Use a stack to track the history path. Push stack: advance step Pop stack: backtrack Use a variable for each cube to track what is the next direction to explore. (stack) Advance step: push the next direction into the stack Backtrack: pop the next direction out of the stack Use a boolean table (2d array) to mark the cubes visited. Advance step: mark new cube Backtrack: unmark cube 2017/4/23