16-Recursive-Backtracking

16-Recursive-Backtra - CS106X Handout 16 Autumn 2010 October 6 th 2010 Recursive Backtracking Recursive Backtracking So far all of the recursive

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS106X Handout 16 Autumn 2010 October 6 th , 2010 Recursive Backtracking Recursive Backtracking So far, all of the recursive algorithms we have seen have shared one very important property: each time a problem was recursively decomposed into a simpler instance of the same problem, only one such decomposition was possible; consequently, the algorithm was guaranteed to produce a solution to the original problem. Today we will begin to examine problems with several possible decompositions from one instance of the problem to another. That is, each time we make a recursive call, we will have to make a choice as to which decomposition to use. If we choose the wrong one, we will eventually run into a dead end and find ourselves in a state from which we are unable to solve the problem immediately and unable to decompose the problem any further; when this happens, we will have to backtrack to a "choice point" and try another alternative. If we ever solve the problem, great: we’re done. Otherwise, we need to keep exploring all possible paths by making choices and, when they prove to have been wrong, backtracking to the most recent choice point. What’s really interesting about backtracking is that we only back up in the recursion as far as we need to go to reach a previously unexplored choice point. Eventually, more and more of these choice points will have been explored, and we will backtrack further and further. If we happen to backtrack to our initial position and find ourselves with no more choices from that initial position, the particular problem at hand is unsolvable. Continue reading Chapters 5, 6, and 7. We’ll spend this week and some of next week going over many of the textbook’s examples, plus a good number of others. 2 Shrinking A Word [courtesy of Julie Zelenski] Consider the simply stated question: Is it possible to take an English word and remove all of its letters in some order such that every string along the way is also an English word? Sometimes it’s possible. For example, we can shrink the word "amart" down to the empty string while obeying the restriction that all of the intervening strings are also legitimate words. Check this out: "smart" "mart" "art" "at" "a" "" We elect to first remove the s , then the m , then the r , then the t , and finally the a . Note that every single string in the above diagram is an English word: That means that, for the purposes of the this problem, it’s possible to shrink the word " smart " down to the empty string. Not surprisingly, there are some perfectly good English words that can’t be shrunk at all: "zephyr" , "lymph" , "rope" , "father" . A reasonable question to ask at this point: Can we programmatically figure out which words can be shrunk and which ones can’t be?...
View Full Document

This note was uploaded on 01/13/2011 for the course CS 106X taught by Professor Cain,g during the Fall '08 term at Stanford.

Page1 / 13

16-Recursive-Backtra - CS106X Handout 16 Autumn 2010 October 6 th 2010 Recursive Backtracking Recursive Backtracking So far all of the recursive

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online