CmpSci 187: Programming with Data Structures Spring 2015

Similar documents
CMPSCI 187: Programming With Data Structures. Lecture #15: Thinking Recursively David Mix Barrington 10 October 2012

CMPSCI 187: Programming With Data Structures. Lecture #16: Thinking About Recursion David Mix Barrington 12 October 2012

To illustrate what is intended the following are three write ups by students. Diagonalization

CmpSci 187: Programming with Data Structures Spring 2015

We cover recursion in 150. Why do it again in 151?

CMPSCI 250: Introduction to Computation. Lecture #24: General Search, DFS, and BFS David Mix Barrington 24 March 2014

Recursive Definitions

CMPSCI 187 / Spring 2015 Hanoi

CS200: Recursion and induction (recap from cs161)

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CMPSCI 250: Introduction to Computation. Lecture #1: Things, Sets and Strings David Mix Barrington 22 January 2014

Clicker Question What will this print? def blah(x): return x+1 def main(): z = blah(3) print(z) main() A) 3 B) 4 C) 5 D) It causes an error

Chapter 15: Recursion

APCS-AB: Java. Recursion in Java December 12, week14 1

CSC 273 Data Structures

Recursive Thinking. Chapter 8: Recursion. Recursive Definitions. Recursion. Java Software Solutions for AP* Computer Science A 2nd Edition

CmpSci 187: Programming with Data Structures Spring 2015

Solutions to Problem 1 of Homework 3 (10 (+6) Points)

Summary. csci 210: Data Structures Recursion. Recursive algorithms. Recursion. Topics READING:

csci 210: Data Structures Recursion

Recursion. Thinking Recursively. Tracing the Recursive Definition of List. CMPT 126: Lecture 10. Recursion

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Recursion. Tracing Method Calls via a Stack. Beyond this lecture...

CSCI 136 Data Structures & Advanced Programming. Lecture 14 Fall 2018 Instructor: Bills

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

CS 3410 Ch 7 Recursion

Reduction & Recursion Overview

CmpSci 187: Programming with Data Structures Spring 2015

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Lecture 24 Tao Wang 1

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

recursive algorithms 1

CompSci 105 S2 C - ASSIGNMENT TWO -

CMSC 150 LECTURE 7 RECURSION

COT 3100 Spring 2010 Midterm 2

You must bring your ID to the exam.

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

LECTURE 17 GRAPH TRAVERSALS

Backtracking. The Eight Queens Problem. The Eight Queens Problem. The Eight Queens Problem. The Eight Queens Problem. Chapter 5

Recursion Solution. Counting Things. Searching an Array. Organizing Data. Backtracking. Defining Languages

CMPSCI 187: Programming With Data Structures. Lecture #31: Graphs David Mix Barrington 26 November 2012

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

Backtracking. See Section 7.7 p of Weiss

CS 61B, Spring 1999 MT3 Professor M. Clancy

Recursion Chapter 4 Self-Reference. Recursive Definitions Inductive Proofs Implementing Recursion

CMPSCI 250: Introduction to Computation. Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013

1.7 Recursion. Department of CSE

Exercise 1: Tail Sum of Squares (2 points) Homework 3 READ THIS BEFORE YOU BEGIN:

CMPSCI 250: Introduction to Computation. Lecture #28: Regular Expressions and Languages David Mix Barrington 2 April 2014

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

2.3 Recursion 7/23/2015 3:06:35 PM

Pace University. Fundamental Concepts of CS121 1

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

CSE 20 DISCRETE MATH WINTER

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

CMPSCI 187: Programming With Data Structures. Lecture 6: The StringLog ADT David Mix Barrington 17 September 2012

CS61B Spring 2016 Guerrilla Section 6 Worksheet

CSC 222: Computer Programming II. Spring 2004

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

Counting Problems; and Recursion! CSCI 2824, Fall 2012!

A Look Back at Arithmetic Operators: the Increment and Decrement

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Recursion. Let s start by looking at some problems that are nicely solved using recursion. First, let s look at generating The Fibonacci series.

UNIT 5A Recursion: Basics. Recursion

Recursion (Part 2) 1

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

Backtracking. Module 6. The Eight Queens Problem. CS 147 Sam Houston State University Dr. McGuire. Backtracking. Recursion Revisited

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

EE 368. Weeks 4 (Notes)

CSC 222: Computer Programming II. Spring 2005

Recursion. Jordi Cortadella Department of Computer Science

Recursion. So, just as you are allowed to call function B from within function A, you are ALSO allowed to call function A from within function A!

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 5. The Towers of Hanoi. Divide and Conquer

Data Structure (CS301)

Massachusetts Institute of Technology Department of Mechanical Engineering

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

Recursion. notes Chapter 8

CSC 1052 Algorithms & Data Structures II: Recursion

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

2.3 Recursion 7/21/2014 5:12:34 PM

APCS Semester #1 Final Exam Practice Problems

Getting to know Greenfoot

Induction and Recursion. CMPS/MATH 2170: Discrete Mathematics

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

CMPSCI 187 / Spring 2015 Implementing Sets Using Linked Lists

Lab 7 1 Due Thu., 6 Apr. 2017

Total Score /15 /20 /30 /10 /5 /20 Grader

EK131 E5 Introduction to Engineering

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

CSCE 110 Notes on Recursive Algorithms (Part 12) Prof. Amr Goneid

Transcription:

CmpSci 187: Programming with Data Structures Spring 2015 Lecture #9 John Ridgway February 26, 2015 1 Recursive Definitions, Algorithms, and Programs Recursion in General In mathematics and computer science we often have definitions of concepts that refer to themselves. We call them recursive. For example: A directory in most operating systems, for example, is something that contains things that are either files or directories. A statement in Java might be an if statement, and an if statement is made up of the words if and else, a boolean expression in parentheses, and one or two statements. A postfix expression is either an operand or two postfix expressions followed by an operator. Recursive Algorithms Recursive algorithms are algorithms that call themselves, but only on a smaller version of the same problem. They are most useful when applied to recursively defined concepts. Much of CMPSCI 250 is devoted to the relationship of recursive definitions and algorithms to inductive proofs here we will look at the definitions and algorithms. Computing Factorials The factorial of a non-negative integer n, written n!, is the product of all the numbers from 1 through n, inclusive, or 1 2... n. 1

