Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
The N-Queens Problem lab01.
CompSci 100e Program Design and Analysis II March 3, 2011 Prof. Rodger CompSci 100e, Spring
Eight queens puzzle. The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard such that none of them are able to capture.
SIGCSE Nifty assignments: Brute force and backtracking Steve Weiss Department of Computer Science University of North Carolina at Chapel Hill
Greedy Algorithms. Announcements Exam #1 ◦ See me for 2 extra points if you got #2(a) wrong. Lab Attendance ◦ 12 Labs, so if anyone needs to miss a lab.
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.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday.
Artificial Intelligence Lecture 9. Outline Search in State Space State Space Graphs Decision Trees Backtracking in Decision Trees.
Announcements.
Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Constraint Satisfaction Problems (CSPs) CPSC 322 – CSP 1 Poole & Mackworth textbook: Sections § Lecturer: Alan Mackworth September 28, 2012.
Dijkstra’s Algorithm. This algorithm finds the shortest path from a source vertex to all other vertices in a weighted directed graph without negative.
Recursion: Backtracking
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
Back Tracking Project Due August 11, 1999 N-queens: –A classic puzzle for chess buffs is the N- Queens problem. Simply stated: is it possible to place.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
CS 312: Algorithm Analysis Lecture #32: Intro. to State-Space Search This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported.
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
Announcements This Wednesday, Class and Labs are cancelled! The last lab is due this Wednesday … how many people are planning on doing it? Finally posted.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
Sudoku Jordi Cortadella Department of Computer Science.
Dynamic Programming. Algorithm Design Techniques We will cover in this class: ◦ Greedy Algorithms ◦ Divide and Conquer Algorithms ◦ Dynamic Programming.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
CSE373: Data Structures & Algorithms Lecture 22: The P vs. NP question, NP-Completeness Lauren Milne Summer 2015.
CSE373: Data Structure & Algorithms Lecture 21: More Sorting and Other Classes of Algorithms Lauren Milne Summer 2015.
CompSci 100e 6.1 Plan for the week l More recursion examples l Backtracking  Exhaustive incremental search  When we a potential solution is invalid,
Backtracking & Brute Force Optimization Intro2CS – weeks
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
Analysis & Design of Algorithms (CSCE 321)
SEARCH Heng Ji Feb 02/05, Search We will consider the problem of designing goal- based agents in fully observable, deterministic, discrete,
February 11, 2016Introduction to Artificial Intelligence Lecture 6: Search in State Spaces II 1 State-Space Graphs There are various methods for searching.
1 CSC 384 Lecture Slides (c) , C. Boutilier and P. Poupart CSC384: Lecture 16  Last time Searching a Graphplan for a plan, and relaxed plan heuristics.
CompSci Problem Solving: Sudoku  Rules of the Game Sudoku is played with a 9 by 9 "board" consisting of nine 3 by 3 sub-boards. The symbols 1 -
Dynamic Programming … Continued
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
1 Tirgul 11: Recursion & Backtracking. 2 Elements of a recursive solution (Reminder) A base case that is so simple we need no computation to solve it.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
CSG3F3/ Desain dan Analisis Algoritma
CSE373: Data Structure & Algorithms Lecture 23: More Sorting and Other Classes of Algorithms Catie Baker Spring 2015.
CSSE 230 Day 25 Skip Lists.
Intro to Computer Science II
CSCI 104 Backtracking Search
Data Structures and Algorithms
Fundamentals of Programming II Backtracking with Stacks
CSE 143 Lecture 19 More Recursive Backtracking
Computer Science cpsc322, Lecture 14
Using a Stack Chapter 6 introduces the stack data type.
Analysis and design of algorithm
Using a Stack Chapter 6 introduces the stack data type.
Jordi Cortadella Department of Computer Science
Exercise: Dice roll sum
adapted from Recursive Backtracking by Mike Scott, UT Austin
The N-queens problem Team: 404 brain not found
Using a Stack Chapter 6 introduces the stack data type.
Haskell Tips You can turn any function that takes two inputs into an infix operator: mod 7 3 is the same as 7 `mod` 3 takeWhile returns all initial.
Using a Stack Chapter 6 introduces the stack data type.
Exercise: Dice roll sum
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Backtracking, Search, Heuristics
Presentation transcript:

Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.

Backtracking Introduction to Backtracking

Backtracking Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating possibilities. A standard example of backtracking would be going through a maze. ◦ At some point in a maze, you might have two options of which direction to go: Junction Portion A Portion B

Backtracking Junction Portion B Portion A One strategy would be to try going through Portion A of the maze. If you get stuck before you find your way out, then you "backtrack" to the junction. At this point in time you know that Portion A will NOT lead you out of the maze, so you then start searching in Portion B

Backtracking Clearly, at a single junction you could have even more than 2 choices. The backtracking strategy says to try each choice, one after the other, ◦ if you ever get stuck, "backtrack" to the junction and try the next choice. If you try all choices and never found a way out, then there IS no solution to the maze. Junction B C A

Backtracking – Eight Queens Problem Find an arrangement of 8 queens on a single chess board such that no two queens are attacking one another. In chess, queens can move all the way down any row, column or diagonal (so long as no pieces are in the way). ◦ Due to the first two restrictions, it's clear that each row and column of the board will have exactly one queen.

The backtracking strategy is as follows: 1)Place a queen on the first available square in row 1. 2)Move onto the next row, placing a queen on the first available square there (that doesn't conflict with the previously placed queens). 3)Continue in this fashion until either: a)you have solved the problem, or b)you get stuck.  When you get stuck, remove the queens that got you there, until you get to a row where there is another valid square to try. Backtracking – Eight Queens Problem Animated Example: k/achtdamen/eight.htm#up Q Q Q Q Q Q Continue…

Backtracking – Eight Queens Problem When we carry out backtracking, an easy way to visualize what is going on is a tree that shows all the different possibilities that have been tried. On the board we will show a visual representation of solving the 4 Queens problem (placing 4 queens on a 4x4 board where no two attack one another).

Backtracking – Eight Queens Problem The neat thing about coding up backtracking, is that it can be done recursively, without having to do all the bookkeeping at once. ◦ Instead, the stack or recursive calls does most of the bookkeeping ◦ (ie, keeping track of which queens we've placed, and which combinations we've tried so far, etc.)

void solveItRec(int perm[], int location, struct onesquare usedList[]) { if (location == SIZE) { printSol(perm); } for (int i=0; i<SIZE; i++) { if (usedList[i] == false) { if (!conflict(perm, location, i)) { perm[location] = i; usedList[i] = true; solveItRec(perm, location+1, usedList); usedList[i] = false; } perm[] - stores a valid permutation of queens from index 0 to location-1. location – the column we are placing the next queen usedList[] – keeps track of the rows in which the queens have already been placed. Found a solution to the problem, so print it! Loop through possible rows to place this queen. Only try this row if it hasn’t been used Check if this position conflicts with any previous queens on the diagonal 1)mark the queen in this row 2)mark the row as used 3)solve the next column location recursively 4)un-mark the row as used, so we can get ALL possible valid solutions.

Backtracking – 8 queens problem - Analysis Another possible brute-force algorithm is generate the permutations of the numbers 1 through 8 (of which there are 8! = 40,320), ◦ and uses the elements of each permutation as indices to place a queen on each row. ◦ Then it rejects those boards with diagonal attacking positions. The backtracking algorithm, is a slight improvement on the permutation method, ◦ constructs the search tree by considering one row of the board at a time, eliminating most non-solution board positions at a very early stage in their construction. ◦ Because it rejects row and diagonal attacks even on incomplete boards, it examines only 15,720 possible queen placements. A further improvement which examines only 5,508 possible queen placements is to combine the permutation based method with the early pruning method: ◦ The permutations are generated depth-first, and the search space is pruned if the partial permutation produces a diagonal attack

Sudoku and Backtracking Another common puzzle that can be solved by backtracking is a Sudoku puzzle. The basic idea behind the solution is as follows: 1)Scan the board to look for an empty square that could take on the fewest possible values based on the simple game constraints. 2)If you find a square that can only be one possible value, fill it in with that one value and continue the algorithm. 3)If no such square exists, place one of the possible numbers for that square in the number and repeat the process. 4)If you ever get stuck, erase the last number placed and see if there are other possible choices for that slot and try those next.

Mazes and Backtracking A final example of something that can be solved using backtracking is a maze. ◦ From your start point, you will iterate through each possible starting move. ◦ From there, you recursively move forward. ◦ If you ever get stuck, the recursion takes you back to where you were, and you try the next possible move. In dealing with a maze, to make sure you don't try too many possibilities, ◦ one should mark which locations in the maze have been visited already so that no location in the maze gets visited twice. ◦ (If a place has already been visited, there is no point in trying to reach the end of the maze from there again.

Backtracking – optional homework problem Determine how many solutions there are to the 5 queens problem. ◦ Demonstrate backtracking for at least 2 solutions to the 5 queens problem, by tracing through the decision tree as shown in class.

References Slides adapted from Arup Guha’s Computer Science II Lecture notes: ctures/ ctures/ Additional material from the textbook: Data Structures and Algorithm Analysis in Java (Second Edition) by Mark Allen Weiss Additional images: xkcd.com