02-backtracking

# 02-backtracking - Algorithms Tis a lesson you should heed...

This preview shows pages 1–3. Sign up to view the full content.

Algorithms Lecture 2: Backtracking ’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 2 Backtracking In this lecture, I want to describe another recursive algorithm strategy called backtracking . A backtracking algorithm tries to build a solution to a computational problem incrementally. Whenever the algorithm needs to decide between two alternatives to the next component of the solution, it simply tries both options recursively. 2.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. One solution to the 8 queens problem, represented by the array [4,7,3,8,2,5,1,6] Obviously, in any solution to the n -Queens problem, there is exactly one queen in each column. So we will represent our possible solutions using an array Q [ 1 .. n ] , where Q [ i ] indicates the square in column i that contains a queen, or 0 if no queen has yet been placed in column i . To find a solution, we will 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 n -queens solutions that are consistent with a given partial solution. The input parameter r is the first empty row. Thus, to compute all n -queens solutions with no restrictions, we would call R ECURSIVE NQ UEENS ( Q [ 1.. n ] ,1 ) . 1

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

View Full Document
Algorithms Lecture 2: Backtracking 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 ] = r - j + i ) or ( Q [ i ] = r + i - j ) legal F ALSE if legal Q [ r ] j R ECURSIVE NQ UEENS ( Q [ 1.. n ] , r + 1 ) Like most recursive algorithms, the execution of a backtracking algorithm can be illustrated using a recursion tree . The root of the recursion tree corresponds to the original invocation of the algorithm; edges in the tree correspond to recursive calls. A path from the root down to any node shows the history of a partial solution to the n -Queens problem, as queens are added to successive rows. The leaves correspond to partial solutions that cannot be extended, either because there is already a queen on every row, or because every position in the next empty row is in the same row, column, or diagonal as an existing queen.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern