RECURSION.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Introduction to Recursion and Recursive Algorithms
Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
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.
Recursion. Binary search example postponed to end of lecture.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Programming with Recursion
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Recursion Gordon College CPS212
Recursion.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
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.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
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.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
1 Chapter 3. Recursion Lecture 6. In functions and data structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
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.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
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! =
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Recursion You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
Recursion Powerful Tool
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Programming with Recursion
CS212: Data Structures and Algorithms
Recursion.
Recursion CENG 707.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion DRILL: Please take out your notes on Recursion
ADT Implementation: Recursion, Algorithm Analysis, and Standard Algorithms Chapter 10 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second.
Data Structures and Algorithms
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Chapter 8: Recursion Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray.
Chapter 15 Recursion.
More on Recursive Methods
Programming with Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion: The Mirrors
Algorithm design and Analysis
Recursion Chapter 11.
Stacks & Recursion.
Chapter 12 Supplement: Recursion with Java 1.5
Basics of Recursion Programming with Recursion
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Recursion: The Mirrors
Yan Shi CS/SE 2630 Lecture Notes
High-Level Programming Language
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
ITEC324 Principle of CS III
Presentation transcript:

RECURSION

What is recursion? a function calls itself direct recursion a function calls its invoker indirect recursion f1 f f2

Recursion is an alternative to iteration (loop) often more "elegant" and concise than a loop sometimes very inefficient recursion should be considered when a problem can be defined in terms of successive smaller problems of the same type, i.e. recursively eventually the problem gets small enough that the answer is known (base case) reducing the problem size leads to the base case sometimes the "work" of the algorithm is done after the base case is reached

Outline of a Recursive Function if (answer is known) provide the answer else make a recursive call to solve a smaller version of the same problem base case recursive case - “leap of faith”

Factorial (n) - recursive Factorial (n) = n * Factorial (n-1) for n > 0 Factorial (0) = 1 int RecFact (int n) { if (n > 0) return n * RecFact (n-1); else return 1; } base case

How Recursion Works a recursive function call is handled like any other function call each recursive call has an activation record on the stack stores values of parameters and local variables when base case is reached return is made to previous call the recursion “unwinds”

Recursive Call Tree RecFact (4) 24 int RecFact (int n) 4 { 3 2 1 6 24 int RecFact (int n) { if (n > 0) return n * RecFact (n-1); else return 1; }

Factorial (n) - iterative Factorial (n) = n * (n-1) * (n-2) * ... * 1 for n > 0 Factorial (0) = 1 int IterFact (int n) { int fact =1; for (int i = 1; i <= n; i++) fact = fact * i; return fact; }

Another Recursive Definition Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, …. 1 for n == 1 fib(n) = 1 for n == 2 fib(n-2) + fib(n-1) for n>2

Tracing fib(6) 5 6 4 4 3 3 2 3 2 2 1 2 1 2 1

When to use recursion? if recursive and iterative algorithms are of similar efficiency --> iteration if the problem is inherently recursive and a recursive algorithm is less complex to write than an iterative algorithm --> recursion recursion often simpler than iteration when it is not tail recursion or there are multiple recursive calls recursion very inefficient when values are recomputed

Euclid'sAlgorithm Finds the greatest common divisor of two nonnegative integers that are not both 0 Recursive definition of gcd algorithm gcd (a, b) = a (if b is 0) gcd (a, b) = gcd (b, a % b) (if b != 0) Write a recursive gcd function prototype? definition?

Ackermann's function A(0, n) = n + 1 A(m+1, 0) = A(m, 1) A(m+1, n+1) = A(m, A(m+1, n))

Iterative gcd int gcd (int a, int b) { int temp; while (b != 0) temp = b; b = a % b; a = temp; } return a;

iterative binary search bool binarySearch (const int a[ ], int numItems, int item) { bool found = false; int first = 0; int last = numItems – 1; while (first <= last && ! found) { mid = (first + last) / 2; if (item == a[mid]) found = true; else if (item > a[mid]) first = mid +1; else last = mid – 1; } return found;

bool binarySearch(const int a[ ], int first, int last, int item) { if (first > last) return false; // base case 1 (unsuccessful search) else int mid = (first + last) / 2; if (item == a[mid]) return true; // base case 2 (successful search) else if (item > a[mid]) return binarySearch(a, mid+1, last, item); // X return binarySearch(a, first, mid-1, item); // Y }

a = [1 5 9 12 15 21 29 31] binarySearch(a, 0, 7, 6) binarySearch(a, 0, 7, 9) 2, 1, 0, 7, 3 0, 2, 1 2, 2, 2 X Y false 0, 7, 3 0, 2, 1 2, 2, 2 X Y true base case 2 base case 1

What Does a Compiler Do? Lexical analysis Parsing Generate object code divide a stream of characters into a stream of tokens total = cost + 0.08 * cost; if ( ( cond1 && ! cond2 ) ) Parsing do the tokens form a valid program, i.e. follow the syntax rules? Generate object code

BNF (Backus-Naur form) a language used to define the syntax rules of a programming language consists of productions – rules for forming some construct of the language meta-terms – symbols of BNF terminals – appear as shown non-terminals – syntax defined by another production

Syntax Rules for a simplified boolean expression meta-terms are in red bexpr -> bterm || bterm | bterm bterm -> bfactor && bfactor | bfactor bfactor -> !bfactor|(bexpr)|true|false|ident ident -> alpha {alpha|digit|_} alpha -> a .. z | A .. Z digit -> 0 .. 9 meta-terms are in red terminals are in blue non-terminals are in black

bexpr -> bterm || bterm | bterm does a sequence of tokens form a valid bexpr? if (there is a valid bterm) if (nextToken is ||) if (there is a valid bterm) return true else return false else return true else return false

Comparing Algorithms and ADT Data Structures big Oh Notation Comparing Algorithms and ADT Data Structures

Algorithm Efficiency a measure of the amount of resources consumed in solving a problem of size n time space benchmarking – code the algorithm, run it with some specific input and measure time taken better for measuring and comparing the performance of processors than for measuring and comparing the performance of algorithms Big Oh (asymptotic analysis) provides a formula that associates n, the problem size, with t, the processing time required to solve the problem