Download presentation
Presentation is loading. Please wait.
Published byJohn Lee Modified over 9 years ago
1
Backtracking
2
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
3
N-Queens 12345678 1Q 2Q 3Q 4Q 5Q 6Q 7Q 8Q
4
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
5
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
6
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
7
4-Queens Q Q Q Q xQ Q xxQ Q xxQ Q Q xxQ xQ
8
Q xxQ xxQ Q xxQ xxxQ
9
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
10
4-Queens Q xxQ xxxQ Q xxxQ Q xxxQ Q Q xxxQ xQ Q xxxQ xQ Q Q xxxQ xQ xQ
11
Q xxxQ xQ xxQ Q xxxQ xQ xxxQ Q xxxQ xxQ Q xxxQ xxxQ Q xxxQ xQ
12
xQ xQ Q xQ xQ xQ xxQ xQ xxxQ xQ xxxQ Q
13
xQ xxxQ Q Q xQ xxxQ Q xQ xQ xxxQ Q xxQ
14
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
15
4-Queens 1 1234 2 1234 1243 1243 1234 1 123
16
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
17
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
18
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
19
Graph Coloring C A FB E D
20
C A FB E D
21
C A FB E D
22
C A FB E D
23
C A FB E D
24
C A FB E D
25
C A FB E D
26
C A FB E D
27
C A FB E D
28
C A FB E D
29
C A FB E D
30
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?
31
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)
32
Depth vs. Breadth First Search A BHK CDGIJLMP EFNO
33
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?
34
Depth vs. Breadth First Search A BCD EFGHIJKL MNOP
35
Practice Problems Foundations of Algorithms Chapter 5: 1, 3, 7, 11, 13, 16, 30, 31
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.