Backtracking •Backtrackingis used to solve problems in which a sequenceof objects is selected from a specified setso 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.
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
has intentionally blurred sections.
Sign up to view the full version.
Backtracking: n-Queens Problem -I- Problem: Place n queens on an n xn 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 x4 x4 x4 = 256 possible solutions. 1 2 3 4 1 2 3 4 Qinrow1is in column 1 1,1 1,2 1,3 1,4 2,1 3,1 4,1 4,4 Checked first. Checked next