Recursive Methods and Problem Solving. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

Similar documents
Chapter 13 Recursion. Chapter Objectives

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

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

Data Structures And Algorithms

Complexity of Algorithms

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

Recursive Definitions

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

Recursion (Rosen, 6 th edition, Section 4.3, 4.4)

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

Search,Sort,Recursion

Introduction to Computers & Programming

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

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

Recursion. Fundamentals of Computer Science

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

CSC 1052 Algorithms & Data Structures II: Recursion

Recursion. Overview. Mathematical induction. Hello recursion. Recursion. Example applications. Goal: Compute factorial N! = 1 * 2 * 3...

recursive algorithms 1

Programming with Recursion. What Is Recursion?

Recursion Chapter 3.5

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

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

Recursion. Jordi Cortadella Department of Computer Science

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

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

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

Lecture 10: Recursion vs Iteration

Chapter 15: Recursion

2.3 Recursion. Overview. Mathematical Induction. What is recursion? When one function calls itself directly or indirectly.

Recursion. Example R1

1.7 Recursion. Department of CSE

Recursion. Chapter 7

UNIT 5A Recursion: Basics. Recursion

CSC 273 Data Structures

Solving problems by recursion

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

Recursion. Chapter 5

Recursion. Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

Recursion. Dr. Jürgen Eckerle FS Recursive Functions

Programming II (CS300)

CSC-140 Assignment 4

12/30/2013 S. NALINI,AP/CSE

Data Abstraction & Problem Solving with C++: Walls and Mirrors 6th Edition Carrano, Henry Test Bank

CSE 214 Computer Science II Recursion

Recursion vs Induction

C22a: Problem Solving using Recursion

Overview. What is recursion? When one function calls itself directly or indirectly.

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself?

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself? contains a reference to itself.

Programming II (CS300)

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

Why use recursion? Some problems are more easily expressed in a recursive definition

CS103L SPRING 2017 UNIT 8: RECURSION

What is Recursion? ! Each problem is a smaller instance of itself. ! Implemented via functions. ! Very powerful solving technique.

CS 310 Advanced Data Structures and Algorithms

Programming II (CS300)

CS159. Nathan Sprague. November 9, 2015

Recursion. Chapter 11. Chapter 11 1

! New mode of thinking. ! Powerful programming paradigm. ! Mergesort, FFT, gcd. ! Linked data structures.

Fun facts about recursion

Recursion. CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Recursion. General Algorithm for Recursion. When to use and not use Recursion. Recursion Removal. Examples

Announcement. Submit assignment 3 on CourSys Do not hand in hard copy Due Friday, 15:20:00. Caution: Assignment 4 will be due next Wednesday

26 How Many Times Must a White Dove Sail...

Outline. Simple Recursive Examples Analyzing Recursion Sorting The Tower of Hanoi Divide-and-conquer Approach In-Class Work. 1 Chapter 6: Recursion

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

2.3 Recursion. Overview. Greatest Common Divisor. Greatest Common Divisor. What is recursion? When one function calls itself directly or indirectly.

2.3 Recursion. Overview. Greatest Common Divisor. Greatest Common Divisor. What is recursion? When one function calls itself directly or indirectly.

8.1. Chapter 8: Introduction to Search Algorithms. Linear Search. Linear Search. Linear Search - Example 8/23/2014. Introduction to Search Algorithms

Recursion vs Induction. CS3330: Algorithms The University of Iowa

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

Module 10. Recursion. Adapted from Absolute Java, Rose Williams, Binghamton University

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

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

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

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

csci 210: Data Structures Recursion

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Chapter 6 Recursion. The Concept of Recursion

Unit #2: Recursion, Induction, and Loop Invariants

Algorithm Design and Recursion. Search and Sort Algorithms

CSE 2123 Recursion. Jeremy Morris

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

15. Recursion and Search Algorithms

Unit #3: Recursion, Induction, and Loop Invariants

Objectives. Recursion. One Possible Way. How do you look up a name in the phone book? Recursive Methods Must Eventually Terminate.

Chapter 19 Recursion and Method Redefinition

Reading 8 : Recursion

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Biostatistics 615/815 - Lecture 2 Introduction to C++ Programming

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

Lecture 8 Recursion. The Mirrors

CS 112 Introduction to Programming

UNIT 5A Recursion: Basics. Recursion

SOFTWARE DEVELOPMENT 1. Recursion 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

q To develop recursive methods for recursive mathematical functions ( ).

Decision-Making and Repetition

Transcription:

Recursive Methods and Problem Solving Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

