Lecture 8 - Backtracking

Lecture 8 - Backtracking - Backtracking Backtracking...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Backtracking
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Backtracking Backtracking is used to solve problems in which a sequence of objects is selected from a specified set so that the sequence satisfies some criterion. Backtracking is used to solve a class of problems known as Constraint Satisfaction Problems (CSP). Backtracking is a refinement of the brute force approach. In the brute force approach, we systematically search for a solution to a problem among all available options. All possible solutions defines the State space of the problem and can be represented as a tree called the State Space Tree . Backtracking is a modified depth-first search of a tree. Often the goal is to find any feasible solution rather than an optimal solution.
Background image of page 2
Depth-First Search of a Tree In backtracking we traverse the state-space tree in a depth first order. Consider the state-space tree shown below: Nodes are numbered in the order of a depth-first traversal of the tree. 1 2 3 7 8 4 5 6 11 12 16 13 14 15 9 10 Depth-first traversal: Follow a path as deep as possible until a leaf is reached. At a leaf, back up until reach a node with unvisited child. Repeat again
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Backtracking: n-Queens Problem -I- Problem : Place n queens on an n x n chessboard so that no two queens threaten each other. No two queens threaten each other: Not in the same row. Assign each queen to a different row. Each queen can be placed in one of the four columns 4 x 4 x 4 x 4 = 256 possible solutions. 1 2 3 4 1 2 3 4 Q in row 1 is in column 1 1,1 1,2 1,3 1,4 2,1 3,1 4,1 4,4 Checked first. Checked next
Background image of page 4
Backtracking: n-Queens Problem -II- Make the search more efficient : – Not in the same column. – Not in the same diagonal. 1,1 1,2 2,1 x x 2,2 x x x x x x x 3,2 x x x x 4,1 4,4 x x x 2,1 2,2 2,3 2,4 3,1 4,1 4,2 4,3 x x x x x Results in a Pruned Tree 1 2 3 4 1 2 3 4 x x x x
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Backtracking: After determining that a node leads to a “dead end” go back to the node’s parent and proceed with search on next child. Nonpromising nodes : Cannot lead to a solution Promising Nodes : May lead to a solution Backtracking Revisited 1,1 1,2 2,1 x x 2,2 x x x x x x x 3,2 x x x x 4,1 4,4 x x x 2,1 2,2 2,3 2,4 3,1 4,1 4,2 4,3 x x x x x Dead end nonpromising promising
Background image of page 6
void checknode( node v) { node u; if (promising(v)) if (there is a solution at v) write the solution; else for (each child u of v) checknode(u); } General Algorithm for Backtracking void df_tree_search( node v) { node u; visit v; for (each child u of v) df_tree_search(u); }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/14/2011 for the course CMPS 323 taught by Professor Shaban during the Spring '11 term at Qatar University.

Page1 / 17

Lecture 8 - Backtracking - Backtracking Backtracking...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online