This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Algorithms Lecture 3: Backtracking [ Fa’10 ] ’Tis a lesson you should heed, Try, try again; If at first you don’t succeed, Try, try again; Then your courage should appear, For, if you will persevere, You will conquer, never fear; Try, try again. — Thomas H. Palmer, The Teacher’s Manual: Being an Exposition of an Efficient and Economical System of Education Suited to the Wants of a Free People (1840) When you come to a fork in the road, take it. — Yogi Berra 3 Backtracking In this lecture, I want to describe another recursive algorithm strategy called backtracking . A back tracking algorithm tries to build a solution to a computational problem incrementally. Whenever the algorithm needs to decide between multiple alternatives to the next component of the solution, it simply tries all possible options recursively. 3.1 n Queens The prototypical backtracking problem is the classical n Queens Problem , first proposed by German chess enthusiast Max Bezzel in 1848 for the standard 8 × 8 board, and both solved and generalized to larger boards by Franz Nauck in 1850. The problem is to place n queens on an n × n chessboard, so that no two queens can attack each other. For readers not familiar with the rules of chess, this means that no two queens are in the same row, column, or diagonal. Obviously, in any solution to the nQueens problem, there is exactly one queen in each row. So we will represent our possible solutions using an array Q [ 1.. n ] , where Q [ i ] indicates which square in row i contains a queen, or 0 if no queen has yet been placed in row i . To find a solution, we put queens on the board row by row, starting at the top. A partial solution is an array Q [ 1.. n ] whose first r 1 entries are positive and whose last n r + 1 entries are all zeros, for some integer r . The following recursive algorithm recursively enumerates all complete nqueens solutions that are consistent with a given partial solution. The input parameter r is the first empty row. Thus, to compute all nqueens solutions with no restrictions, we would call R ECURSIVENQUEENS ( Q [ 1 .. n ] ,1 ) . R ECURSIVE NQ UEENS ( Q [ 1.. n ] , r ) : if r = n + 1 print Q else for j ← 1 to n legal ← T RUE for i ← 1 to r 1 if ( Q [ i ] = j ) or ( Q [ i ] = j + r i ) or ( Q [ i ] = j r + i ) legal ← F ALSE if legal Q [ r ] ← j R ECURSIVE NQ UEENS ( Q [ 1.. n ] , r + 1 ) c Copyright 2011 Jeff Erickson. Released under a Creative Commons AttributionNonCommercialShareAlike 3.0 License ( http://creativecommons.org/licenses/byncsa/3.0/ ). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision....
View
Full
Document
This note was uploaded on 10/14/2011 for the course ECON 101 taught by Professor Smith during the Spring '11 term at West Virginia University Institute of Technology.
 Spring '11
 Smith

Click to edit the document details