SlideShare una empresa de Scribd logo
The N-Queens
problem
Presented By:
Anoop Chaudhary
A Seminar on
 Introduction to N-Queens Problem
 Constraints and Solution Space
 The Backtracking Approach
 Solution to 4-Queens Problem using backtracking
 Algorithm for N-Queens Problems
 Time complexity of N-Queens algorithm
 Advantages of using backtracking
Contents:
2
What is a queen ?
 A queen is one of six different chess pieces.
 It can move any distance available vertically, horizontally
or diagonally on the 8 x 8 grid of chessboard.
3
Figure 1: Movement of a Queen on a Chessboard
Introduction to N-Queens Problem
4
Place n queens on an n x n chessboard such that no two
queens attack each other.
One solution to each n-queens problem for n=4,6 and 8
4-queens 6-queens 8-queens
History
5
 1848: Problem proposed by Max Bezzel.
 1850: Friedrich Gauss claims Q(8)=92
 1874: Dr. Glaisher prove Gauss’s claim
 1874: Paul proves that Q(n)>0 for all n>=4
 20th Century: Problem became a benchmark for
combinatorial optimization
 N-Queen problem is generalized form of 8-Queens problem
 Assumption: Queen i is to be placed on row i. Hence, all solutions
to the N-queens problem can be represented as n-tuples (x1,
…, xn), where xi is the column on which queen i is placed.
 Explicit Constraints:
 xi are chosen from set Si ={1, …, n} and 1<=i<=n.
So the Solution space consists of nn n-tuples.
 Implicit Constraints:
 No two xi ’s can be same
Now, the solution space reduced to n!
 No two queens can be on the same diagonal.
Constraints of N-Queens Problem
6
(2, 4, 1, 3)
The Solution Space of N-Queens Problem
7
 The solution space for the N-Queens problem can be
represented using a State Space Tree –
 The root of the tree represents 0 choices.
 Nodes at depth 1 represent first choice.
 Nodes at depth 2 represents the second choice, and so on till
depth n.
 Edges from depth i to i+1 are labeled with the values of xi.
 In this tree a path from a root to a leaf represents a candidate
solution and All the candidate solutions define the solution
space.
Example: 4-Queens Problem
8
State Space Tree:
The Backtracking Approach
9
 Backtracking is a standard method to find solutions for a particular
kind of problems known as “Constraint-Satisfaction-Problems”.
 Backtracking is finding the solution of a problem whereby the solution
depends on the previous steps taken. Thus the general steps of
backtracking are :
 Start with a sub-solution (partial candidate)
 Check if partial candidate will lead to the solution or not.
 If not, then come back and change the partial candidate and
continue again.
 Backtracking can be seen as a form of intelligent depth-first search
(DFS).
Example: 4-Queens Problem
a b c d
e f g h
10
State Space tree, generated during backtracking
11
* The B denotes the dead node.
Algorithm for N-Queens Problems
1. Place (k, i)
2. {
3. for j ← 1 to k - 1 do
4. // Check if two queens in same column or in same diagonal
5. if ( x[j] == i ) or ( Abs x[j]) - i) == (Abs (j - k) )
6. then return false;
7. return true;
8. }
 Returns true if a queen can be placed in kth row and ith column. Otherwise returns false.
 x[] is a global array whose first (k-1) values have been set.
 Abs(r) returns the absolute value of r.
12
Algorithm for N-Queens Problems
1. NQueens(k, n)
2. {
3. for i ← 1 to n do
4. {
5. if Place (k, i) then
6. {
7. x[k] ← i;
8. if (k ==n) then write ( x[1 : n] );
9. else NQueens(k + 1, n);
10. }
11. }
12. }
13
Complexity
14
 Time complexity of N queens algorithm : For finding a
single solution where the first queen Q has been assigned
the first column and can be put on N positions, the
second queen has been assigned the second column and
would choose from N-1 possible positions and so on; the
time complexity is O ( N ) * ( N - 1 ) * ( N - 2 ) * … 1 ). i.e
The worst-case time complexity is O(N!) .
 Thus, for finding all the solutions to the N Queens
problem the time complexity runs in polynomial time.
Advantages of using backtracking
15
 Ability to find and count all the possible solutions rather
than just one.
 Parallel version of algorithm can increase speed several
times.
References
16
 “Computer Algorithms” by Ellis Horowitz, Sartaz Sahni and
Rajashekharan printed by Computer Science Press.
 The n-Queens Problem, Article in “The American Mathematical
Monthly” 101(7)- August 1994, DOI:10.2307/2974691
 http://mathcenter.oxford.emory.edu/site/cs171/nQueensProblemAnd
Backtracking/
N-queens.pptx

Más contenido relacionado

Similar a N-queens.pptx

