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

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

View Full Document Right Arrow Icon
CMPSCI 187: Programming With Data Structures Lecture 23: Examples of Recursion 4 November 2011 Guest Lecturer: Bobby Simidchieva
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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?
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

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

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

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