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.

Slides:



Advertisements
Similar presentations
Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006.
Advertisements

Adversarial Search We have experience in search where we assume that we are the only intelligent being and we have explicit control over the “world”. Lets.
BackTracking Algorithms
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
October 1, 2012Introduction to Artificial Intelligence Lecture 8: Search in State Spaces II 1 A General Backtracking Algorithm Let us say that we can formulate.
Solving Problems by Searching Currently at Chapter 3 in the book Will finish today/Monday, Chapter 4 next.
Branch & Bound Algorithms
CSC 423 ARTIFICIAL INTELLIGENCE
September 26, 2012Introduction to Artificial Intelligence Lecture 7: Search in State Spaces I 1 After our “Haskell in a Nutshell” excursion, let us move.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
B ACKTRACK SEARCH ALGORITHM. B ACKTRACKING Suppose you have to make a series of decisions, among various choices, where You don’t have enough information.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
November 10, 2009Introduction to Cognitive Science Lecture 17: Game-Playing Algorithms 1 Decision Trees Many classes of problems can be formalized as search.
Constraint Satisfaction Problems
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Using Search in Problem Solving
Ch 13 – Backtracking + Branch-and-Bound
Chapter 5 Outline Formal definition of CSP CSP Examples
2/10/03Tucker, Sec Tucker, Applied Combinatorics, Sec. 3.2, Important Definitions Enumeration: Finding all of the possible paths in a rooted tree.
Backtracking.
Constraint Satisfaction Not all problems are solved by a sequential series of steps. How do we solve other types of problems?
Busby, Dodge, Fleming, and Negrusa. Backtracking Algorithm Is used to solve problems for which a sequence of objects is to be selected from a set such.
Dr. Jouhaina Chaouachi Siala
Tree Searching Breadth First Search Dept First Search.
Artificial Intelligence Lecture 9. Outline Search in State Space State Space Graphs Decision Trees Backtracking in Decision Trees.
Formal Description of a Problem In AI, we will formally define a problem as –a space of all possible configurations where each configuration is called.
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
Introduction to search Chapter 3. Why study search? §Search is a basis for all AI l search proposed as the basis of intelligence l inference l all learning.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
CS 484 – Artificial Intelligence1 Announcements Homework 2 due today Lab 1 due Thursday, 9/20 Homework 3 has been posted Autumn – Current Event Tuesday.
Design and Analysis of Algorithms - Chapter 111 How to tackle those difficult problems... There are two principal approaches to tackling NP-hard problems.
1 Chapter 5 Advanced Search. 2 Chapter 5 Contents l Constraint satisfaction problems l Heuristic repair l The eight queens problem l Combinatorial optimization.
Chapter 12 Coping with the Limitations of Algorithm Power Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
BackTracking CS335. N-Queens The object is to place queens on a chess board in such as way as no queen can capture another one in a single move –Recall.
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.
1 N -Queens via Relaxation Labeling Ilana Koreh ( ) Luba Rashkovsky ( )
Chapter 5 Section 1 – 3 1.  Constraint Satisfaction Problems (CSP)  Backtracking search for CSPs  Local search for CSPs 2.
Games. Adversaries Consider the process of reasoning when an adversary is trying to defeat our efforts In game playing situations one searches down the.
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.
CSCI 5582 Fall 2006 CSCI 5582 Artificial Intelligence Fall 2006 Jim Martin.
Search CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Search in State Spaces Problem solving as search Search consists of –state space –operators –start state –goal states A Search Tree is an efficient way.
Breadth-first and depth-first traversal Prof. Noah Snavely CS1114
Analysis & Design of Algorithms (CSCE 321)
N- Queens Solution with Genetic Algorithm By Mohammad A. Ismael.
Chapter 13 Backtracking Introduction The 3-coloring problem
CSCE350 Algorithms and Data Structure Lecture 21 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Chapter 5 Team Teaching AI (created by Dewi Liliana) PTIIK Constraint Satisfaction Problems.
February 11, 2016Introduction to Artificial Intelligence Lecture 6: Search in State Spaces II 1 State-Space Graphs There are various methods for searching.
Breadth-first and depth-first traversal CS1114
Optimization Problems The previous examples simply find a single solution What if we have a cost associated with each solution and we want to find the.
CSG3F3/ Desain dan Analisis Algoritma
BackTracking CS255.
Depth-First Search N-Queens Problem Hamiltonian Circuits
CSCI 104 Backtracking Search
CSPs: Search and Arc Consistency Computer Science cpsc322, Lecture 12
Design and Analysis of Algorithm
Problem Solving: Brute Force Approaches
Spanning Trees Longin Jan Latecki Temple University based on slides by
Back Tracking.
A General Backtracking Algorithm
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.
Backtracking.
Backtracking and Branch-and-Bound
CPSC 322 Introduction to Artificial Intelligence
Spanning Trees Longin Jan Latecki Temple University based on slides by
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Presentation transcript:

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 a queen can move horizontally, vertically, or diagonally an infinite distance This implies that no two queens can be on the same row, column, or diagonal –We usually want to know how many different placements there are

N-Queens Q 2Q 3Q 4Q 5Q 6Q 7Q 8Q

4-Queens Lets take a look at the simple problem of placing 4 queens on a 4x4 board The brute-force solution is to place the first queen, then the second, third, and forth –After all are placed we determine if they are placed legally There are 16 spots for the first queen, 15 for the second, etc. –Leading to 16*15*14*13 = 43,680 different combinations Obviously this isn’t a good way to solve the problem

4-Queens First lets use the fact that no two queens can be in the same row to help us –That means we get to place a queen in each row So we can place the first queen into the first row, the second into the second, etc. This cuts down on the amount of work –Now there are 4 spots for the first queen, 4 spots for the second, etc. 4*4*4*4 = 256 different combinations

4-Queens However, we can still do better because as we place each queen we can look at the previous queens we have placed to make sure our new queen is not in the same column or diagonal as a previously placed queen Then we could use a Greedy-like strategy to select the next valid position for each row

4-Queens Q Q Q Q xQ Q xxQ Q xxQ Q Q xxQ xQ

Q xxQ xxQ Q xxQ xxxQ

So now what do we do? Well, this is very much like solving a maze –As you walk through the maze you have to make a series of choices –If one of your choices leads to a dead end, you need to back up to the last choice you made and take a different route Change one of your earlier selections –Eventually you will find your way out of the maze

4-Queens Q xxQ xxxQ Q xxxQ Q xxxQ Q Q xxxQ xQ Q xxxQ xQ Q Q xxxQ xQ xQ

Q xxxQ xQ xxQ Q xxxQ xQ xxxQ Q xxxQ xxQ Q xxxQ xxxQ Q xxxQ xQ

xQ xQ Q xQ xQ xQ xxQ xQ xxxQ xQ xxxQ Q

xQ xxxQ Q Q xQ xxxQ Q xQ xQ xxxQ Q xxQ

This type of problem is often viewed as a state-space tree –A tree of all the states that the problem can be in We start with an empty board state at the root and try to work our way down to a leaf node –Leaf nodes are completed boards

4-Queens

Graph Coloring Graph coloring is the problem of coloring each vertex in a graph such that no two adjacent vertices are the same color Some direct examples: –Map coloring –Register assignment

Graph Coloring The same issues apply as in N-Queens –We don’t want to simply pick all subsets Way too many –We want to prune the state-space tree as soon as we find something that won’t work This implies that we need a sequence of vertices to color As we color the next vertex we need to make sure it doesn’t conflict with any of its previously colored neighbors –We may need to backtrack

Graph Coloring C A FB E D As an example: –The vertices are enumerated in order A-F –The colors are given in order: R, G, B

Graph Coloring C A FB E D

C A FB E D

C A FB E D

C A FB E D

C A FB E D

C A FB E D

C A FB E D

C A FB E D

C A FB E D

C A FB E D

C A FB E D

Depth-First Search One can view backtracking as a Depth-First Search through the State-Space –We start our search at the root –We end our search when we find a valid leaf node (or when we explore the entire space) A Depth-First Search expands the deepest node next –A child will be expanded before the child’s siblings are expanded This type of search can get you to the leaf nodes as fast as possible How do you implement a Depth-First Search?

Backtracking Framework We will try and build a generic backtracking framework that works with problems States –All it should need to work with is States State should be generic and not married to any specific problem States should be able to: –Tell us if it has more children –Produce the next child –Tell us if it is solved –Tell us if it is feasible (In class design of the State, NQueens problem specific, and Backtracking framework classes)

Depth vs. Breadth First Search A BHK CDGIJLMP EFNO

A Breadth first search expands all the children at a particular level before advancing to the next level –How can a breadth first search be implemented? –Which is better: a depth or breadth first search? Worst case Big-O?

Depth vs. Breadth First Search A BCD EFGHIJKL MNOP

Practice Problems Foundations of Algorithms Chapter 5: 1, 3, 7, 11, 13, 16, 30, 31