C OMP 110 R ECURSION Instructor: Jason Carter. 2 R ECURSION English Return (Oxford/Webster) procedure repeating itself indefinitely or until condition.

Slides:



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

Chapter 20 Recursion.
Chapter 1 The Study of Body Function Image PowerPoint
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Factors, Primes & Composite Numbers
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
Summary of Convergence Tests for Series and Solved Problems
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.
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.
ALGEBRAIC EXPRESSIONS
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULTIPLYING MONOMIALS TIMES POLYNOMIALS (DISTRIBUTIVE PROPERTY)
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Addition Facts
ALGEBRAIC EXPRESSIONS
Year 6 mental test 5 second questions
Year 6 mental test 10 second questions
ZMQS ZMQS
Richmond House, Liverpool (1) 26 th January 2004.
BT Wholesale October Creating your own telephone network WHOLESALE CALLS LINE ASSOCIATED.
Chapter 17 Recursion.
Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.
ABC Technology Project
1 / / / /. 2 (Object) (Object) –, 10 (Class) (Class) –, –, – (Variables) [ Data member Field Attribute](, ) – (Function) [ Member function Method Operation.
2 |SharePoint Saturday New York City
© S Haughton more than 3?
VOORBLAD.
15. Oktober Oktober Oktober 2012.
Progam.-(6)* Write a program to Display series of Leaner, Even and odd using by LOOP command and Direct Offset address. Design by : sir Masood.
Factors, Prime Numbers & Composite Numbers
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)
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:
© 2012 National Heart Foundation of Australia. Slide 2.
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Chapter 5 Test Review Sections 5-1 through 5-4.
© 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.
25 seconds left…...
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Test B, 100 Subtraction Facts
H to shape fully developed personality to shape fully developed personality for successful application in life for successful.
Januar MDMDFSSMDMDFSSS
Week 1.
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
1 Unit 1 Kinematics Chapter 1 Day
PSSA Preparation.
Introduction to Recursion and Recursive Algorithms
Chapter 30 Induction and Inductance In this chapter we will study the following topics: -Faraday’s law of induction -Lenz’s rule -Electric field induced.
Copyright © Cengage Learning. All rights reserved.
Lecture 12 Recursion part 1
Recursion English –Return (Oxford/Webster) –procedure repeating itself indefinitely or until condition met, such as grammar rule (Webster) adequate: satisfactory.
Presentation transcript:

C OMP 110 R ECURSION Instructor: Jason Carter

2 R ECURSION English Return (Oxford/Webster) procedure repeating itself indefinitely or until condition met, such as grammar rule (Webster) adequate : satisfactory satisfactory : adequate recursion : recursion Mathematics expression giving successive terms of a series (Oxford) Programming Method calling itself. On a smaller problem. Alternative to loops.

3 R ECURSION Recursive Functions Recursive Procedures Number-based Recursion List-based Recursion

