Chapter 5. Recursion. Data Structures and Algorithms in Java



Similar documents
12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Solving Problems Recursively

Introduction to Java

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

CS 2412 Data Structures. Chapter 2 Stacks and recursion

Stack Allocation. Run-Time Data Structures. Static Structures

Sample CSE8A midterm Multiple Choice (circle one)

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Moving from CS 61A Scheme to CS 61B Java

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Recursion and Recursive Backtracking

Explain the relationship between a class and an object. Which is general and which is specific?

Java Program Coding Standards Programming for Information Technology

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

J a v a Quiz (Unit 3, Test 0 Practice)

Example of a Java program

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Unit 6. Loop statements

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

What Is Recursion? Recursion. Binary search example postponed to end of lecture

C Compiler Targeting the Java Virtual Machine

Lecture 9. Semantic Analysis Scoping and Symbol Table

10CS35: Data Structures Using C

Basic Programming and PC Skills: Basic Programming and PC Skills:

csci 210: Data Structures Recursion

Iteration CHAPTER 6. Topic Summary

Module 2 Stacks and Queues: Abstract Data Types

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

Glossary of Object Oriented Terms

Basics of Java Programming Input and the Scanner class

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Semester Review. CSC 301, Fall 2015

Chapter 5 Functions. Introducing Functions

CS 111 Classes I 1. Software Organization View to this point:

Sources: On the Web: Slides will be available on:

Homework/Program #5 Solutions

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Pseudo code Tutorial and Exercises Teacher s Version

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Section IV.1: Recursive Algorithms and Recursion Trees

Data Structures. Algorithm Performance and Big O Analysis

Algorithm Design and Recursion

Programming Exercises

PES Institute of Technology-BSC QUESTION BANK

Two-Dimensional Arrays Summer 2010 Margaret Reid-Miller

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

50 Computer Science MI-SG-FLD050-02

CS170 Lab 11 Abstract Data Types & Objects

Common Data Structures

Habanero Extreme Scale Software Research Project

Chapter 5 Names, Bindings, Type Checking, and Scopes

Computing Concepts with Java Essentials

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Topic 11 Scanner object, conditional execution

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Building Java Programs

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Conditionals (with solutions)

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

An Incomplete C++ Primer. University of Wyoming MA 5310

St S a t ck a ck nd Qu Q eue 1

Introduction to Programming

Compiler Construction

Part I. Multiple Choice Questions (2 points each):

Illustration 1: Diagram of program function and data flow

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright /6/11 12:33 PM!

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Building Java Programs

4.2 Sorting and Searching

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Course Intro Instructor Intro Java Intro, Continued

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Programming and Software Development CTAG Alignments

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

CS 241 Data Organization Coding Standards

Building Java Programs

Introduction to Object-Oriented Programming

Lecture 11: Tail Recursion; Continuations

Stacks. Linear data structures

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Transcription:

Chapter 5 Recursion Data Structures and Algorithms in Java

Objectives Discuss the following topics: Recursive Definitions Method Calls and Recursion Implementation Anatomy of a Recursive Call Tail Recursion Nontail Recursion Indirect Recursion Data Structures and Algorithms in Java 2

Objectives (continued) Discuss the following topics: Nested Recursion Excessive Recursion Backtracking Case Study: A Recursive Descent Interpreter Data Structures and Algorithms in Java 3

Recursive Definitions Recursive definitions are programming concepts that define themselves A recursive definition consists of two parts: The anchor or ground case, the basic elements that are the building blocks of all other elements of the set Rules that allow for the construction of new objects out of basic elements or objects that have already been constructed Data Structures and Algorithms in Java 4

Recursive Definitions (continued) Recursive definitions serve two purposes: Generating new elements Testing whether an element belongs to a set Recursive definitions are frequently used to define functions and sequences of numbers Data Structures and Algorithms in Java 5

Recursive Functions The factorial function 1 if n = 0( anchor) n! = n.( n 1)! if n > 0( inductive step) Power of 2 function n 1 if n = 0 gn ( ) = 2 = 2. gn ( 1) ifn> 0 Data Structures and Algorithms in Java 6

