1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.

Slides:



Advertisements
Similar presentations
Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.
Advertisements

Recursion.
Recursion. Binary search example postponed to end of lecture.
Programming with Recursion
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.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
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.
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
Fundamental in Computer Science Recursive algorithms 1.
Data Structures Using C++1 Chapter 11 Binary Trees.
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 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
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.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Chapter 4: Induction and Recursion
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.
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
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.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
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.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
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.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Tail Recursion l The case in which a function contains only a single recursive call and it is the last statement to be executed in the function. l Tail.
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.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
Pei Zheng, Michigan State University 1 Chapter 8 Recursion.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
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.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
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.
递归算法的效率分析. When a function is called... A transfer of control occurs from the calling block to the code of the function --It is necessary that there be.
Recursion Powerful Tool
Programming with Recursion
Recursion.
Chapter 4: Induction and Recursion
Trees Saurav Karmakar
Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
Induction and Recursion
Programming with Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Chapter 18-3 Recursion Dale/Weems.
Trees.
Module 1-10: Recursion.
When a function is called...
Yan Shi CS/SE 2630 Lecture Notes
Chapter 18 Recursion.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Presentation transcript:

1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas

2 Mathemetical Induction To prove Let p(n) denote the statement involving the integer variable n. The Principle of Mathematical Induction states: If p(1) is true and, for some integer K >=1, p(k+1) is true whenever p(k) is true then p(n) is true for all n>=1. 4 steps in using Induction: Base cases; --- p(1), p(2), … Induction hypothesis (IH); --- assume p(k) is true Statement to be proved in induction; --- it is true for p(k+1) Induction step.--- prove p(k+1) is true based on IH

3 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we need to avoid making an infinite sequence of function calls (infinite recursion)

4 Finding a Recursive Solution a recursive solution to a problem must be written carefully the idea is for each successive recursive call to bring you one step closer to a situation in which the problem can easily be solved this easily solved situation is called the base case each recursive algorithm must have at least one base case, as well as a general (recursive) case

5 General format for Many Recursive Functions if (some easily-solved condition) // base case solution statement else // general case recursive function call

6 A recursive definition int s (int n) { if (n ==1) return 1; else return s(n-1) + n; } A few of problems: n  0 at the beginning; return value might be too large to fit in an int.

7 When a function is called... a transfer of control occurs from the calling block to the code of the function--it is necessary that there be a return to the correct place in the calling block after the function code is executed; this correct place is called the return address when any function is called, the run-time stack is used--on this stack is placed an activation record for the function call

8 Stack Activation Frames the activation record contains the return address for this function call, and also the parameters, and local variables, and space for the function’s return value, if non-void the activation record for a particular function call is popped off the run-time stack when the final closing brace in the function code is reached, or when a return statement is reached in the function code at this time the function’s return value, if non-void, is brought back to the calling block return address for use there

9 A Stake of Activation Records S(1) S(2) S(3) S(4) Main()

10 int Func ( /* in */ int a, /* in */ int b ) { int result; if ( b == 0 ) // base case result = 0; else if ( b > 0 ) // first general case result = a + Func ( a, b - 1 ) ) ; // instruction 50 return result; } A recursive function

11 FCTVAL ? result ? b 2 a 5 Return Address 100 Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 100 original call at instruction 100 pushes on this record for Func(5,2)

12 FCTVAL ? result ? b 1 a 5 Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 Return Address 100 record for Func(5,2) call in Func(5,2) code at instruction 50 pushes on this record for Func(5,1) Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 100

13 FCTVAL ? result ? b 0 a 5 Return Address 50 FCTVAL ? result 5+Func(5,0) = ? b 1 a 5 Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 Return Address 100 record for Func(5,2) record for Func(5,1) call in Func(5,1) code at instruction 50 pushes on this record for Func(5,0) Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 100

14 FCTVAL 0 result 0 b 0 a 5 Return Address 50 FCTVAL ? result 5+Func(5,0) = ? b 1 a 5 Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 Return Address 100 record for Func(5,0) is popped first with its FCTVAL record for Func(5,2) record for Func(5,1) Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 100

15 record for Func(5,2) record for Func(5,1) is popped next with its FCTVAL FCTVAL 5 result 5+Func(5,0) = 5+ 0 b 1 a 5 Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 Return Address 100 Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 100

16 FCTVAL 10 result 5+Func(5,1) = 5+5 b 2 a 5 Return Address 100 record for Func(5,2) is popped last with its FCTVAL Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 100

Let’s try an example 17

18 Too much recursion Can Be Dangerous Fibonacci numbers. Long fib (int n) { If (n <=1) return n; Else return fib(n-1) + fib(n-2); }

19 Too much Recursion Can be Dangerous This definition will lead to exponential running time. Reason: -- too much redundant work Not necessary to use recursive Write a non-recursive Fibonacci code

20 Fibonacci

21 Tree Tree is a fundamental structure in computer science. Recursive definition: A tree is a root and zero or more nonempty subtrees. Nonrecursive definition: A tree consists of s set of nodes and a set of directed edges that connect pairs of nodes. That is, a connected graph without loop.

22 Traversal Three standard traversal order preorder - V L R inorder - L V R postorder - L R V Preorder: traverse the node itself first, then all nodes in the LEFT subtree, then all nodes in the RIGHT subtree. Inorder: traverse all nodes in the LEFT subtree first, then the node itself, then all nodes in the RIGHT subtree. Postorder: traverse all nodes in the LEFT subtree first, then all nodes in the RIGHT subtree, then the node itself, V RL

23 Recursive Traversal Implementation Void PrintInorder (root) if root != null PrintInorder(root->left); print(root->data); PrintInorder(root->right); endif; Void PrintInorder (root) if root != null PrintInorder(root->left); print(root->data); PrintInorder(root->right); endif; The difference is the order of the three statements in the ‘IF’ preorder : inorder : postorder : preorder : inorder : postorder : Void PrintPreorder (root) if root != null print(root->data); PrintPreorder(root->left); PrintPreorder(root->right); endif; Void PrintPreorder (root) if root != null print(root->data); PrintPreorder(root->left); PrintPreorder(root->right); endif; Void PrintPostorder (root) if root != null PrintPostorder(root->left); PrintPostorder(root->right); print(root->data); endif; Void PrintPostorder (root) if root != null PrintPostorder(root->left); PrintPostorder(root->right); print(root->data); endif;

Binary Search 3-ways comparisons int binarySearch(vector a[], int x){ int low = 0; int high = a.size() – 1; int mid; while(low <= high) { mid = (low + high) / 2; if(a[mid] < x) low = mid + 1; else if( a[mid] > x) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 }//binary search using three-ways comparisons

25 Some more examples: Factorials Binary Search template int binarySearch( const vector & a, const Comparable & x, int low, int high ) { if( low > high ) return NOT_FOUND; int mid = ( low + high ) / 2; if( a[ mid ] < x ) return binarySearch( a, x, mid + 1, high ); else if( x < a[ mid ] ) return binarySearch( a, x, low, mid - 1 ); else return mid; }

26 Divide and Conquer Given an instance of the problem to be solved, split this into several, smaller, sub-instances (of the same problem) independently solve each of the sub-instances and then combine the sub- instance solutions so as to yield a solution for the original instance.

27 Recursion or Iteration? EFFICIENCYCLARITY