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.

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

Factorial Recursion stack Binary Search Towers of Hanoi
Recursion. Binary search example postponed to end of lecture.
Programming with Recursion
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.
Recursion.
1 Chapter 7 Recursion. 2 What Is Recursion? l Recursive call A method call in which the method being called is the same as the one making the call l Direct.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Prof. S.M. Lee Department of Computer Science. Answer:
Recursion and Implementation of Functions
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.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
1 Chapter 1 RECURSION. 2 Chapter 1  Subprogram implementation  Recursion  Designing Recursive Algorithms  Towers of Hanoi  Backtracking  Eight Queens.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
CS 1704 Introduction to Data Structures and Software Engineering.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
Chapter 12 Recursion, Complexity, and Searching and Sorting
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
1 Lecture 14 Chapter 18 - Recursion. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County.
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 Joe Meehean.  call themselves directly  or indirectly void f(){... f();... } void g(){... h();... } void h(){... g();... }
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 17: Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Chapter 7 Programming with Recursion. What Is Recursion? Recursive call A method call in which the method being called is the same as the one.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Concepts of Algorithms CSC-244 Unit 5 and 6 Recursion Shahid Iqbal Lone Computer College Qassim University K.S.A.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
Program Development and Design Using C++, Third Edition
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Recursion ITFN The Stack. A data structure maintained by each program at runtime. Push Pop.
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 Powerful Tool
Programming with Recursion
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Chapter 8: Recursion Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray.
Recursion CSE 2320 – Algorithms and Data Structures
Topic 6 Recursion.
Recursion DRILL: Please take out your notes on Recursion
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Introduction to C++ Recursion
Programming with Recursion
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursion Chapter 11.
RECURSION Annie Calpe
CSC 143 Recursion.
Yan Shi CS/SE 2630 Lecture Notes
Chapter 18 Recursion.
Presentation transcript:

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 interest: “The value after 10 years is equal to the interest rate times the value after 9 years.”  A phrase is a "palindrome" if the 1st and last letters are the same, and what's inside is itself a palindrome (or is empty).

CSC 143 P 3 Computer Science Examples  Recursive procedure: a procedure that invokes itself  Recursive data structures: a data structure may contain a pointer to an instance of the same type struct Node { int data; Node *next; };  Recursive definitions: if A and B are postfix expressions, then A B + is a postfix expression

CSC 143 P 4 Factorial n! ( “ n factorial” ) can be defined in two ways:  Non-recursive definition n! = n * (n-1) * (n-2) … * 2 * 1  Recursive definition n! = 1, if n = 1 n*(n-1)!, if n > 1

CSC 143 P 5 Factorial (2)  How do we write a function that reflects the recursive definition? int factorial(int n) { if ( n == 1 ) return 1; else return n * factorial(n-1); }  Note that the factorial function calls itself.  How can this work?

CSC 143 P 6 Activation Records  Remember that local variables and parameters are allocated when a function is entered, deleted when the function exits (automatic storage).  Here's how:  Whenever a function is called, a new activation record is pushed on the runtime stack, containing: -- a separate copy of all local variables and parameters -- control info (e.g. return address)  Activation record is popped at end of function (or block)  A recursive function call is no different in this respect  Each recursive call has its own copy of locals

CSC 143 P 7 Example int fact (int n) { if ( n == 1 ) return 1; else return n * fact (n-1); }... int main () { int x = fact (4); cout << “4! = “ << x << endl;...

CSC 143 P * fact(3) 3 3 * fact(2) 2 2 * fact(1) n (different memory location) result n n fact(4) n result 24 (=4!)

CSC 143 P 9 Infinite Recursion  Must always have some way to make recursion stop, otherwise it runs forever: int BadFactorial(n) { int x = BadFactorial(n-1); if ( n == 1 ) return 1; else return n * x; }  What is the value of BadFactorial(2) ?

CSC 143 P 10 Infinite Recursion (2) Similar problems with incorrect recursive definitions of data types: struct Node { int data; Node next; }; data next... How does Node *next; solve this problem?

CSC 143 P 11 Using Recursion Properly  For correct recursion (recursion that does something useful and eventually stops), need two parts: 1. One or more base cases that are not recursive if ( n == 1 ) return 1; 2. One or more recursive cases that operate on smaller problems that get closer to the base case(s) return n * factorial(n-1);  The base case(s) should always be checked before the recursive calls

CSC 143 P 12 Recursive Data Structures  Many data structures are defined in terms of (pointers to) other instances of themselves  Linked Lists and Trees (that we haven't seen) are examples: struct Node { int data; Node *next; }; struct TreeNode { int data; TreeNode *left; TreeNode *right; };  Recursive data structures suggest recursive algorithms

CSC 143 P 13 Printing a Linked List void print(Node* first) { if (first == NULL) return; else { cout data << “ “; print(first->next); } }  How many recursive calls do we make when printing the list ?

CSC 143 P 14 Printing in Reverse Order  At first, seems difficult. All the pointers point only forward.  Recursion to the rescue! void print(Node* first) { if (first == NULL) return; else { print(first->next); cout data << “ “; } }

CSC 143 P 15 Summing a List int listSum(Node* list) { if (list == NULL) return 0;// empty list has sum == 0 else return list->data+listSum(list->next); }  How would you modify this to count the length of a list? Add N to each element of a list?  What is the cost in time compared to a loop? How much space does it take?

CSC 143 P 16 List Remove  Remove nodes with a given data value from list Node* ListRemove(Node *first, int v){ if (first == NULL) return NULL; else if (first->data != v){ Node* newNode=new Node; newNode->data=first->data; newNode->next=ListRemove(first->next,v) ; return newNode; } else return ListRemove(first->next, v); }

CSC 143 P 17 What does this function do? int mystery (int x) { assert (x > 0); if (x == 1) return 0; int temp = mystery (x / 2); return 1 + temp; }

CSC 143 P 18 How about this one int g(int n) { if (n <= 1) return 1; else if (n % 2 == 0)// n even return 1 + g(n / 2); else // n odd return 1 + g(3* n + 1); } g(7) = 1 + g(22) = 2 + g(11) = 3 + g(34) = 4 + g(17) = 5 + g(52) = 6 + g(26) = 7 + g(13) = 8 + g(40) = 9 + g(20) = 10 + g(10) = 11 + g(5) =12 + g(16) = 13 + g(8) = 14 + g(4) =15 + g(2) = 16 + g(1) = 17

CSC 143 P 19 Recursion vs. Iteration  When to use recursion?  Processing recursive data structures  “Divide & Conquer” algorithms: 1. Divide problem into subproblems 2. Solve each subproblem recursively 3. Combine subproblem solutions  When to use iteration instead?  Nonrecursive data structures  Problems with obvious iterative structure  Any iteration can be rewritten using recursion, and vice-versa (at least in theory)  Iteration generally uses less (stack) space and is somewhat faster

CSC 143 P 20 Which is Better?  If a single recursive call is at the very end of the function:  Known as tail recursion  Easy to rewrite iteratively  Recursive problems that are not tail recursive are harder to write nonrecursively  Usually have to simulate recursion with a stack  Some programming languages provide no loops (loops are implemented through if and recursion)

CSC 143 P 21 Summary  Recursion is something defined in terms of itself  Recursive procedures  Recursive data structures  Recursive definitions  Activation records make it work  Two parts of all recursive functions  Base case(s)  Recursive case(s)  Base case always checked first  Examples  Factorial (n!)  Recursive linked list manipulation