CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Introduction to Recursion and Recursive Algorithms
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
Recursion CSC 220: Data Structure Winter Introduction A programming technique in which a function calls itself. One of the most effective techniques.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion. Binary search example postponed to end of lecture.
Chapter 17 Recursion. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Mathematical Definition: RunningSum(1)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
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. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 15 Recursive Algorithms.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Data Structures ساختمان داده ها مظفر بگ محمدی دانشکده فنی دانشگاه ایلام.
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.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Recursion.
CMSC 2021 Recursion Recursive Definition – one that defines something in terms of itself Recursion – A technique that allows us to break down a problem.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
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.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
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.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
CSC 211 Data Structures Lecture 13
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.
Data Structure Introduction.
Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of the problem.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
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 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Chapter 15 Recursion.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
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 17 Recursion.
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Algorithm design and Analysis
Recursion Data Structures.
Module 1-10: Recursion.
ITEC324 Principle of CS III
Presentation transcript:

CSC 221: Recursion

Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of the problem Requires terminating condition: Case for which recursion is no longer needed

Recursion: Induction Basis Mathematical Induction: –Prove that statement is true for first n values, given that it is true for first n-1 values –Prove that the statement is true for a base case.

Recursion: Mathematical Induction Sum of first N positive integers is (N*(N+1)) / 2 Base Case: –1 st positive integer 1 (1 * (1+1)) / 2=>(1*2)/2=> 2/2 => 1 Inductive Case: Assume true for n-1 –Sum(1..N-1) = ((N-1) * (N-1+1)) / 2 => ((N-1) * (N)) / 2) = (N 2 –N)/2 –Adding N = (N 2 –N)/2 + N = (N 2 -N)/2 + 2N/2 = (N 2 + N)/2=> (N * (N+1)) / 2

Factorial Recursion Factorial:n! = n * (n-1)! –Base Case => 0! = 1 –Smaller problem => Solving (n-1)! Implementation: long factorial(long inputValue) { if (inputValue == 0) return 1; else return inputValue * factorial(inputValue - 1); }

Searching We want to find whether or not an input value is in a sorted list: 8 in [1, 2, 8, 10, 15, 32, 63, 64]? 33 in [1, 2, 8, 10, 15, 32, 63, 64]?

Searching int index = 0; while (index < listSize) { if (list[index] == input) return index; index++; } return –1;

Searching Better method: –Number of operations to find input if in the list: Dependent on position in list 1 operation to size of list –Number of operations to find input if not in the list: Size of list

Searching Better method? Use fact that we know the list is sorted Cut what we have to search in half each time –Compare input to middle –If input greater than middle, our value has be in the elements on the right side of the middle element –If input less than middle, our value has to be in the elements on the left side of the middle element –If input equals middle, we found the element.

Searching: Binary Search for (int left = 0, right = n –1; left <= right;) { middle =(left + right) / 2; if (input == list[middle]) return middle; else if (input < list[middle]) right = middle – 1; else left = middle + 1; } return – 1;

Searching: Binary Search 8 in [1, 2, 8, 10, 15, 32, 63, 64]? 1 st iteration: Left = 0, Right = 7, Middle = 3, List[Middle] = 10 Check 8 == 10 => No, 8 < 10 2 nd iteration: Left = 0, Right = 2, Middle = 1, List[Middle] = 2 Check 8 == 2 => No, 8 > 2 3 rd iteration: Left = 2, Right = 2, Middle = 2 Check 8 == 8 => Yes, Found It!

Searching: Binary Search Binary Search Method: –Number of operations to find input if in the list: Dependent on position in list 1 operation if middle Log 2 n operations maximum –Number of operations to find input if not in the list: Log 2 n operations maximum

Recursive Binary Search Two requirements for recursion: –Same algorithm, smaller problem –Termination condition Binary search? –Search in half of previous array –Stop when down to one element

Recursive Binary Search int BinarySearch(int *list, const int input, const int left, const int right) { if (left < right) { middle =(left + right) / 2; if (input == list[middle]) return middle; else if (input < list[middle]) return BinarySearch(list, input, left, middle-1); else return BinarySearch(list,input,middle+1,right); } return – 1; }

While vs Recursion While and Recursion are essentially interchangeable Considerations: –Efficiency –Simplification of programming –Readability/Understandability While they are equivalent, there is not always an obvious while implementation of some functions that are easily implemented with recursion

Fibonacci Computation Fibonacci Sequence: –1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, … –Simple definition: –Fib[0] = 1 –Fib[1] = 1 –Fib[N] = Fib(N-1) + Fib(N-2)

Recursive Fibonacci int fibonacci(int input) { if ((input == 0) || (input == 1)) return 1; else return (fibonacci(input-1) + fibonacci(input-2)); }

Iterative Fibonacci int fibonacci(int input) { int first = 1; int second = 1; int temp; for (int k = 0; k < input; k++) { temp = first; first = second; second = temp + second; } return first; }

Efficiency of Recursion Recursion can sometimes be slower than iterative code Two main reasons: –Program stack usage –Result generation

Types of Recursion Linear Recursion: –1 recursive call per function –Factorial, Binary Search examples Tree Recursion: –2 or more recursive calls per function –Fibonacci Example

Efficiency of Recursion Stack Usage: –When a function is called by a program, that function is placed on the program call stack: main() getData() readFile() Returns file data to be used in getData() Returns formatted data to be printed in main()

