SlideShare a Scribd company logo
1 of 39
Backtracking
Introduction to Backtracking
BACK TRACKING
• Backtracking is a general algorithm for finding all (or
some) solutions to some computational problem, that
incrementally builds candidates to the solutions, and
abandons each partial candidate ‘c’ ("backtracks") as soon
as it determines that ‘c’ cannot possibly be completed to a
valid solution.
• Backtracking is an important tool for solving constraint
satisfaction problems, such as crosswords, verbal
arithmetic, Sudoku, and many other puzzles.
Backtracking
 Suppose you have to make a series of decisions,
among various choices, where
◦ You don’t have enough information to know what to
choose
◦ Each decision leads to a new set of choices
◦ Some sequence of choices (possibly more than one) may
be a solution to your problem
 Backtracking is a methodical way of trying out
various sequences of decisions, until you find one
that “works”
WHAT IS 8 QUEEN PROBLEM?
• The eight queens puzzle is the problem of placing
eight chess queens on an 8×8 chessboard so that no two
queens attack each other.
• Thus, a solution requires that no two queens share the
same row, column, or diagonal.
• The eight queens puzzle is an example of the more
general n-queens problem of placing n queens on
an n×n chessboard, where solutions exist for all natural
numbers n with the exception of 1, 2 and 3.
• The solution possibilities are discovered only up to 23
queen.
Example: The n-Queen problem
 Place n queens on an n by n chess board so that no
two of them are on the same row, column, or
diagonal
BACKTRACKING DEMO FOR 4 QUEENS
STEPS REVISITED - BACKTRACKING
1. Place the first queen in the left upper corner of the table.
2. Save the attacked positions.
3. Move to the next queen (which can only be placed to the next
line).
4. Search for a valid position. If there is one go to step 8.
5. There is not a valid position for the queen. Delete it (the x
coordinate is 0).
6. Move to the previous queen.
7. Go to step 4.
8. Place it to the first valid position.
9. Save the attacked positions.
10. If the queen processed is the last stop otherwise go to step 3.
COUNTING SOLUTIONS
• The following table gives the number of distinct
solutions for placing n queens on an n × n board, for
n=1–26.
• Note that the six queens puzzle has fewer solutions
than the five queens puzzle.
• There is currently no known formula for the exact
number of solutions.
Order
(“N”) Total Solutions Exec time
---------------------------------------------
1 1 < 0 seconds
2 0 < 0 seconds
3 0 < 0 seconds
4 2 < 0 seconds
5 10 < 0 seconds
6 4 < 0 seconds
7 40 < 0 seconds
8 92 < 0 seconds
9 352 < 0 seconds
10 724 < 0 seconds
11 2,680 < 0 seconds
12 14,200 < 0 seconds
13 73,712 < 0 seconds
14 365,596 0.2s
15 2,279,184 1.9 s
16 14,772,512 11.2 s
17 95,815,104 77.2 s
18 666,090,624 9.6 m
19 4,968,057,848 75.0 m
20 39,029,188,884 10.2 h
21 314,666,222,712 87.2 h
22 2,691,008,701,644 31.9
23 24,233,937,684,440 296 d
24 227,514,171,973,736 ?
25 2,207,893,435,808,352 ?
26 22,317,699,616,364,044 ?
(s = seconds m = minutes h = hours d = days)
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
Q
Q
Q
Q
Q Q
Continue…
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

More Related Content

Similar to Backtracking Basics.pptx

Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycleBacktracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cyclevarun arora
 
N Queen Algorithm
N Queen AlgorithmN Queen Algorithm
N Queen AlgorithmA.I. Tazib
 
data structures- back tracking
data structures- back trackingdata structures- back tracking
data structures- back trackingAbinaya B
 
Backtracking Algorithm.pptx
Backtracking Algorithm.pptxBacktracking Algorithm.pptx
Backtracking Algorithm.pptxsangeeta194160
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesFahim Ferdous
 
Chessboard Puzzles Part 2 - Independence
Chessboard Puzzles Part 2 - IndependenceChessboard Puzzles Part 2 - Independence
Chessboard Puzzles Part 2 - IndependenceDan Freeman
 
N-queens.pptx
N-queens.pptxN-queens.pptx
N-queens.pptxanoop2708
 
Algorithm Performance For Chessboard Separation Problems
Algorithm Performance For Chessboard Separation ProblemsAlgorithm Performance For Chessboard Separation Problems
Algorithm Performance For Chessboard Separation ProblemsTina Gabel
 
Constrain satisfaction in artificial intelligence.pptx
Constrain satisfaction in artificial intelligence.pptxConstrain satisfaction in artificial intelligence.pptx
Constrain satisfaction in artificial intelligence.pptxKalpana Mohan
 
INTERVIEW TIPS HOW TO TACKLE SOFT SKILLS INTELLIGENT AND SMART ANSWERS BY SOU...
INTERVIEW TIPS HOW TO TACKLE SOFT SKILLS INTELLIGENT AND SMART ANSWERS BY SOU...INTERVIEW TIPS HOW TO TACKLE SOFT SKILLS INTELLIGENT AND SMART ANSWERS BY SOU...
INTERVIEW TIPS HOW TO TACKLE SOFT SKILLS INTELLIGENT AND SMART ANSWERS BY SOU...SOURAV DAS
 
Equations and tilings
Equations and tilingsEquations and tilings
Equations and tilingsnavajomath
 

Similar to Backtracking Basics.pptx (20)

Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycleBacktracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
 
N Queen Algorithm
N Queen AlgorithmN Queen Algorithm
N Queen Algorithm
 
data structures- back tracking
data structures- back trackingdata structures- back tracking
data structures- back tracking
 
