1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

Slides:



Advertisements
Similar presentations
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.
Advertisements

Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
More on Recursion More techniques 1. Binary search algorithm Binary searching for a key in an array is similar to looking for a word in dictionary Take.
COMPSCI 105 S Principles of Computer Science Recursion 1.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
Self-Reference - Induction Cmput Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
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 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introduction to C Programming CE Lecture 21 Recursion and Linear Linked Lists.
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
1 “Not all recursive solutions are better than iterative solutions…” “… recursion, however, can provide elegantly simple solutions to problems of great.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Cosc236/recursion1 Recursion Recursive method calls itself Recursion frequently occurs in mathematics Recursive definition –2 parts base part (basis) recursive.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
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.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion Lecture 6 Dr. Musab.
CMPT 225 Recursion. Objectives Understand how the Fibonacci series is generated Recursive Algorithms  Write simple recursive algorithms  Analyze simple.
Chapter 6 Questions Quick Quiz
1 Lecture 18: Selection Sort, Quicksort & Merge Sort Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
1 Lecture 15: Big O Notation (Wednesday) Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science Test See Web for Details Don’t be deleted!
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
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 You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
Recursion.
CSC 205 Programming II Lecture 8 Recursion.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Recursion CENG 707.
Recursion CENG 707.
Chapter 15 Recursion.
Recursion: The Mirrors
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Recursion Chapter 11.
Calculate n! Multiply my number by the factorial of the rest of the numbers. if( num > 2 ) return num * factorialR2( num - 1 ); else return 2;
Recursion.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Recursion Chapter 18.
Recursion: The Mirrors
Java Programming: Chapter 9: Recursion Second Edition
CSC 143 Recursion.
Recursion Method calling itself (circular definition)
Main() { int fact; fact = Factorial(4); } main fact.
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursive Thinking.
Recursion: The Mirrors
Presentation transcript:

1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working on it.

2 Recursion: Compute using self-reference Iteration: Compute using loops (for, while, etc.)

3 Definition: “A phone directory is a list of names and telephone numbers in alphabetical order by surname” Searching a Phone Directory “It’s easy if there’s only one name in the book!” “But it’s hard if the book is any larger.”

4 Factorial Factorial(n) = 1 x 2 x 3 x … n Factorial(0) = 1 Iterative Definition Factorial(n) = n * Factorial(n-1) Factorial(0) = 1 Recursive Definition Textbook, pp

5 In Java public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p. 53

6 Tracing Recursive Programs Recursion for void methods The Towers of Hanoi

7 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n = ? A: fact(n-1)= ? return= ? A For each method call, make a box with space for: Values of method’s parameters and local variables Return values of all called methods Return value for this method Box Tracing

8 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n = 3 A: fact(n-1)= ? return= ? A

9 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n = 3 A: fact(n-1)= ? return= ? n= 2 A: fact(n-1)= ? return= ? A A

10 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n= 1 A: fact(n-1)= ? return= ? n = 3 A: fact(n-1)= ? return= ? n= 2 A: fact(n-1)= ? return= ? A A A

11 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n = 0 A: fact(n-1)= ? return= ? n= 1 A: fact(n-1)= ? return= ? n = 3 A: fact(n-1)= ? return= ? n= 2 A: fact(n-1)= ? return= ? A A A A

12 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n = 0 A: fact(n-1)= ? return= 1 n= 1 A: fact(n-1)= 1 return= ? n = 3 A: fact(n-1)= ? return= ? n= 2 A: fact(n-1)= ? return= ? A A A A

13 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n = 0 A: fact(n-1)= ? return= 1 n= 1 A: fact(n-1)= 1 return= 1 n = 3 A: fact(n-1)= ? return= ? n= 2 A: fact(n-1)= 1 return= ? A A A A

14 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n = 0 A: fact(n-1)= ? return= 1 n= 1 A: fact(n-1)= 1 return= 1 n = 3 A: fact(n-1)= 2 return= ? n= 2 A: fact(n-1)= 1 return= 2 A A A A

15 Proving recursive algorithms 1.Prove each base cases works 2.Prove each recursive case works* 3.Prove all recursive calls meet a base case public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Proof by induction Textbook, p

16 Tracing Recursive Programs Recursion for void methods The Towers of Hanoi

17 Reversing a String Textbook, p. 59ff god “ ”

18 Reversing a String Textbook, p. 59ff god “ ” If string is empty do nothing

19 Reversing a String Textbook, p. 59ff god “ ” If string is empty do nothing Otherwise, Output last character Reverse substring of first (n-1) characters

20 Reversing a String Formal Box Trace Textbook, p. 62 god reverse “ ” If string is empty do nothing Otherwise, Output last character Reverse substring of first (n-1) characters

21 More than one way …. Textbook, p. 63ff If string is empty do nothing Otherwise, Reverse substring of last (n-1) characters Output first character

22 Invariants public static int fact( int n ) { if ( n == 0 ) { return 1; } else { // Invariant: return n * fact( n-1 ); } Textbook, p. 56

23 Loop invariants Is there something that we want to be true every time the while test is executed? // Computes the sum of // the first n items. int sum = 0; int j = 0; while (j < n) { sum += item[j]; j++; }

24 Loop invariants sum contains the sum of the first j items j is in the range 0..n-1 Textbook, p. 12 // Computes the sum of // the first n items. int sum = 0; int j = 0; while (j < n) { sum += item[j]; j++; }