The N-Queens Problem lab01.

Slides:



Advertisements
Similar presentations
Polynomial Time Algorithms for the N-Queen Problem Rok sosic and Jun Gu.
Advertisements

 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
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.
SEARCH APPLICATIONS David Kauchak CS30 – Spring 2015.
Data Structures and Algorithms
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?
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
Call Stacks John Keyser. Stacks A very basic data structure. – Data structure: a container for holding data in a program. – Classes, structs can be thought.
Data Structure Dr. Mohamed Khafagy. Stacks Stack: what is it? ADT Applications Implementation(s)
Vlad Furash & Steven Wine.  Problem surfaced in 1848 by chess player Max Bezzel as 8 queens (regulation board size)  Premise is to place N queens on.
Artificial Intelligence Lecture 9. Outline Search in State Space State Space Graphs Decision Trees Backtracking in Decision Trees.
Recursion Chapter 5.
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.
Solving N-Queens in Clojure
Recursion When to use it and when not to use it. Basics of Recursion Recursion uses a method Recursion uses a method Within that method a call is made.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
March 1, 2009 Dr. Muhammed Al-Mulhem 1 ICS 482 Natural Language Processing LR Parsing Muhammed Al-Mulhem March 1, 2009.
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze  Certain mazes can be solved using the “right-hand”
1 Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking.
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"?
CompSci 100e 6.1 Plan for the week l More recursion examples l Backtracking  Exhaustive incremental search  When we a potential solution is invalid,
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Backtracking & Brute Force Optimization Intro2CS – weeks
The n queens problem Many solutions to a classic problem: On an n x n chess board, place n queens so no queen threatens another.
N- Queens Solution with Genetic Algorithm By Mohammad A. Ismael.
DATA STRUCTURES Application of Stack – Infix to Postfix conversion a Joshua Presentation.
An Analysis of the n- Queens problem Saleem Karamali.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Eight Queens Problem The problem is to place 8 queens on a chess board so that none of them can attack the other. A chess board can be considered a plain.
Constraints Satisfaction Edmondo Trentin, DIISM. Constraint Satisfaction Problems: Local Search In many optimization problems, the path to the goal is.
Local search algorithms In many optimization problems, the path to the goal is irrelevant; the goal state itself is the solution State space = set of "complete"
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
ADT Stack What Is a Stack? Standard Template Library Stack Class
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Articulation Points 2 of 2 (Algorithm)
CSC212 Data Structure - Section AB
Using a Stack Chapter 6 introduces the stack data type.
1.5 Matricies.
Intro to Computer Science II
CSCI 104 Backtracking Search
CS 270 Math Foundations of CS
Data Structures and Algorithms
Fundamentals of Programming II Backtracking with Stacks
Sit-In Lab 1 Ob-CHESS-ion
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"?
Using a Stack Chapter 6 introduces the stack data type.
Recursion Copyright (c) Pearson All rights reserved.
Using a Stack Chapter 6 introduces the stack data type.
QQ: Are speed, velocity, and acceleration the same thing
Stacks.
adapted from Recursive Backtracking by Mike Scott, UT Austin
Using a Stack Chapter 6 introduces the stack data type.
Using a Stack Chapter 6 introduces the stack data type.
Exercise: Dice roll sum Write a method diceSum similar to diceRoll, but it also accepts a desired sum and prints only arrangements that add up to.
The n queens problem Many solutions to a classic problem:
Exercise: Dice roll sum
© T Madas.
Exercise: Dice roll sum
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"?
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
N-ლაზიერის ამოცანა ვთქვათ, მოცემულია საჭადრაკო დაფა და 8 ლაზიერი
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

The N-Queens Problem lab01

The N-Queens Problem Suppose you have N chess queens… …and a N by N chess board Can the queens be placed on board without conflicts? Conflict: Any two queens on board attacking each other is a conflict.

Attacking of queens Two queens are attacking each other if they are in the same row in the same column in the same diagonal

Denoting the positions of queens A 4 by 4 chess board. (4 rows, 4 columns) row 3, column 1 row 2, column 3 row 1, column 1

A 8 by 8 chess board. (8 rows, 8 columns)

Pseudocode of N-queens Problem Initialize a stack s where we can keep track of the placement of queens Place the first queen, push its position onto s and set filled to 0. int filled =0;

Continued… Repeat these steps: if there are no conflicts else if there is a conflict and there is room to shift the current queen rightward else if there is a conflict and there is no room to shift the current queen rightward increase filled by 1. If filled is now N, then algorithm is done. Otherwise move to the next row and place a queen in the first column. Push its position onto the stack. Move the current queen rightward, adjusting the record on top of stack to indicate the new position. Keep popping the stack, and decrease filled by 1 until a row is reached where the queen can be shifted rightward. Shift the queen rightward and adjust the record on top of stack to indicate the new position.

Example:4-queues problem(4 queens on a 4 by 4 board) Initialize a stack s where we can keep track of the placement of queens top Stack s bottom

Place the first queen, push its position onto s and set filled to 0. int filled =0;

there are no conflicts, increase filled by 1.

move to the next row (row 2) and place a queen in the first column move to the next row (row 2) and place a queen in the first column. Push its position onto the stack.

If there is a conflict (yellow line), then we shift the new queen to the next column. (rightward ). Adjust the record on top of stack to indicate the new position (ROW 2, COL 2).

If there is a conflict (yellow line), then we shift the new queen to the next column. (rightward ). Adjust the record on top of stack to indicate the new position (ROW 2, COL 3). ROW 2, COL 3

there are no conflicts, increase filled by 1. ROW 2, COL 3

move to the next row and place a queen in the first column move to the next row and place a queen in the first column. Push its position onto the stack. ROW 3, COL 1

there is a conflict and there is room to shift the current queen rightward. Move the current queen rightward, adjusting the record on top of stack to indicate the new position. (ROW 2, COL 2)

there is a conflict and there is room to shift the current queen rightward. Move the current queen rightward, adjusting the record on top of stack to indicate the new position. (ROW 2, COL 3)

there is a conflict and there is room to shift the current queen rightward. Move the current queen rightward, adjusting the record on top of stack to indicate the new position. (ROW 2, COL 4)

there is a conflict and there is no room to shift the current queen rightward. Keep popping the stack, and decrease filled by 1 until a row is reached where the queen can be shifted rightward. Shift the queen rightward. Adjust the record on top of stack to indicate the new position (ROW 2, COL 4). ROW 2, COL 4

there are no conflicts, increase filled by 1 move to the next row and place a queen in the first column. Push its position onto the stack (ROW2, COL 1).

there is a conflict and there is room to shift the current queen rightward. Move the current queen rightward, adjusting the record on top of stack to indicate the new position. ROW 3, COL 1 ROW 3, COL 2

3 3 there are no conflicts. Increase filled by 1. move to the next row and place a queen in the first column. Push its position onto the stack. ROW 4, COL 1 ROW 3, COL 2 ROW 3, COL 2 3 3

there is a conflict and there is room to shift the current queen rightward. Move the current queen rightward, adjusting the record on top of stack to indicate the new position. ROW 4, COL 1 ROW 4, COL 2 ROW 3, COL 2 ROW 3, COL 2 3 3

there is a conflict and there is room to shift the current queen rightward. Move the current queen rightward, adjusting the record on top of stack to indicate the new position. ROW 4, COL 2 ROW 4, COL 3 ROW 3, COL 2 ROW 3, COL 2 3 3

ROW 4, COL 3 ROW 4, COL 4 ROW 3, COL 2 ROW 3, COL 2 3 3

there is a conflict and there is no room to shift the current queen rightward Keep popping the stack, and decrease filled by 1 until a row is reached where the queen can be shifted rightward. Shift the queen rightward. Adjust the record on top of stack to indicate the new position (ROW , COL ) ROW 4, COL 4 ROW 3, COL 2 ROW 3, COL 3 3 2

ROW 3, COL 3 ROW 3, COL 4 2 2

there is a conflict and there is no room to shift the current queen rightward Keep popping the stack, and decrease filled by 1 until a row is reached where the queen can be shifted rightward. Shift the queen rightward. Adjust the record on top of stack to indicate the new position (ROW , COL ) ROW 2, COL 4 ROW 2, COL 4 ROW 1, COL 1 2 1=filled

Keep popping the stack, because current queen can not move rightward. decrease filled by 1 Shift the queen rightward. Adjust the record on top of stack to indicate the new position (ROW 1 , COL 2). ROW 2, COL 4 ROW 1, COL 1 ROW 1, COL 2 1=filled 0=filled

2=filled 1=filled Two steps are omitted here. ROW 1, COL 2

ROW 3, COL 1 ROW 2, COL 4 ROW 1, COL 2 3=filled

4=filled if there are no conflicts increase filled by 1. If filled is now N(N=4), then algorithm is done. ROW 4, COL 3 ROW 3, COL 1 ROW 2, COL 4 ROW 1, COL 2 4=filled