Backtracking Algorithm.pptx
Backtracking Algorithm.pptxBacktracking Algorithm.pptx
Backtracking Algorithm.pptx
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
Back tracking
Back trackingBack tracking
Back tracking
 
backtracking 8 Queen.pptx
backtracking 8 Queen.pptxbacktracking 8 Queen.pptx
backtracking 8 Queen.pptx
 
N queens using backtracking
N queens using backtrackingN queens using backtracking
N queens using backtracking
 
Chessboard Puzzles Part 2 - Independence
Chessboard Puzzles Part 2 - IndependenceChessboard Puzzles Part 2 - Independence
Chessboard Puzzles Part 2 - Independence
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Stochastic Process Exam Help
Stochastic Process Exam HelpStochastic Process Exam Help
Stochastic Process Exam Help
 
Stochastic Process Assignment Help
Stochastic Process Assignment HelpStochastic Process Assignment Help
Stochastic Process Assignment Help
 
N-queens.pptx
N-queens.pptxN-queens.pptx
N-queens.pptx
 
Algorithm Performance For Chessboard Separation Problems
Algorithm Performance For Chessboard Separation ProblemsAlgorithm Performance For Chessboard Separation Problems
Algorithm Performance For Chessboard Separation Problems
 
algorithm Unit 4
algorithm Unit 4 algorithm Unit 4
algorithm Unit 4
 
Constrain satisfaction in artificial intelligence.pptx
Constrain satisfaction in artificial intelligence.pptxConstrain satisfaction in artificial intelligence.pptx
Constrain satisfaction in artificial intelligence.pptx
 
N queen puzzle
N queen puzzleN queen puzzle
N queen puzzle
 
N Queens problem
N Queens problemN Queens problem
N Queens problem
 
INTERVIEW TIPS HOW TO TACKLE SOFT SKILLS INTELLIGENT AND SMART ANSWERS BY SOU...
INTERVIEW TIPS HOW TO TACKLE SOFT SKILLS INTELLIGENT AND SMART ANSWERS BY SOU...INTERVIEW TIPS HOW TO TACKLE SOFT SKILLS INTELLIGENT AND SMART ANSWERS BY SOU...
INTERVIEW TIPS HOW TO TACKLE SOFT SKILLS INTELLIGENT AND SMART ANSWERS BY SOU...
 
Equations and tilings
Equations and tilingsEquations and tilings
Equations and tilings
 

Recently uploaded

HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 

Recently uploaded (20)

Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 

Backtracking Basics.pptx

  • 2. BACK TRACKING • Backtracking is a general algorithm for finding all (or some) solutions to some computational problem, that incrementally builds candidates to the solutions, and abandons each partial candidate ‘c’ ("backtracks") as soon as it determines that ‘c’ cannot possibly be completed to a valid solution. • Backtracking is an important tool for solving constraint satisfaction problems, such as crosswords, verbal arithmetic, Sudoku, and many other puzzles.
  • 3. Backtracking  Suppose you have to make a series of decisions, among various choices, where ◦ You don’t have enough information to know what to choose ◦ Each decision leads to a new set of choices ◦ Some sequence of choices (possibly more than one) may be a solution to your problem  Backtracking is a methodical way of trying out various sequences of decisions, until you find one that “works”
  • 4. WHAT IS 8 QUEEN PROBLEM? • The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens attack each other. • Thus, a solution requires that no two queens share the same row, column, or diagonal. • The eight queens puzzle is an example of the more general n-queens problem of placing n queens on an n×n chessboard, where solutions exist for all natural numbers n with the exception of 1, 2 and 3. • The solution possibilities are discovered only up to 23 queen.
  • 5. Example: The n-Queen problem  Place n queens on an n by n chess board so that no two of them are on the same row, column, or diagonal
  • 6.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33. STEPS REVISITED - BACKTRACKING 1. Place the first queen in the left upper corner of the table. 2. Save the attacked positions. 3. Move to the next queen (which can only be placed to the next line). 4. Search for a valid position. If there is one go to step 8. 5. There is not a valid position for the queen. Delete it (the x coordinate is 0). 6. Move to the previous queen. 7. Go to step 4. 8. Place it to the first valid position. 9. Save the attacked positions. 10. If the queen processed is the last stop otherwise go to step 3.
  • 34. COUNTING SOLUTIONS • The following table gives the number of distinct solutions for placing n queens on an n × n board, for n=1–26. • Note that the six queens puzzle has fewer solutions than the five queens puzzle. • There is currently no known formula for the exact number of solutions.
  • 35. Order (“N”) Total Solutions Exec time --------------------------------------------- 1 1 < 0 seconds 2 0 < 0 seconds 3 0 < 0 seconds 4 2 < 0 seconds 5 10 < 0 seconds 6 4 < 0 seconds 7 40 < 0 seconds 8 92 < 0 seconds 9 352 < 0 seconds 10 724 < 0 seconds 11 2,680 < 0 seconds 12 14,200 < 0 seconds 13 73,712 < 0 seconds 14 365,596 0.2s
  • 36. 15 2,279,184 1.9 s 16 14,772,512 11.2 s 17 95,815,104 77.2 s 18 666,090,624 9.6 m 19 4,968,057,848 75.0 m 20 39,029,188,884 10.2 h 21 314,666,222,712 87.2 h 22 2,691,008,701,644 31.9 23 24,233,937,684,440 296 d 24 227,514,171,973,736 ? 25 2,207,893,435,808,352 ? 26 22,317,699,616,364,044 ? (s = seconds m = minutes h = hours d = days)
  • 37. 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.
  • 38.  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 Q Q Q Q Q Q Continue…
  • 39. 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

Editor's Notes

  1. 12