Recursive Factorial Function int factorial (int num) { if (num == 0) return 1; else return num * factorial (num 1); } Data Structures and Algorithms in Java 7

Recursive Factorial Trace Data Structures and Algorithms in Java 8

Recursive Implementation: Largest Value in Array public static int largest(int list[], int lowerindex, int upperindex) { int max; if(lowerindex == upperindex) //the size of the sublist is 1 return list[lowerindex]; else { max = largest(list, lowerindex + 1, upperindex); if(list[lowerindex] >= max) return list[lowerindex]; else return max; } } Data Structures and Algorithms in Java 9

Execution of largest (list, 0, 3) Data Structures and Algorithms in Java 10

Recursive Fibonacci public static int rfibnum(int a, int b, int n) { if(n == 1) return a; else if(n == 2) return b; else return rfibnum(a, b, n - 1) + rfibnum(a, b, n - 2); } Data Structures and Algorithms in Java 11

Execution of rfibonacci(2,3,5) Data Structures and Algorithms in Java 12

Towers of Hanoi Problem with Three Disks Data Structures and Algorithms in Java 13

Towers of Hanoi: Three Disk Solution Data Structures and Algorithms in Java 14

Towers of Hanoi: Three Disk Solution Data Structures and Algorithms in Java 15

Towers of Hanoi: Recursive Algorithm public static void movedisks(int count, int needle1, int needle3, int needle2) { if(count > 0) { movedisks(count - 1, needle1, needle2, needle3); System.out.println("Move disk + count + from + needle1 + to + needle3 + ". ); movedisks(count - 1, needle2, needle3, needle1); } } Data Structures and Algorithms in Java 16

Method Calls and Recursion Implementation Activation records contain the following: Values for all parameters to the method, location of the first cell if an array is passed or a variable is passed by reference, and copies of all other data items Local (automatic) variables that can be stored elsewhere The return address to resume control by the caller, the address of the caller s instruction immediately following the call Data Structures and Algorithms in Java 17

Method Calls and Recursion Implementation (continued) A dynamic link, which is a pointer to the caller s activation record The returned value for a method not declared as void Data Structures and Algorithms in Java 18

Method Calls and Recursion Implementation (continued) Figure 5-1 Contents of the run-time stack when main() calls method f1(), f1() calls f2(), and f2() calls f3() Data Structures and Algorithms in Java 19

Power Function x n 1 if n = 0 = n 1 x. x if n> 0 A Java method for x n double power { double x, int n) { if (n = = 0) return 1.0; // else return x. power (x, n-1); Data Structures and Algorithms in Java 20

Trace of recursive calls Static public void main ( String args[ ]) { y = power ( 5.6, 2); } Call 1 power(5.6, 2) Call 2 power(5.6,1) Call 3 power(5.6,0) Call 3 1 Call 2 5.6 Call 1 31.36 Data Structures and Algorithms in Java 21

Anatomy of a Recursive Call Figure 5-2 Changes to the run-time stack during execution of power(5.6,2) Data Structures and Algorithms in Java 22

Tail Recursion Tail recursion is characterized by the use of only one recursive call at the very end of a method implementation void tail (int i) { if (i > 0) { System.out.print (i + ""); tail(i-1); } } Data Structures and Algorithms in Java 23

Tail Recursion Example of nontail recursion: void nontail (int i) { if (i > 0) { nontail(i-1); System.out.print (i + ""); nontail(i-1); } } Data Structures and Algorithms in Java 24

Nontail Recursion /*200 */ void reverse( ) { /*201 */ char ch = getchar( ) ; /*202 */ if (ch!= `\n` ) { /*203 */ reverse( ) ; /*204 */ System.out.print( ch ); } } Data Structures and Algorithms in Java 25

Nontail Recursion (continued) Figure 5-3 Changes on the run-time stack during the execution of reverse() Data Structures and Algorithms in Java 26

Nontail Recursion (continued) Figure 5-4 Examples of von Koch snowflakes 1. Divide an interval side into three even parts 2. Move one-third of side in the direction specified by angle Data Structures and Algorithms in Java 27

Nontail Recursion (continued) 3. Turn to the right 60 (i.e., turn 60 ) and go forward one-third of side 4. Turn to the left 120 and proceed forward one-third of side 5. Turn right 60 and again draw a line one-third of side long Data Structures and Algorithms in Java 28

Nontail Recursion (continued) Figure 5-5 The process of drawing four sides of one segment of the von Koch snowflake Data Structures and Algorithms in Java 29

Nontail Recursion (continued) drawfourlines (side, level) if (level = 0) draw a line; else drawfourlines(side/3, level-1); turn left 60 ; drawfourlines(side/3, level-1); turn right 120 ; drawfourlines(side/3, level-1); turn left 60 ; drawfourlines(side/3, level-1); Data Structures and Algorithms in Java 30

Nontail Recursion (continued) Figure 5-6 Recursive implementation of the von Koch snowflake Data Structures and Algorithms in Java 31

Nontail Recursion (continued) Figure 5-6 Recursive implementation of the von Koch snowflake (continued) Data Structures and Algorithms in Java 32

Nontail Recursion (continued) Figure 5-6 Recursive implementation of the von Koch snowflake (continued) Data Structures and Algorithms in Java 33

Nontail Recursion (continued) Figure 5-6 Recursive implementation of the von Koch snowflake (continued) Data Structures and Algorithms in Java 34

Nontail Recursion (continued) Figure 5-6 Recursive implementation of the von Koch snowflake (continued) Data Structures and Algorithms in Java 35

Indirect Recursion receive() decode() store() receive() decode() receive(buffer) while buffer is not filled up if information is still incoming get a character and store it in buffer; else exit(); decode(buffer); decode(buffer) decode information in buffer; store(buffer); store(buffer) transfer information from buffer to file; receive(buffer); Data Structures and Algorithms in Java 36

Indirect Recursion (continued) 2 x 3 tan x 3 sin( x) = sin. 3 2 x 1+ tan 3 sin( x) tan( x) = cos( x) x cos( x) = 1 sin 2 Data Structures and Algorithms in Java 37

Indirect Recursion (continued) Figure 5-7 A tree of recursive calls for sin (x) Data Structures and Algorithms in Java 38

Nested Recursion Ackermann Function m+ 1 if n= 0 Anm (, ) = An ( 1,1) ifn> 0, m= 0 An ( 1, Anm (, 1) otherwise A A m+ 3 (3, m) = 2 3.2 16. 2 (4, m) = 2 3 Even A 16 = = 2 65536 (4,1) 2 3 2 3 Data Structures and Algorithms in Java 39

Excessive Recursion Figure 5-8 The tree of calls for Fib(6) Data Structures and Algorithms in Java 40

Excessive Recursion (continued) Figure 5-9 Number of addition operations and number of recursive calls to calculate Fibonacci numbers Data Structures and Algorithms in Java 41

Excessive Recursion (continued) Figure 5-10 Comparison of iterative and recursive algorithms for calculating Fibonacci numbers Data Structures and Algorithms in Java 42

Backtracking Backtracking is a technique for returning to a given position (e.g., entry point) after trying other avenues that are unsuccessful in solving a particular problem Data Structures and Algorithms in Java 43

Backtracking (continued) Figure 5-11 The eight queens problem Data Structures and Algorithms in Java 44

Backtracking (continued) putqueen(row) for every position col on the same row if position col is available place the next queen in position col; if (row < 8) putqueen(row+1); else success; remove the queen from position col; Data Structures and Algorithms in Java 45

Backtracking (continued) Figure 5-12 A 4 x 4 chessboard Data Structures and Algorithms in Java 46

Backtracking (continued) Figure 5-13 Eight queens problem implementation Data Structures and Algorithms in Java 47

Backtracking (continued) Figure 5-13 Eight queens problem implementation (continued) Data Structures and Algorithms in Java 48

Backtracking (continued) Figure 5-13 Eight queens problem implementation (continued) Data Structures and Algorithms in Java 49

Backtracking (continued) Figure 5-14 Steps leading to the first successful configuration of four queens as found by the method putqueen() Data Structures and Algorithms in Java 50

Backtracking (continued) Figure 5-15 Changes in the four arrays used by method putqueen() Data Structures and Algorithms in Java 51

Backtracking (continued) Figure 5-16 Changes on the run-time stack for the first successful completion of putqueen() Data Structures and Algorithms in Java 52

Backtracking (continued) Figure 5-17 Changes to the chessboard leading to the first successful configuration Data Structures and Algorithms in Java 53

Backtracking (continued) Figure 5-18 Trace of calls to putqueen() to place four queens (continued) Data Structures and Algorithms in Java 54

Case Study: A Recursive Descent Interpreter The process of translating one executable statement at a time and immediately executing it is called interpretation Translating the entire program first and then executing it is called compilation Data Structures and Algorithms in Java 55

Case Study: A Recursive Descent Interpreter (continued) Figure 5-19 Diagrams of methods used by the recursive descent interpreter Data Structures and Algorithms in Java 56

Case Study: A Recursive Descent Interpreter (continued) Figure 5-19 Diagrams of methods used by the recursive descent interpreter (continued) Data Structures and Algorithms in Java 57

Case Study: A Recursive Descent Interpreter (continued) term() f1 = factor(); while current token is either / or * f2 = factor(); f1 = f1 * f2 or f1 / f2; return f1; factor() process all +s and s preceding a factor; if current token is an identifier return value assigned to the identifier; else if current token is a number return the number; else if current token is ( e = expression(); if current token is ) return e; Data Structures and Algorithms in Java 58

Case Study: A Recursive Descent Interpreter (continued) Figure 5-20 Implementation of a simple language interpreter Data Structures and Algorithms in Java 59

Case Study: A Recursive Descent Interpreter (continued) Figure 5-20 Implementation of a simple language interpreter (continued) Data Structures and Algorithms in Java 60

Case Study: A Recursive Descent Interpreter (continued) Figure 5-20 Implementation of a simple language interpreter (continued) Data Structures and Algorithms in Java 61

Case Study: A Recursive Descent Interpreter (continued) Figure 5-20 Implementation of a simple language interpreter (continued) Data Structures and Algorithms in Java 62

Case Study: A Recursive Descent Interpreter (continued) Figure 5-20 Implementation of a simple language interpreter (continued) Data Structures and Algorithms in Java 63

Case Study: A Recursive Descent Interpreter (continued) Figure 5-20 Implementation of a simple language interpreter (continued) Data Structures and Algorithms in Java 64

Case Study: A Recursive Descent Interpreter (continued) Figure 5-20 Implementation of a simple language interpreter (continued) Data Structures and Algorithms in Java 65

Case Study: A Recursive Descent Interpreter (continued) Figure 5-20 Implementation of a simple language interpreter (continued) Data Structures and Algorithms in Java 66

Case Study: A Recursive Descent Interpreter (continued) Figure 5-20 Implementation of a simple language interpreter (continued) Data Structures and Algorithms in Java 67

Case Study: A Recursive Descent Interpreter (continued) Figure 5-20 Implementation of a simple language interpreter (continued) Data Structures and Algorithms in Java 68

Case Study: A Recursive Descent Interpreter (continued) Figure 5-20 Implementation of a simple language interpreter (continued) Data Structures and Algorithms in Java 69

Summary Recursive definitions are programming concepts that define themselves Recursive definitions serve two purposes: Generating new elements Testing whether an element belongs to a set Recursive definitions are frequently used to define functions and sequences of numbers Data Structures and Algorithms in Java 70

Summary (continued) Tail recursion is characterized by the use of only one recursive call at the very end of a method implementation. Backtracking is a technique for returning to a given position (e.g., entry point) after trying other avenues that are unsuccessful in solving a particular problem. The process of translating one executable statement at a time and immediately executing it is called interpretation. Data Structures and Algorithms in Java 71