Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Chapter 20 Recursion.
Data Structures Through C
Algorithm Design Techniques
Feichter_DPG-SYKL03_Bild-01. Feichter_DPG-SYKL03_Bild-02.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
Chapter 3: Top-Down Design with Functions Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
Multiplying binomials You will have 20 seconds to answer each of the following multiplication problems. If you get hung up, go to the next problem when.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Year 6 mental test 5 second questions
Around the World AdditionSubtraction MultiplicationDivision AdditionSubtraction MultiplicationDivision.
ZMQS ZMQS
Solve Multi-step Equations
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
© 2006 Pearson Education Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William.
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.
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
CSC 205 Programming II Lecture 10 Towers of Hanoi.
CS1010: Programming Methodology
Chapter Objectives To learn about recursive data structures and recursive methods for a LinkedList class To understand how to use recursion to solve the.
Chapter 17 Recursion.
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
ABC Technology Project
1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"
2 |SharePoint Saturday New York City
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Squares and Square Root WALK. Solve each problem REVIEW:
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
Chapter 5 Test Review Sections 5-1 through 5-4.
SIMOCODE-DP Software.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 7: Recursion Java Software Structures: Designing and Using.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Addition 1’s to 20.
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Chapter 8: Recursion Java Software Solutions
Week 1.
Analyzing Genes and Genomes
C OMP 110 R ECURSION Instructor: Jason Carter. 2 R ECURSION English Return (Oxford/Webster) procedure repeating itself indefinitely or until condition.
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Starting Out with Java: From Control Structures through Objects
Intracellular Compartments and Transport
PSSA Preparation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Essential Cell Biology
How Cells Obtain Energy from Food
Introduction to Recursion and Recursive Algorithms
Towers of Hanoi
Lecture 12 Recursion part 1
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Presentation transcript:

Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06

2 A Game My number = 10* the number inside + 1

3 A Game My number = 10* the number inside + 1

4 A Game My number = 10* the number inside + 1

5 A Game My number =1

6 A Game My number = 10* = 11

7 A Game My number = 10* = 111

8 A Game My number = 10* =1111

9 Examples of Recursion

10 Examples of Recursion Divide the line into 16 segments:

11 Outline The concept of recursion How to write and use recursive method How the recursive method are executed Recursion vs. iteration Examples: Tower of Hanoi Summary

12 Previous Game My number =1 Simple case Produce a same problem with smaller size

13 Definition of Recursion A computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself in a step having a termination condition so that successive repetitions are processed up to the critical step until the condition is met at which time the rest of each repetition is processed from the last one called to the first

14 Recursion Concepts Two parts to recursion: Base case(s) – termination If the problem is easy, solve it immediately. Recursion step – call itself If the problem can't be solved immediately, divide it into smaller problems, then Solve the smaller problems by applying this procedure to each of them.. Divide Conquer Converge

15 Previous Game For any n>0: num(n) -> num(n-1) -> num(n-2)….-> num(0) n: # of boxes Recursion step: num(n) = 10*num(n-1) + 1 My number =1 Base case: num(0) = 1 My number = 10* the number inside + 1

16 Recursion Method Recursion step: num(n) = 10*num(n-1) + 1 Base case: num(0) = 1 int Num(int n) { if (n == 0) return 1; else return 10*Num(n-1) +1; }

