{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

22-Recursive-Backtracking

22-Recursive-Backtracking - CS106X Winter 2008 H a 22...

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

View Full Document Right Arrow Icon
CS106X H a n 22 Winter 2008 February 1, 2008 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. You’ll want to read Chapter 6 of the reader very carefully. We’ll complement the reader with a good number of additional examples not in the reader. 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. Morphing Write a function Morph which recursively attempts to morph the start word into the destination word by a series of transformations. A transformation is changing one letter from the start word into the letter at the corresponding position in the destination word . Each word formed along the way must be a valid word (as reported by the dictionary from the Boggle assignment that went out on Wednesday). Assume the lexicon has been opened and is ready for use. Remember the member function to check if a word is in the lexicon is: bool Lexicon::containsWord(string word); act end act ant and end read boot read bead beat boat boot The two parameters to Morph are the start word and the target word. Your function should return true if and only if a morph could be found (i.e. you do not have to keep track of the steps, just report whether you found a morph or not).
Image of page 1

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

View Full Document Right Arrow Icon
2 bool Morph(string start, string dest, Lexicon& lex) { if (start == dest) return true; if (!lex.containsWord(start)) return false; for (int i = 0; i < start.length(); i++) { if (dest[i] != start[i]) { char saved = start[i]; start[i] = dest[i]; if (Morph(lex, start, dest)) return true; start[i] = saved; } } return false; } Playing Dominoes The game of dominoes is played with rectangular pieces composed of two connected squares, each of which is marked with a certain number of dots.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern