Department of Computer Engineering Recursive Problem Solving 2140101 Computer Programming for International Engineers.

Slides:



Advertisements
Similar presentations
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Advertisements

Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
1 Recursion Overview l Introduction to recursion and recursive methods l Simple popular recursive algorithms l Writing recursive methods l Preview: 1-D.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Review of Recursion What is a Recursive Method? The need for Auxiliary (or Helper) Methods How Recursive Methods work Tracing of Recursive Methods.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
IB Computer Science Unit 5 – Advanced Topics Recursion.
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
1 Examples of Recursion Instructor: Mainak Chaudhuri
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Department of Computer Engineering Methods Computer Programming for International Engineers.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Recursion Definition Recursion is the computer programming process, whereby a method calls itself.
1 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Review of Recursion What is a Recursive Method?
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
Abdulmotaleb El Saddik University of Ottawa
Chapter 15 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Definitions
Algorithm Analysis (for Divide-and-Conquer problems)
Chapter 12 Recursion (methods calling themselves)
CS1120: Recursion.
Review of Recursion What is a Recursive Method?
The Hanoi Tower Problem
Chapter 11 Recursion.
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Recursion.
CSC 143 Recursion.
Review of Recursion What is a Recursive Method?
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion Method calling itself (circular definition)
Recursive Thinking.
Review of Recursion What is a Recursive Method?
Presentation transcript:

Department of Computer Engineering Recursive Problem Solving Computer Programming for International Engineers

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Objectives Students should: Be able to explain the concept of recursive definition. Be able to use recursion in Java to solve problems.

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Recursive Problem Solving How to solve problem recursively ? Break a problem into identical but smaller, or simpler problems. Solve smaller problems to obtain a solution to the original one.

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Example: Summation Alternative Solution: public static int s(int n){ int sum = 0; for(int i=0;i<=n;i++) sum += i; return sum; } Let S ( n ) be the sum of integers from 0 to n. Mathematically, we can write: Java Implementation: Iterative Approach

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Example: Summation Write a method finding the sum of integers from 0 to n Let S ( n ) be the sum of integers from 0 to n. Mathematically, we can write: Java Implementation: public static int s(int n){ if(n==0) return 0; return s(n-1)+n; } Recursive Approach

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Example: Summation public static int s(int n){ if(n==0) return 0; return s(n-1)+n; } Solving S ( n ) is broken into solving S ( n -1), which is a simpler (but somewhat identical) problem. A case B B A

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Example: Summation In finding S (2), method invocation can be depicted as: public static int s(int n){ if(n==0) return 0; return s(n-1)+n; } S(2 )

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Example: Factorial Write a method finding n! Mathematically, we can write: Java Implementation: public static int factorial(int n){ int s = 1; for(int i=1;i<=n;i++) s *= i; return s; } Iterative Approach

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Example: Factorial Write a method finding n! Alternatively, we can write: Java Implementation: Recursive Approach public static int factorial(int n){ if(n==0) return 1; return factorial(n-1)*n; }

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY factorial(4) Example: Factorial

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY public static int s(int n){ if(n==0) return 0; return s(n-1)+n; } Recursive Method Design A recursive method must have two parts. – Base cases : determine the case where the recursive method invocation terminates – Recursive cases : recursive calls itself, but with simpler parameters public static intfactorial(int n){ if(n==0) return 1; return factorial(n-1)*n; } Recursive cases Base cases

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Example: Fibonacci Example: Fibonacci Numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …) –The Fibonacci numbers form a sequence of integer defined recursively by:

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Recursive Method Design public class FiboDemo { public static void main(String[] args) { final int n = 20; for(int i=0;i<20;i++) System.out.print(fibo(i)+","); System.out.println(); } public static int fibo(int n){ if(n<=0) return 0; if(n==1) return 1; return fibo(n-1)+fibo(n-2); }

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Recursive Method Design fibo(4)

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Costs of Recursion A recursive method accomplishes its task by successively calling itself. Therefore, there are many invocations of method involved. Each time a recursive call is made, a certain amount of memory must be allocated. For a recursive method that makes very deep recursions, a large amount of memory is required. Does this mean we should avoid recursive algorithms ?

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Example: Fibonacci Numbers Revisited import java.io.*; public class FiboDemo { public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader(newInputStreamReader(System.in)); System.out.print("Enter n:"); int n = Integer.parseInt(stdin.readLine()); System.out.println("---Using fibo() "); System.out.println("F("+n+")="+fibo(n)); System.out.println("---Using fiboNew() "); System.out.println("F("+n+")="+fiboNew(n)); } public static int fibo(int n){ System.out.println("fibo("+n+") is called."); if(n<=0) return 0; if(n==1) return 1; return fibo(n-1)+fibo(n-2); } // continue on the next page

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Example: Fibonacci Numbers Revisited // The same fibo() as the previous example public static int fiboNew(int n){ int [] remember = new int[n+1]; for(int i=0;i<=n;i++) remember[i]=-1; return fiboNew(n,remember); } public static int fiboNew(int n,int [] r){ System.out.println("fiboNew("+n+") is called."); if(n<=0){ r[0]=0; return r[0]; } if(n==1) r[n]=1; else r[n]=(r[n-1]==-1?fiboNew(n-1,r):r[n-1]) + (r[n-2]==-1?fiboNew(n-2,r):r[n-2]); return r[n]; }

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Example: Fibonacci Numbers Revisited From the picture, we can see that finding the 6th Fibonacci number using fibo() requires more than three times as many method invocations as it is required in the case of using fiboNew().

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Example: The Towers of Hanoi Sometimes, the easiest and the least error- prone ways to write programs for solving some problems are recursivemethods. Sometimes, an iterative approach is much more difficult than the recursive ones. See example “Towers of Hanoi”

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Towers of Hanoi Peg APeg BPeg C Goal: Move all disks on Peg A to PegB. Rules: 1. Only one disk can be moved at a time, and this disk must be top disk on a tower. 2. A larger disk cannot be placed on the top of a smaller disk. Using minimum number of moves

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Towers of Hanoi

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Example: The Towers of Hanoi import java.io.*; public class TowerOfHanoiDemo { public static void main(String[] args) throws IOException{ BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter number of disks:"); int n = Integer.parseInt(stdin.readLine()); move(n,"A","B","C"); } // continue on the next page

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Example: The Towers of Hanoi public static void move(int n, String orgPole,String destPole,String otherPole){ String step; if(n<=1){ step = "Move Disk1 from Peg "+orgPole+" to Peg "+destPole; System.out.println(step); }else{ move(n-1,orgPole,otherPole,destPole); step = "Move Disk"+n+" from Peg "+orgPole+" to Peg "+destPole; System.out.println(step); move(n-1,otherPole,destPole,orgPole); }

Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY Example: The Towers of Hanoi Try solving it using an iterative approach.