Review: Calling Methods int x(int n) { int m = 0; n = n + m + 1; return n; int y(int n) { int m = 1; n = x(n); return m + n; What does y(3) return? Java Programming: Program Design Including Data Structures 2

Calling Methods Methods can call other methods Can a method call itself? Yes! This is called a recursive method (function) A method within a method Java Programming: Program Design Including Data Structures 3

Java Programming: Program Design Including Data Structures 4

Example void test(int n) { if (n > 0) { System.out.println(n); test(n-1); System.out.println(n); Trace the execution of test(4) Java Programming: Program Design Including Data Structures 5

Example (pt 2) void test(int n) { if (n > 0) { System.out.println(n); test(n-1); System.out.println(n); Trace the execution of test(-4) Java Programming: Program Design Including Data Structures 6

Terminology A recursive method is any method that calls itself Base case VERY IMPORTANT Stops the recursion (prevents infinite loops) Solved directly to return a value without calling the same method again Java Programming: Program Design Including Data Structures 7

Recursive Definitions Directly recursive: method that calls itself Indirectly recursive: method that calls another method and eventually results in the original method call Tail recursive method: recursive method in which the last statement executed is the recursive call Infinite recursion: case where every recursive call results in another recursive call Java Programming: Program Design Including Data Structures 8

Tracing a Recursive Method As always, go line by line Recursive methods may have many copies Every method call creates a new copy and transfers flow of control to the new copy Each copy has its own: code parameters local variables Java Programming: Program Design Including Data Structures 9

Tracing a Recursive Method After completing a recursive call: Control goes back to the calling environment Recursive call must execute completely before control goes back to previous call Execution in previous call begins from point immediately following recursive call Java Programming: Program Design Including Data Structures 10

Factorial Numbers Factorial numbers (i.e., n!) defined recursively: factorial(0) = 1 factorial(n+1) = factorial(n) * n+1 Examples 0! = 1 1! = 1 * 1 = 1 2! = 2 * 1 * 1 = 2 3! = 3 * 2 * 1 * 1 = 6 4! = 4 * 3 * 2 * 1 * 1 = 24 Java Programming: Program Design Including Data Structures 11

Iterative Factorial Method public static int fact(int num) { int tmp = 1; for (int i = 1; i <= num; i++) { tmp *= i; return tmp; Trace fact(5) Java Programming: Program Design Including Data Structures 12

Recursive Factorial Method public static int fact(int num) { if (num == 0) return 1; else return num * fact(num 1); Trace fact(5) Java Programming: Program Design Including Data Structures 13

Recursive Factorial Trace Java Programming: Program Design Including Data Structures 14

Recursion or Iteration? Moral: There is usually more than one way to solve a problem! Iteration (loops to repeat code) Recursion (nested function calls to repeat code) Tradeoffs between two options: Sometimes recursive solution is easier Recursive solution is often slower Java Programming: Program Design Including Data Structures 15

Today s Topic: Recursion (Continued) Java Programming: Program Design Including Data Structures 16

Facts about Recursion Recursive methods call themselves Each call solves an identical problem The code is the same! Successive calls solve smaller/simpler instances Every recursive algorithm has at least one base case A known/easy to solve case Often, when we reach 1 or 0 Java Programming: Program Design Including Data Structures 17

Designing Recursive Algorithms General strategy: Divide and Conquer Questions to ask yourself How can we reduce the problem to smaller version of the same problem? How does each call make the problem smaller? What is the base case? Will you always reach the base case? Java Programming: Program Design Including Data Structures 18

Similarities Closely related to recursive definitions in math Also closely related to proof by induction Inductive proofs work in reverse Start by proving a base case Then show that if it is true for case n, it must also be true for case n+1 Java Programming: Program Design Including Data Structures 19

Exercise Write a recursive function that prints the numbers 1 n in descending order: public void descending(int n) { Java Programming: Program Design Including Data Structures 20

Exercise Write a recursive function that prints the numbers 1 n in descending order: public void descending(int n) { if (n <= 0) return; System.out.println(n); descending(n-1); Java Programming: Program Design Including Data Structures 21

Exercise Write a recursive function to perform exponentiation return x m, assuming m >= 0 public int exp(int x, int m) { Java Programming: Program Design Including Data Structures 22

Exercise Write a recursive function to perform exponentiation return x m, assuming m >= 0 public int exp(int x, int m) { if (m == 0) { return 1; if (m == 1) { return x; return x * exp(x, m-1); Java Programming: Program Design Including Data Structures 23

public static boolean p(string s, int i, int f) if (i < f) { if (s[i] == s[f]) { return p(s, i+1, f-1); else { return false; else { return true; What does p(s,0,s.length-1) return a) if s ="UTEP' b) if s ="SAMS' c) if s ="kayak" d) if s= "ABBA" Java Programming: Program Design Including Data Structures 24

Towers of Hanoi The legend of the temple of Brahma 64 golden disks on 3 pegs The universe will end when the priest move all disks from the first peg to the last Java Programming: Program Design Including Data Structures 25

The Rules Only move one disk at a time A move is taking one disk from a peg and putting it on another peg (on top of any other disks) Cannot put a larger disk on top of a smaller disk With 64 disks, at 1 second per disk, this would take roughly 585 billion years Java Programming: Program Design Including Data Structures 26

Towers of Hanoi: Three Disk Problem Java Programming: Program Design Including Data Structures 27

Towers of Hanoi: Three Disk Solution Java Programming: Program Design Including Data Structures 28

Towers of Hanoi: Three Disk Solution Java Programming: Program Design Including Data Structures 29

Four disk solution (courtesy wikipedia) Java Programming: Program Design Including Data Structures 30

Recursive algorithm idea Final step is to move the bottom disk from peg 1 to peg 3 To do this, the other n-1 disks must be on peg 2 So, we need an way to move n-1 disks from peg 1 to peg 2 Base case: moving the smallest disk is easy (you can always move it to any peg in one step) Java Programming: Program Design Including Data Structures 31

Pseudocode solvetowers (count, source, destination, spare) { if (count == 1) { move directly else { solvetowers(count-1, source, spare, destination) solvetowers(1, source, destination, spare) solvetowers(count-1, spare, destination, source) Java Programming: Program Design Including Data Structures 32

Today s Topic: Recursion (Continued) Java Programming: Program Design Including Data Structures 33

Recursive Fibonacci Java Programming: Program Design Including Data Structures 34

Recursive Fibonacci (continued) public static int fib(int n) { if (n <= 2) { return 1; return fib(n - 1) + fib(n 2) Java Programming: Program Design Including Data Structures 35

Recursive Fibonacci (continued) Java Programming: Program Design Including Data Structures 36

Iterative Solution Recursive solution repeats many computations, so it is very inefficient. An iterative approach: public static int fib(int n) { if (n <= 2) return 1; int f1 = f2 = 1; for (int i = 3; i <= n; i++) { int tmp = f1 + f2; f1 = f2; f2 = tmp; return f2; Java Programming: Program Design Including Data Structures 37

Improved Recursion int[] cache = new int[n]; public static int fib(int n) { if (cache[n] <= 0) { cache[n] = fib(n-2) + fib(n-1); return cache[n]; Bugs? Java Programming: Program Design Including Data Structures 38

Improved Recursion (debugged) int[] cache = new int[n+1]; public static int fib(int n) { if (n <= 2) return 1; if (cache[n] <= 0) { cache[n] = fib(n-2) + fib(n-1); return cache[n]; Java Programming: Program Design Including Data Structures 39

Search Design a method that returns true if element n is a member of array x[] and false if not Iterative approach: public boolean search(int[] x, int n) { for(int i = 0; i < x.length, i++) { if (x[i] == n]) return true; return false; Java Programming: Program Design Including Data Structures 40

Recursive Search A recursive variant: boolean search(int[] x, int size, int n) { if (size > 0) { if (x[size-1] == n) { return true; else { return search(x, size-1, n); return false; Java Programming: Program Design Including Data Structures 41

Faster Search The problem: these methods are slow Recall the phone book example Linear search need to look at every element Binary search is much faster on sorted data Java Programming: Program Design Including Data Structures 42

Binary Search Pseudocode search(phonebook, name) { if only one page, scan for the name else open to the middle determine if name is before or after this page if before search (first half of phonebook, name) else search (second half of phonebook, name) Java Programming: Program Design Including Data Structures 43

boolean binarysearch(int[] x, int start, int end, int n) { if (end < start) return false; int mid = (start+end) / 2; if (x[mid] == n) { return true; else { if (x[mid] < n) { return search(x, mid+1, end, n); else { return search(x, start, mid-1, n); Java Programming: Program Design Including Data Structures 44

Programming Example int test(string s, int last) { if (last < 0) { return 0; if (s.charat(last) == 0 ) { return 2 * test(s, last-1); return 1 + 2 * test(s, last-1); Trace test( 01101, 4) What does method test do? Java Programming: Program Design Including Data Structures 45

Exercise Write a recursive function convert a decimal number into a binary number, printing the binary number public static void dectobin(int num){ Java Programming: Program Design Including Data Structures 46

Exercise Decimal to Binary public static void dectobin(int num) { if (num > 0) { dectobin(num / 2); System.out.print(num % 2); Java Programming: Program Design Including Data Structures 47