Efficiency of Recursion Every stack entry maintains information about the function: –Where to return to when the function completes –Storage for local variables –Pointers or copies of arguments passed in

Efficiency of Recursion When using recursive functions, every recursive call is added to the stack and it grows fast: Fibonacci (5) –Fibonacci (5) = Fibonacci (4) + Fibonacci(3) –Fibonacci (1) in the computation of Fibonacci(4) is the first time we don’t have to call the function again. Stack entries take up space and require extra processing main() Fibonacci(5) Fibonacci(4) Fibonacci(3) Fibonacci(2) Fibonacci(1)

Efficiency of Recursion Another Reason for Slowdowns [Tree Recursion] –Traditional Recursion doesn’t save answers as it executes –Fib(5) = Fib(4) + Fib(3) = Fib(3) + Fib(2) + Fib(3) = Fib(2) + Fib(1) + Fib(2) + Fib(3) = Fib(1) + Fib(0) + Fib(1) + Fib(2) + Fib(3) = Fib(1) + Fib(0) + Fib(1) + Fib(1) + Fib(0) + Fib(3) = Fib(1) + Fib(0) + Fib(1) + Fib(1) + Fib(0) + Fib(2) + Fib(1) = Fib(1) + Fib(0) + Fib(1) + Fib(1) + Fib(0) + Fib(1) + Fib(0) + Fib(1) Solution: Dynamic programming – saving answers as you go and reusing them

Dynamic Programming Fibonacci Problem –We know the upper bound we are solving for –Ie Fibonacci (60) = 60 different answers –Generate an array 60 long and initalize to –1 –Everytime we find a solution, fill it in in the array –Next time we look for a solution, if the value in the array for the factorial we need is not –1, use the value present.

Fibonacci Examples Three implementations of fibonacci: –Naive recursion implementation (worst performance) –Dynamic Programming recursion implementation (better) –Iterative implementation (best)

Recursive Datastructures Recursion is useful when datastructure is inherently recursive Unix directory hierarchy is a tree datastructure / /home /var/usr /home/turketwh /home/turketwh/CS112/home/turketwh/CS221

Directory Traversal ls –R in Unix=> recursive list Potential implementation? –listDirectory(directory baseDirectory) { file[] files = getFiles(); int fileCount = getFileCount(); for (int i = 0; i < fileCount; i++) { if (files[I].type == “dir”) listDirectory(file); else listFile(file); }

Recursive Datastructures Will see a lot of datastructures that are recursive –Lists [Atom + Smaller List] –Trees [ Root + Subtrees] Have simple implementations because their functionality can be defined recursively.

Towers of Hanoi Not allowed General Problem: For any number of boxes, move boxes from start peg to destination peg. Can never place a bigger box on top of a smaller box.

Towers Of Hanoi Recursive? 1 box from peg 1 to peg 3 1 _ __ _ 1 2 boxes from peg 1 to peg __2 1 __ 1 2_ _ 2

Towers Of Hanoi 3 boxes from peg 1 to peg _ _ 3 _ __ _ 3_ _ 3 For n boxes, 1)Solve the n-1 problem from start to the temp peg 2)Move the nth box to the destination peg 3)Solve the n-1 problem from the temp peg to the destination peg

Space Efficiency Factorial(N) –Factorial(4) => =4*Factorial(3) =4 * 3 * Factorial(2) = 4 * 3 * 2 * Factorial(1) =4 * 3 * 2 * 1 –Has to make a maximum of N calls before base case is reached and function returns. –N activation records will be placed on the stack. Since the size of the input is N, this function requires memory that is linearly related to the size of the input

Space Efficiency Fibonacci(N) – Standard recursive implementation –Fib(5) = Fib(4) + Fib(3) –For Fib(4), also puts Fib(3),Fib(2), Fib(1) on stack –For Fib(3), also puts Fib(2), Fib(1) on stack –Fib(4), Fib(3) compute separately Space Efficiency considers maximum amount required at one time – Fib(4) branch in this case Requires memory linearly related to input size

Tail Recursion Tail Recursion: When the results of a recursive call are not used after the recursive call within the calling function. long factorialHelper(long startValue, long inputValue) { if (startValue == 0) return 1; else if (inputValue == 1) return startValue; else return factorialHelper(startValue * (inputValue-1), inputValue - 1); } long factorial(long inputValue) { return factorialHelp(inputValue, inputValue); }

Space Efficiency: Tail Recursion Since the results of the recursive call aren’t needed for other computations, the stack frames aren’t needed to hold partial results. With each call, new frame replaces old frame. –The compiler handles these optimizations. Requires constant amount of memory and is not dependent on the size of the input.

Space Efficiency: Tail Recursion main() factorialHelper(3,3) factorialHelper(6,2) factorialHelper(6,1) Without Optimization Return 6 Call With Optimization looks like this: (each call of fh is done when it calls the next, so take it off the stack) main() factorialHelper(3,3) main() factorialHelper(6,2) main() factorialHelper(6,1)

Recursion Key Ideas –Decomposition: Solve smaller problem(s) and combine answers –Tends to allow for very simple writing of code –Used naively, may lead to Significant stack usage Repeated computations –Have touched on methodologies to help work around those issues: Tail Recursion Storing answers (‘dynamic programming’) –Suggests a new technique may be needed for computing number of operations for algorithm to complete