(The factorial of 0 is 1, because multiplying together an empty set of numbers is like not multiplying at all, or multiplying by 1.) For example, 5! = 1 2 3 4 5 = 120. Here is a recursive definition of factorial : 0! = 1, and if n > 0, n! = n (n 1)! Computing Factorials (continued) If we use this definition to try to calculate 5!, we get that 5! = 5 4!. If we apply the definition again to 4!, we get that 5! = 5 4 3!. Continuing in the same way, we get 5! = 5 4 3 2! = 5 4 3 2 1! = 5 4 3 2 1 0! = 5 4 3 2 1 1 just as in the non-recursive definition. Recursive Code for Factorials The recursive definition is easy to turn into code, as is the original iterative definition: public static int factorial ( int n) { if (n == 0) { return 1; else { return n* factorial (n -1); public static int iterfact ( int n) { int x = 1; for ( int i =1; i <=n; i ++) {x *= i; return x; Clicker Question #1 public int foo ( int n) { if (n <= 1) { return 0; return 1 + foo (n -2); What is returned by the above recursive method if called with a positive integer n as parameter? A. n B. 0 C. n/2 D. the method never returns a value 2

Recursive Factorial Example What happens when we run the recursive code with a parameter of 5? That call makes a call on factorial(4), which calls factorial(3), which calls factorial(2), which calls factorial(1), which calls factorial(0). This returns 1 to the factorial(1), which returns 1 to factorial(2), which returns 2 to factorial(3), which returns 6 to factorial(4), which returns 24 to factorial(5), which returns 120, the right answer. 2 The Three Questions Three Fundamental Questions For a recursive algorithm to work correctly on a particular input, we need positive answers to the three questions that DJW pose in Section 4.2: Does the algorithm have a base case? Does every recursive call make progress toward the base case? Can we show that the general call to the algorithm gets the right answer if we assume that all the recursive calls get the right answer? Three Fundamental Questions (continued) A base case is where there is no further recursion. Our factorial algorithm has a base case of n = 0. We can often guarantee progress because some parameter gets smaller with every call. Our factorial algorithm always calls factorial(n-1), so we get smaller if we are positive. We can justify correctness, assuming correctness of the calls, by a recursive definition. This is clear for factorials. Clicker Question #2 public void clear ( Stack < Integer > s) { if (!s. empty ()) { s. pop (); clear (s); What is the base case of this recursive method? A. a StackUnderflowException is thrown B. s has a finite number of elements C. calling clear makes the stack smaller D. s is empty 3

3 The Towers of Hanoi Towers of Hanoi In the Towers of Hanoi game, there are three pegs and n rings of different sizes. Originally all n rings are on the first peg, with the largest at the bottom and the others in order of size above it. Towers of Hanoi We want to move all the rings to another peg, following the rules: we move one ring at a time; and no larger peg may ever be put atop a smaller peg. A Recursive Solution We can recursively solve the puzzle by defining the following procedure to move k rings from peg A to peg B. This turns out to take 2 k 1 moves. public void move ( peg A, peg B, int k) { if (k == 1) { else { move (A, C, k -1); move (C, B, k -1); A Recursive Solution We can recursively solve the puzzle by defining the following procedure to move k rings from peg A to peg B. This turns out to take 2 k 1 moves. 4

public void move ( peg A, peg B, int k) { if (k == 0) { // nothing to do else { move (A, C, k -1); move (C, B, k -1); Clicker Question #3 public void move ( peg A, peg B, int k) { if (k!= 0) { move (A, C, k -1); move (C, B, k -1); How do we know that this method will make progress toward its base case? A. we move at least one ring with each call B. the parameter k decreases with each call C. the base case has k equal to 1 D. each call has only two recursive calls 4 Counting Blobs Grids and Continents (Blobs) In games like CivilizationTM, the computer creates a map to play on, with land areas, water areas, different terrain for each area, and so forth. We re now going to look at a simple version of this where the world is a rectangular grid of squares, each one land or water. Squares are considered adjacent if they share a horizontal or vertical edge, not if they just meet at a corner. Grids and Continents (continued) A continent (called a blob in DJW) is a set of land squares such that you may travel by land from any square on the continent to any other square on the continent, but not to any other square. Again, you cannot jump diagonally across a corner in this process. We re going to look at the computational problem of counting the continents in a particular rectangular map. We ll be greatly helped by the use of recursion. 5

Continent Example On the left, land squares have an X and water squares have a -. On the right, the five continents have been identified and each land square labeled with the name of its continent. X-X--X--X-- A-A--B--C-- XXX--X----- AAA--B----- --XX-X----- --AA-B----- -----XX---- -----BB---- ----X---XXX ----D---EEE Clicker Question #4 How many continents does this grid have? Continent Example X-X--XXX---X-XXXX--X-- XXX--X--XXXXXX--X----- --XXXX-------XX-X----- -----XX--------XXXXX-- ----XX-XXXX----X---XXX A. 3 B. 4 C. 5 D. 6 DJW s code only counts the continents without naming or marking them. We could traverse the grid, counting the squares. But this over-counts each continent, counting one for each square in it. The right idea is to traverse the grid; the first time you find one of the squares of a continent increment your counter, then mark all of the squares of the continent. Recursive Marking Use a boolean array visited that will hold true for every land square determined to be part of a continent. Set this visited value true for every square reachable from the target square by marking the target square then recursively marking any of its four neighbors that are in the grid and are unseen land squares. 6

Code to Mark Continents private void markblob ( int row, int col ) { visited [ row ][ col ] = true ; if (( row - 1) >= 0 && grid [ row - 1][ col ] &&! visited [ row - 1][ col ]) { markblob ( row - 1, col ); // same for ( row + 1, col ) // same for ( row, col - 1) // same for ( row, col + 1) Answering the Three Questions for markblob Base case: when we call markblob on a square that has no unseen land squares as neighbors, it just marks its target square and returns. Progress toward base case: every time we mark a square, we reduce the total number of unseen land squares in the grid, which started out as a finite number. And every call to markblob marks at least one such square, because we only ever call it on an unseen land square. The Third Question: Correctness General correctness: We want to be sure that when markblob(i,j) is called, it marks (i, j) and every unseen land square that can be reached from (i, j) by using unseen land squares. Suppose we are sure that this property is true for all the calls to markblob on neighbors of (i, j). Then if a square is reachable from (i, j), it must be either be (i, j) itself or be reachable from one of its neighbors. So if the neighbor calls do their job, so does the first call. 7