4 F ACTORIAL ( N ) public static int factorial( int n) { int product = 1; while (n > 0) { product *= n; n -= 1; } return product; } public static void main (String[] args) { while (true) { // loop condition never false int n = Console.readInt(); if (n < 0) break; System.out.println("factorial = " + factorial(n)); } 1*2*3*…*n

5 D EFINING F ACTORIAL ( N ) Product of the first n numbers 1*2*3*…*n factorial(0)= 1 factorial(1)= 1= 1*factorial(0) factorial(2)= 2*1= 2*factorial(1) factorial(3)= 3*2*1= 3*factorial(2) factorial(4)= 4*3*2*1= 4*factorial(3) factorial(n)= n*n-1*…*1= n*factorial(n-1)

6 D EFINING F ACTORIAL ( N ) factorial(n) = 1if n == 0 factorial(n) = n*factorial(n-1)if n > 0

7 I MPLEMENTING F ACTORIAL ( N ) (E DIT ) factorial(n) = 1if n == 0 factorial(n) = n*factorial(n-1)if n > 0 public static int factorial( int n) { … }

8 I MPLEMENTING F ACTORIAL ( N ) factorial(n) = 1if n == 0 factorial(n) = n*factorial(n-1)if n > 0 public static int factorial ( int n) { if (n == 0) return 1; if (n > 0) return n*factorial(n-1); } n < 0 ? Function must return something for all cases Multiple return Early return

9 I MPLEMENTING F ACTORIAL ( N ) factorial(n) = 1if n == 0 factorial(n) = n*factorial(n-1)if n > 0 public static int factorial( int n) { if (n == 0) return 1; else if (n < 0) return factorial(-n); else return n*factorial(n-1); } Base case Recursive Reduction Steps factorial(n) = - factorial(n)if n < 0

10 R ECURSIVE M ETHODS Should have base case(s) Recurse on smaller problem(s) recursive calls should converge to base case(s)

11 G ENERAL F ORM OF A R ECURSIVE M ETHOD if (base case 1 ) return solution for base case 1 else if (base case 2) return solution for base case 2 …. else if (base case n) return solution for base case n else if (recursive case 1) do some preprocessing recurse on reduced problem do some postprocessing … else if (recursive case m) do some preprocessing recurse on reduced problem do some postprocessing

12 R ECURSION VS. L OOPS (I TERATION ) public static int factorial( int n) { int product = 1; while (n > 0) { product *= n; n -= 1; } return product; } public static int factorial( int n) { if (n == 0) return 1; else if (n < 0) return factorial(-n); else return n*factorial(n-1); } Implementation follows from definition

13 T RACING R ECURSIVE C ALLS Call Stack Locals

14 T RACING R ECURSIVE C ALLS Invocationnreturn value factorial(1)1? factorial(2)2? Invocationnreturn value factorial(0)0? factorial(1)1? factorial(2)2?

15 T RACING R ECURSIVE C ALLS Call Stack Locals

16 T RACING R ECURSIVE C ALLS Invocationnreturn value factorial(0)01 factorial(1)1? factorial(2)2? Invocationnreturn value factorial(0)01 factorial(1)11 factorial(2)2? Invocationnreturn value factorial(0)01 factorial(1)11 factorial(2)22

17 R ECURSION P ITFALLS public static int factorial ( int n) { return n*factorial(n-1); } factorial(2) 2*factorial(1) 1*factorial(0) 0*factorial(-1) -1*factorial(-2) … Infinite recursion! (stack overflow) No base case

18 R ECURSION P ITFALLS public static int factorial ( int n) { if (n == 0) return 1; else if (n < 0) return factorial(-n); else return n*factorial(n-1); } factorial(2) factorial(3)/3 factorial(4)/4 factorial(5)/5 factorial(6)/6 … Infinite recursion! Recurses on bigger problem

19 R ECURSIVE F UNCTIONS WITH M ULTIPLE P ARAMETERS power(base, exponent) base exponent base*base*base*…*base Exponent # of times power(0, exponent)= 0 power(1, exponent)= 1 power(2, exponent)= 2*2*…*2 (exponent times) power(3, exponent)= 3*3*…*3 (exponent times) No pattern!

20 R ECURSIVE F UNCTIONS WITH M ULTIPLE P ARAMETERS (E DIT ) power(base, exponent) base exponent base*base*base*…*base Exponent # of times

21 R ECURSIVE F UNCTIONS WITH M ULTIPLE P ARAMETERS (E DIT ) power(base, exponent) base exponent base*base*base*…*base Exponent # of times power(base, 0)= 1 power(base, 1)= base*1= base*power(base, 0) power(base, 2)= base*base*1= base*power(base, 1) power(base, exponent)= base*power(base, exponent-1)

22 D EFINING POWER ( BASE, EXPONENT ) power(base, exponent)= 1if exponent <= 0 if exponent > 0power(base, exponent)= base*power(base, exponent-1) public static int power( int base, int exponent) { if (n <= 0) return 1 ; else return base * power(base, exponent-1); }

23 R ECURSIVE P ROCEDURES : GREET ( N ) greet(0)greet(1) print “hello” greet(2) print “hello” greet(n) print “hello” n times

24 D EFINING GREET ( N ) (E DIT ) greet(0)greet(1) print “hello” greet(2) print “hello” greet(n) print “hello” n times

25 D EFINING GREET ( N ) greet(0)greet(1) print “hello” greet(2) print “hello” greet(n) print “hello” n times do nothing; greet(0); print “hello”; greet(1); print “hello”; greet(n-1); print “hello”;

26 D EFINING GREET ( N ) greet(n)do nothing;if n <= 0 greet(n) greet(n-1); print “hello”; if n > 0

27 I MPLEMENTING GREET ( N ) (E DIT ) greet(n)do nothing;if n <= 0 greet(n) greet(n-1); print “hello”; if n > 0

28 I MPLEMENTING GREET ( N ) greet(n)do nothing;if n <= 0 greet(n) greet(n-1); print “hello”; if n > 0 public static void greet ( int n) { if (n > 0) { greet(n-1); System.out.println(“hello”); }

29 L IST - BASED R ECURSION : MULTIPLY L IST () multiplyList()= 1if remaining input is: -1 multiplyList()= 2if remaining input is: 2, -1 multiplyList()= 12if remaining input is: 2, 6, -1 multiplyList() = readNextVal() * multiplyList()if nextVal > 0

30 L IST - BASED R ECURSION : MULTIPLY L IST () multiplyList() = 1if nextVal < 0 multiplyList() = readNextVal() * multiplyList()if nextVal > 0 public static int multiplyList () { int nextVal = Console.readInt(); if (nextVal < 0) return 1; else return nextVal*multiplyList(); }

31 T RACING MULTIPLY L IST () public static int multiplyList () { int nextVal = Console.readInt(); if (nextVal < 0) return 1; else return nextVal*multiplyList(); } InvocationRemaining inputReturn value multiplyList()2, 30, -1? InvocationRemaining inputReturn value multiplyList()30, -1? multiplyList()2, 30, -1? InvocationRemaining inputReturn value multiplyList()? multiplyList()30, -1? multiplyList()2, 30, -1? InvocationRemaining inputReturn value multiplyList()1 multiplyList()30, -1? multiplyList()2, 30, -1? InvocationRemaining inputReturn value multiplyList()1 multiplyList()30, -130 multiplyList()2, 30, -1? InvocationRemaining inputReturn value multiplyList()1 multiplyList()30, -130 multiplyList()2, 30, -160