{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture20-july29

lecture20-july29 - Lecture 18 Announcements Assignment 3 4...

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

1 Lecture 18 Announcements Assignment 3 – 4 owe me team member reviews Assignment 4 – Teams working OK? – Q&A on the assignment Read Ch. 8 (trees) Topics of the day – Recursion (more) – backtracking (Ch. 7.6) – Tree intro. ??? – Team meetings Recursion Again Characteristics of a recursive solution – There must be at least 1 case (base) that for a small value of n, can be directly solved – A problem of a given size (say n) can be split into 1 or more smaller versions of the same problem To design a recursive solution we must: – Recognize the base case(s) and provide solutions – Devise a strategy to split the problem into smaller versions of itself. Each recursive case must make progress towards a base case. – Combine the solutions to the smaller problems in such a way that each larger problem is solved correctly. Recursive Solution Pattern IF the problem can be directly solved for the current value of n – Solve it ELSE – Recursively apply the algorithm to 1 or more problems with smaller values of n – Combine the solution to the smaller problems to get the solution to the larger problem Backtracking Backtracking is an approach to implementing systematic trial and error in a search for a solution A classic example is finding a path through a maze If you are attempting to walk through a maze, you will probably walk down a path as far as you can go Eventually, you will reach your destination or you won’t be able to go any farther If you can’t go any farther, you will need to retrace your steps Backtracking is a systematic approach to trying alternative paths and eliminating them if they don’t work Never try the exact same path more than once, and you will eventually find a solution path if one exists Problems that are solved by backtracking can be described as a set of choices made by some method Recursion allows us to implement backtracking in a relatively straightforward manner Each activation record on the stack is used to remember the choice that was made at that particular decision point Backtracking Start Success! Success! Failure Problem space consists of states (nodes) and actions (paths that lead to new states). When in a node you can only see paths to connected nodes If a node only leads to failure, go back to its "parent" node. Try other alternatives. If these all lead to failure then more backtracking may be necessary. Recursive Backtracking General Pseudo code If at a solution, report success for every possible choice from current state / node 1. Make that choice and take one step along path 2. Use recursion to solve the problem for the new node / state 3. If the recursive call succeeds, report the success to the next higher level 4. Back out of the current choice to restore the state at the beginning of the loop.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}