Recursion Ellen Walker CPSC 201 Data Structures Hiram College.

Slides:



Advertisements
Similar presentations
3/25/2017 Chapter 16 Recursion.
Advertisements

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.
COSC 2006 Data Structures I Recursion III
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. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Recursion Chapter 8. 2 Chapter Contents What Is Recursion? Tracing a Recursive Method Recursive Methods That Return a Value Recursively Processing an.
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.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
COMPSCI 105 S Principles of Computer Science Recursion 3.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 14: Recursion Starting Out with C++ Early Objects
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Recursion: The Mirrors Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Recursion.
© 2006 Pearson Addison-Wesley. All rights reserved 3-1 Chapter 3 Recursion: The Mirrors.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
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 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
COSC 2006 Data Structures I Recursion II
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Chapter 12 Recursion, Complexity, and Searching and Sorting
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
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.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Ramamurthy Recursion: The Mirrors B. Ramamurthy CS114A,B.
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.
Recursion. What is Recursion? Method of solving problems Alternative to iteration All recursive solutions can be implemented iteratively Characteristic...
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Recursion CENG 707.
Chapter 19: Recursion.
Recursion CENG 707.
Chapter 15 Recursion.
Recursion: The Mirrors
Recursion: The Mirrors
Chapter 15 Recursion.
Announcements Final Exam on August 17th Wednesday at 16:00.
Chapter 14: Recursion Starting Out with C++ Early Objects
Algorithm design and Analysis
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion Data Structures.
Recursion: The Mirrors
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion: The Mirrors
Presentation transcript:

Recursion Ellen Walker CPSC 201 Data Structures Hiram College

Divide & Conquer To solve a big problem, break it down into smaller problems Examples: –Find the largest element in a list by comparing pairs of elements –Binary search: find an element in a list by deciding which half of the list it is in, and sorting the smaller half-list

Recursion Every smaller problem (except the smallest one) is an instance of the larger problem Examples: –Binary Search –Factorial –Fibonacci number Implementation: function that calls itself

Sequential Search as Recursion //Find (the first instance of) element in list If the list is empty, return “not found” Else if the first element of the list is the one you’re looking for, return it Else search in the smaller list starting from the second element of the list

Sequential Search in Java //Check if value is in array of integers //Note: end is one value beyond the last in list boolean search1 (int value, int[] list, int st, int end){ if (st==end) return false; // empty list if (list[st]==value) return true; return search1(value, list, st+1, end); }

Sequential Search as Divide & Conquer Divide the problem into two smaller problems: 1.Is the value the first element of the list? 2.Find the value in the smaller list starting at the second element. – Disadvantage: each “smaller” problem isn’t very much smaller

Binary Search Is the element in the first or second half of the list? (For a sorted list, this is a single comparison to the middle element) Search for the element in the appropriate half of the list

Binary Search in Java boolean search2(int val, int[] list, int st, int end){ if (st==end) return false; //empty list int middle = (end-st)/2; if (list[middle]==val) return true; if (val < list[middle]) return search2(val,list,st,middle)); else return search2(val, list, middle+1, end); }

Binary vs. Sequential Sequential search: each list search is 1 smaller Binary search: each list search is half as big Binary search is faster, but… Binary search requires a sorted list

Aspects of A Recursive Solution Base case (or basis or degenerate case) –List is empty, List has 1 element Recursive call –Call to search1 or search2 Recursive call must be “closer” to the base case –The list is shorter in the recursive call

4 Questions for Recursive Algorithm Design 1. How can you define the problem in terms of a smaller problem of the same type? “single step” + “smaller problem” 2. How does each recursive call diminish the size of the problem? 3. What instance of the problem can serve as the base case? 4. As the problem size diminishes, will you reach this base case?

4 Answers for Factorial 1. How can you define the problem… Fact(N) is N*Fact(N-1) 2. How does each … call diminish… N-1 is smaller than N 3. What … can serve as the base case? Fact(0) is 1 4. …will you reach the base case? Yes, as long as you start with N≥0 (a precondition)

Factorial in Java Note: this is a “valued” recursive function The final result is constructed from the results of recursive calls int Fact(int N){ assert (N>=0); //make sure of precondition if (N==0) return 1; else return N*Fact(N-1); }

Activation record Information recorded for each function call –Values of the parameters –Function’s local variables –Space for result –Where to return to (important if multiple calls!) For each function call, create a new activation record For each return, erase the activation record

Writing a string backward Base case: Define the problem in terms of a smaller one? How does each case diminish…? Guaranteed to reach the base case?

Multiple Calls in One Function Fibonacci sequence (counting rabbits) Rabbit(N) = Rabbit(N-1)+Rabbit(N-2) Rabbit(1) = Rabbit(2) = 1 (Rabbit is not defined for values < 1)

A Counting Problem How many ways to visit k of n planets? –Pick some planet (p) –There are count(n-1, k) ways to visit k planets without visiting p –There are count(n-1, k-1) ways to visit k planets with visiting p –Therefore: count(n, k) = count(n-1,k)+count(n-1,k-1) –Base case?

Impractical Recursions Fibonacci: makes as many calls as rabbits! –Solution: compute it iteratively (save intermediate steps) Counting: similar to Fibonacci –Solution: use mathematics to come up with a closed form –Count(n,k) = n! / (k! (n-k)!)

Impractical but unavoidable Towers of Hanoi Move N disks from one tower to another, never putting a larger disk on a smaller disk Solution in 3 steps: –Recursively move N-1 disks to the extra tower –Move the Nth disk to the destination tower –Recursively move N-1 disks to the destination tower Solution time: O(2 N )

K’th smallest element Pick a “partition” element & put smaller elements before, and larger elements after –Remember quicksort? If “partition” element is k’th, it is the answer Otherwise, recurse over the half that has the k’th element in it (change k if it’s the second half!) Impossible to predict how long it will take without knowing the exact array!

Recursive Linked List The empty list is a linked list A list consisting of a head item followed by a list of the rest of the items is a linked list Method to –Add at the beginning of the list –Add at the end of the list Recursive method to –Count the elements –See if an element is in the list