17 Complete Program class NumCalc { int Num( int n ) { if ( n == 0 ) return 1; else return 10*Num(n-1) +1; } class NumCaclTester { public static void main ( String[] args) { NumCalc numcalc = new NumCalc(); int result = numcalc.Num( 3 ); System.out.println(numcalc(3) is " + result ); } Result: numcalc(3) is 1111

18 Recap Skeleton for a recursive Java method type solution(type para ) { if ( base case ) { return something easily computed } else { divide problem into pieces return something calculated from the solution to each piece }

19 Outline The concept of recursion How to write and use recursive method How the recursive method are executed Recursion vs. iteration Examples: Tower of Hanoi Summary

20 Recursive Evaluation num(3) num(3) = 10 * num(2) + 1 = 10 * num(2) + 1 = 10 * (10 * num(1) + 1) + 1 = 10 * (10 * num(1) + 1) + 1 = 10 * (10 * (10 * num(0) + 1) +1) + 1 = 10 * (10 * (10 * num(0) + 1) +1) + 1 = 10 * (10 * (10 * 1 + 1) + 1 ) + 1 = 10 * (10 * (10 * 1 + 1) + 1 ) + 1 = 10 * (10 * ) + 1 = 10 * (10 * ) + 1 = 10 * = 10 * = 1111 = 1111 Num parameter 3 parameter 0 Num parameter 1 Num parameter 2 return 1111 return 1 return 11 return 111

21 Recursion and Method Call Stack Num(3) Num parameter 3 parameter 0 Num parameter 1 Num parameter 2 Num(2) Num(1) Num(0) top of stack When a method is called, push the method and parameters into the stack

22 Recursion and Method Call Stack Num(3) Num parameter 3 parameter 0 Num parameter 1 Num parameter 2 return 1111 return 1 return 11 return 111 Num(2) Num(1) Num(0) top of stack When a method is returned, pop the method and parameters off the stack

23 Factorials n! = n* (n-1) * … * 1, (n> 0) 0! = 1 Recursion step: n! = n*(n-1)! Base case: 0! = 1, 1! = 1 int factorial(int n) { if (n <= 1) return 1; else return n*factorial(n-1); } 1! 2! =2*1! 3! =3*2! 4! =4*3! parameter 4 parameter 1 parameter 2 parameter 3 return 24 return 1 return 2 return 6

24 Iteration int factorial(int n) { int result = 1; for (int i = n; i>=1; i--); result *= i; return result; } n! = n* (n-1) * … * 1, 0! = 1

25 Recursion vs. Iteration int factorial(int n) { int result = 1; for (int i = n; i>=1; i--); result *= i; return result; } int factorial(int n) { if (n <= 1) return 1; else return n*factorial(n-1); } IterationRecursion RepetitionExplicit statement e.g. for, while Repeat method calls Termination test Loop-continuation condition fails Base case is reached Approach termination Modify a counter toward fail condition Produce simpler version of the original problem OverheadLessMore

26 Towers of Hanoi Problem: How to move disks from peg 1 to 3, subjected to: - Only one disk is moved at a time - A larger disk cant be placed above a smaller disk at any time - Peg 2 is used for temporarily holding disks

27 Towers of Hanoi1 2 3 Case 1: move 1 disk from peg 1 to 3, using peg 2 -- Move disk 1 from peg 1 to peg 3 directly 1 1

28 Towers of Hanoi Case 2: move 2 disks from peg 1 to 3, using peg 2 - Move disk 2 from peg 1 to 2 - Move disk 1 from peg 1 to 3 - Move disk 2 from peg 2 to 3

Towers of Hanoi Case 3: move 5 disks from peg 1 to 3 - Move 4 disks(2-5) from peg 1 to 2, using peg 3 - Move disk 1 from peg 1 to 3 - Move 4 disks(2-5) from peg 2 to 3, using peg

30 Towers of Hanoi Case 3: move 5 disks from peg 1 to 3 - Move 4 disks(2-5) from peg 1 to 2, using peg 3 - Move disk 1 from peg 1 to 3 - Move 4 disks(2-5) from peg 2 to 3, using peg 1 How? Same way with less disks

31 Towers of Hanoi General Case : move n disks from peg 1 to 3, using peg 2 - Move n-1 disks from peg 1 to 2, using peg 3 - Move last disk from peg 1 to 3 - Move n-1 disks from peg 2 to 3, using peg 1 void solveTowers (int disks, int srcpeg, int destpeg, int temppeg) { if (disks == 1) { System.out.printf(\n %d -> %d, srcpge,destpeg); } else { solveTowers(disks -1, srcpeg, temppeg, destpeg); System.out.printf(\n%d -> %d, srcpeg,destpeg); solveTowers(disks -1, temppeg, destpeg, srcpeg); } return; }

32 Towers of Hanoi Question: How many moves are need to move n disks from peg 1 to 3? void solveTowers (int disks, int srcpeg, int destpeg, int temppeg) { if (disks == 1) { System.out.printf(\n %d -> %d, srcpge,destpeg); } else { solveTowers(disks -1, srcpeg, temppeg, destpeg); System.out.printf(\n%d -> %d, srcpeg,destpeg); solveTowers(disks -1, temppeg, destpeg, srcpeg); } return; } 2 n -1

33 Towers of Hanoi Case : move 5 disks from peg 1 to 3, using peg 2 Result: 1 --> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > 3

34 Other Examples Fibonacci Series String Permutations Fractals Binary Search …

35 Summary The concept of recursion How to write and use recursive method How the recursive method are executed Recursion vs. iteration Examples: Tower of hanoi In order to understand recursion you must first understand recursion. Unknown