The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998

18.04.2013 Views

Lecture 11 - backtracking Backtracking Backtracking is a systematic way to go through all the possible configurations of a search space. In the general case, we assume our solution is a vector where each element is selected from a finite ordered set , We build from a partial solution of length k and try to extend it by adding another element. After extending it, we will test whether what we have so far is still possible as a partial solution. If it is still a candidate solution, great. If not, we delete and try the next element from : Compute , the set of candidate first elements of v. k = 1 While k > 0 do Listen To Part 11-7 While do (*advance*) = an element in if ( ) is solution, print! k = k + 1 k = k - 1 (*backtrack*) file:///E|/LEC/LECTUR16/NODE11.HTM (4 of 8) [19/1/2003 1:34:54] compute , the candidate kth elements given v. Recursive Backtracking

Lecture 11 - backtracking Recursion can be used for elegant and easy implementation of backtracking. Backtrack(a, k) if a is a solution, print(a) else { } k = k +1 compute while do = an element in = Backtrack(a, k) Backtracking can easily be used to iterate through all subsets or permutations of a set. Backtracking ensures correctness by enumerating all possibilities. For backtracking to be efficient, we must prune the search space. Listen To Part 11-8 How many subsets are there of an n-element set? Constructing all Subsets To construct all subsets, set up an array/vector of n cells, where the value of is either true or false, signifying file:///E|/LEC/LECTUR16/NODE11.HTM (5 of 8) [19/1/2003 1:34:54]

Lecture 11 - backtracking<br />

Backtracking<br />

Backtracking is a systematic way to go through all the possible configurations of a search space.<br />

In the general case, we assume our solution is a vector where each element is selected from a finite<br />

ordered set ,<br />

We build from a partial solution of length k and try to extend it by adding another element. After<br />

extending it, we will test whether what we have so far is still possible as a partial solution.<br />

If it is still a candidate solution, great. If not, we delete and try the next element from :<br />

Compute , the set of candidate first elements of v.<br />

k = 1<br />

While k > 0 do<br />

Listen To Part 11-7<br />

While do (*advance*)<br />

= an element in<br />

if ( ) is solution, print!<br />

k = k + 1<br />

k = k - 1 (*backtrack*)<br />

file:///E|/LEC/LECTUR16/NODE11.HTM (4 of 8) [19/1/2003 1:34:54]<br />

compute , the candidate kth elements given v.<br />

Recursive Backtracking

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!