module5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdfmodule5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdf
Shiwani Gupta
 
Unit 4 jwfiles
Unit 4 jwfilesUnit 4 jwfiles
Unit 4 jwfiles
Nv Thejaswini
 
N Queens problem
N Queens problemN Queens problem
N Queens problem
Arkadeep Dey
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
Philip Schwarz
 
Stochastic Process Exam Help
Stochastic Process Exam HelpStochastic Process Exam Help
Stochastic Process Exam Help
Statistics Exam Help
 
algorithm Unit 4
algorithm Unit 4 algorithm Unit 4
algorithm Unit 4
Monika Choudhery
 
Stochastic Process Assignment Help
Stochastic Process Assignment HelpStochastic Process Assignment Help
Stochastic Process Assignment Help
Statistics Assignment Help
 
Backtracking Algorithm.pptx
Backtracking Algorithm.pptxBacktracking Algorithm.pptx
Backtracking Algorithm.pptx
sangeeta194160
 
Design and analysis of algorithms unit 6 non deterministic polynomial
Design and analysis of algorithms unit 6 non deterministic polynomialDesign and analysis of algorithms unit 6 non deterministic polynomial
Design and analysis of algorithms unit 6 non deterministic polynomial
nowicam882
 
8 QUEENS PROBLEM.pptx
8 QUEENS PROBLEM.pptx8 QUEENS PROBLEM.pptx
8 QUEENS PROBLEM.pptx
sunidhi740916
 
N queens using backtracking
N queens using backtrackingN queens using backtracking
N queens using backtracking
srilekhagourishetty
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
MuradAmn
 
Unit 3 greedy method
Unit 3  greedy methodUnit 3  greedy method
Unit 3 greedy method
MaryJacob24
 
Unit 3 - Greedy Method
Unit 3  - Greedy MethodUnit 3  - Greedy Method
Unit 3 - Greedy Method
MaryJacob24
 
N Queen Problem
N Queen Problem N Queen Problem
N Queen Problem
FoodieVlogger
 
Backtracking
BacktrackingBacktracking
Backtracking
Sally Salem
 
N queen puzzle
N queen puzzleN queen puzzle
N queen puzzle
Akash Sethiya
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
mandlapure
 
Backtracking Basics.pptx
Backtracking Basics.pptxBacktracking Basics.pptx
Backtracking Basics.pptx
sunidhi740916
 
Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2
Techglyphs
 

Similar a N-queens.pptx (20)

module5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdfmodule5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdf
 
Unit 4 jwfiles
Unit 4 jwfilesUnit 4 jwfiles
Unit 4 jwfiles
 
N Queens problem
N Queens problemN Queens problem
N Queens problem
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
 
Stochastic Process Exam Help
Stochastic Process Exam HelpStochastic Process Exam Help
Stochastic Process Exam Help
 
algorithm Unit 4
algorithm Unit 4 algorithm Unit 4
algorithm Unit 4
 
Stochastic Process Assignment Help
Stochastic Process Assignment HelpStochastic Process Assignment Help
Stochastic Process Assignment Help
 
Backtracking Algorithm.pptx
Backtracking Algorithm.pptxBacktracking Algorithm.pptx
Backtracking Algorithm.pptx
 
Design and analysis of algorithms unit 6 non deterministic polynomial
Design and analysis of algorithms unit 6 non deterministic polynomialDesign and analysis of algorithms unit 6 non deterministic polynomial
Design and analysis of algorithms unit 6 non deterministic polynomial
 
8 QUEENS PROBLEM.pptx
8 QUEENS PROBLEM.pptx8 QUEENS PROBLEM.pptx
8 QUEENS PROBLEM.pptx
 
N queens using backtracking
N queens using backtrackingN queens using backtracking
N queens using backtracking
 
04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
 
Unit 3 greedy method
Unit 3  greedy methodUnit 3  greedy method
Unit 3 greedy method
 
Unit 3 - Greedy Method
Unit 3  - Greedy MethodUnit 3  - Greedy Method
Unit 3 - Greedy Method
 
N Queen Problem
N Queen Problem N Queen Problem
N Queen Problem
 
Backtracking
BacktrackingBacktracking
Backtracking
 
N queen puzzle
N queen puzzleN queen puzzle
N queen puzzle
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
 
Backtracking Basics.pptx
Backtracking Basics.pptxBacktracking Basics.pptx
Backtracking Basics.pptx
 
Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2
 

Último

ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
bjmsejournal
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
PKavitha10
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
gowrishankartb2005
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
cnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classicationcnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classication
SakkaravarthiShanmug
 
Welding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdfWelding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdf
AjmalKhan50578
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
People as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimalaPeople as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimala
riddhimaagrawal986
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
Mahmoud Morsy
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Software Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.pptSoftware Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.ppt
TaghreedAltamimi
 

