Docsity

Backtracking Algorithms: Concept, Illustration, and Implementation with Examples, Study notes of Computer Architecture and Organization

Partial preview of the text

Download Backtracking Algorithms: Concept, Illustration, and Implementation with Examples and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Data and File Structures

Backtracking

Backtracking

  • (^) Problem:

Find out all 3-bit binary numbers for which the sum of the 1's is

greater than or equal to 2.

  • (^) The only way to solve this problem is to check all the possibilities:
  • (^) The 8 possibilities are called the search space of the problem. They

can be organized into a tree.

Backtracking: Illustration

0 0 _
0 1 _
0 _ _ 1 _ _
1 0 _
1 1 _

Backtracking

  • (^) We build a partial solution v = (a(1), a(2),..., a(k)), extend it and test

it.

  • (^) If the partial solution is still a candidate solution,

proceed.

else

delete a(k) and try another possible choice for a(k).

  • (^) If possible choices of a(k) are exhausted,

backtrack and try the next choice for a(k-1).

Backtracking: Iterative Version

Compute S(1) ,

k = 1 While (k > 0) do While S(k) != emptySet do (advance) a(k) = an element in S(k) S(k)= S(k)- a(k) if ( a(1),a(2),...,a(k)) is a solution, print it k = k + 1 Compute S(k) Compute , the candidate k-th element given v. k = k - 1 (backtrack)**

Backtracking and Recursion

  • (^) Backtracking is easily implemented with recursion because:
  • (^) The run-time stack takes care of keeping track of the choices that

got us to a given point.

  • (^) Upon failure we can get to the previous choice simply by returning a

failure code from the recursive call.

Improving Backtracking: Search Pruning

  • (^) Search pruning will help us to reduce the search space and hence

get a solution faster.

  • (^) The idea is to a void those paths that may not lead to a solutions as

early as possible by finding contradictions so that we can backtrack

immediately without the need to build a hopeless solution vector.

Eight Queen Problem

  • (^) The solution is a vector of length 8

(a(1), a(2), a(3), ...., a(8)).

a(i) corresponds to the column where we should place the i-th

queen.

  • (^) The solution is to build a partial solution element by element until it

is complete.

  • (^) We should backtrack in case we reach to a partial solution of length

k, that we couldn't expand any more.

Eight Queen Problem: Algorithm

putQueen(row)

for every position col on the same row

if position col is available

place the next queen in position col

if (row<8)

putQueen(row+1);

else success;

remove the queen from position col

Eight Queen Problem: Implementation

  • (^) We know that for queens:

each row will have exactly one queen

each column will have exactly one queen

each diagonal will have at most one queen

  • (^) This will help us to model the chessboard not as a 2-D array, but as

a set of rows, columns and diagonals.

  • (^) To simplify the presentation, we will study for smaller chessboard, 4

by 4

Implementing the Chessboard

  • (^) First: we need to define an array to store the location of so far

placed queens

PositionInRow 1 3 0 2

Implementing the Chessboard Cont’d

  • (^) We have 7 left diagonals, we want to keep track of available

diagonals after the so far allocated queens

T
T
F
T
F
T
T

Implementing the Chessboard Cont’d

  • (^) We have 7 left diagonals, we want to keep track of available

diagonals after the so far allocated queens

T
F
T
T
F
T
T