# 23 - CMPSCI 187 Programming With Data Structures Lecture 23...

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

CMPSCI 187: Programming With Data Structures Lecture 23: Examples of Recursion 4 November 2011 Guest Lecturer: Bobby Simidchieva

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

View Full Document
Examples of Recursion • Recursively Searching a Maze • Depth-First Search Without the seen Field • The Towers of Hanoi Problem • Exponentially-Growing Running Times • Analysis of Recursive Algorithms • Examples of Time Complexity with Recursion
Recursively Searching a Maze • In Section 7.3. L&C return to their maze search example from Chapter 4, from which we adapted Project #2. This time they write a recursive solution, which has somewhat simpler code and does not use an explicit Stack object. The increased clarity of reasoning is exempliFed by the fact that this new version is actually correct. • In our setting, the basic idea is to search a cell for a path to the destination by searching each of its unseen neighbors for a path to the destination. After all, the path from the source can exist only if a path from one of the neighbors exists. • There is clearly a base case, since if we are asked for a path from the destination to itself we can just return it. But why can we be sure that this recursive algorithm will ever reach the base case?

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

View Full Document
Depth-First Search With the Method Stack • Let’s trace the action of the recursive code below. If the source has any open unseen neighbors, we recurse on the Frst one, then on that one’s Frst open unseen neighbor, and so on. We are exactly mimicking Project #2’s search. • We only Fnish processing a cell when we have Fnished with all its open
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 11/22/2011 for the course COMPSCI 187 taught by Professor Barrington during the Fall '11 term at UMass (Amherst).

### Page1 / 9

23 - CMPSCI 187 Programming With Data Structures Lecture 23...

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

View Full Document
Ask a homework question - tutors are online