Último (20)

ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
cnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classicationcnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classication
 
Welding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdfWelding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdf
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
People as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimalaPeople as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimala
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Software Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.pptSoftware Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.ppt
 

N-queens.pptx

  • 2.  Introduction to N-Queens Problem  Constraints and Solution Space  The Backtracking Approach  Solution to 4-Queens Problem using backtracking  Algorithm for N-Queens Problems  Time complexity of N-Queens algorithm  Advantages of using backtracking Contents: 2
  • 3. What is a queen ?  A queen is one of six different chess pieces.  It can move any distance available vertically, horizontally or diagonally on the 8 x 8 grid of chessboard. 3 Figure 1: Movement of a Queen on a Chessboard
  • 4. Introduction to N-Queens Problem 4 Place n queens on an n x n chessboard such that no two queens attack each other. One solution to each n-queens problem for n=4,6 and 8 4-queens 6-queens 8-queens
  • 5. History 5  1848: Problem proposed by Max Bezzel.  1850: Friedrich Gauss claims Q(8)=92  1874: Dr. Glaisher prove Gauss’s claim  1874: Paul proves that Q(n)>0 for all n>=4  20th Century: Problem became a benchmark for combinatorial optimization  N-Queen problem is generalized form of 8-Queens problem
  • 6.  Assumption: Queen i is to be placed on row i. Hence, all solutions to the N-queens problem can be represented as n-tuples (x1, …, xn), where xi is the column on which queen i is placed.  Explicit Constraints:  xi are chosen from set Si ={1, …, n} and 1<=i<=n. So the Solution space consists of nn n-tuples.  Implicit Constraints:  No two xi ’s can be same Now, the solution space reduced to n!  No two queens can be on the same diagonal. Constraints of N-Queens Problem 6 (2, 4, 1, 3)
  • 7. The Solution Space of N-Queens Problem 7  The solution space for the N-Queens problem can be represented using a State Space Tree –  The root of the tree represents 0 choices.  Nodes at depth 1 represent first choice.  Nodes at depth 2 represents the second choice, and so on till depth n.  Edges from depth i to i+1 are labeled with the values of xi.  In this tree a path from a root to a leaf represents a candidate solution and All the candidate solutions define the solution space.
  • 9. The Backtracking Approach 9  Backtracking is a standard method to find solutions for a particular kind of problems known as “Constraint-Satisfaction-Problems”.  Backtracking is finding the solution of a problem whereby the solution depends on the previous steps taken. Thus the general steps of backtracking are :  Start with a sub-solution (partial candidate)  Check if partial candidate will lead to the solution or not.  If not, then come back and change the partial candidate and continue again.  Backtracking can be seen as a form of intelligent depth-first search (DFS).
  • 10. Example: 4-Queens Problem a b c d e f g h 10
  • 11. State Space tree, generated during backtracking 11 * The B denotes the dead node.
  • 12. Algorithm for N-Queens Problems 1. Place (k, i) 2. { 3. for j ← 1 to k - 1 do 4. // Check if two queens in same column or in same diagonal 5. if ( x[j] == i ) or ( Abs x[j]) - i) == (Abs (j - k) ) 6. then return false; 7. return true; 8. }  Returns true if a queen can be placed in kth row and ith column. Otherwise returns false.  x[] is a global array whose first (k-1) values have been set.  Abs(r) returns the absolute value of r. 12
  • 13. Algorithm for N-Queens Problems 1. NQueens(k, n) 2. { 3. for i ← 1 to n do 4. { 5. if Place (k, i) then 6. { 7. x[k] ← i; 8. if (k ==n) then write ( x[1 : n] ); 9. else NQueens(k + 1, n); 10. } 11. } 12. } 13
  • 14. Complexity 14  Time complexity of N queens algorithm : For finding a single solution where the first queen Q has been assigned the first column and can be put on N positions, the second queen has been assigned the second column and would choose from N-1 possible positions and so on; the time complexity is O ( N ) * ( N - 1 ) * ( N - 2 ) * … 1 ). i.e The worst-case time complexity is O(N!) .  Thus, for finding all the solutions to the N Queens problem the time complexity runs in polynomial time.
  • 15. Advantages of using backtracking 15  Ability to find and count all the possible solutions rather than just one.  Parallel version of algorithm can increase speed several times.
  • 16. References 16  “Computer Algorithms” by Ellis Horowitz, Sartaz Sahni and Rajashekharan printed by Computer Science Press.  The n-Queens Problem, Article in “The American Mathematical Monthly” 101(7)- August 1994, DOI:10.2307/2974691  http://mathcenter.oxford.emory.edu/site/cs171/nQueensProblemAnd Backtracking/