COMPSCI 105 S2 2014 Principles of Computer Science Recursion 1.

Slides:



Advertisements
Similar presentations
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Advertisements

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.
© 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.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Ch. 3: Recursion. Recursive Solutions Recursion –An extremely powerful problem-solving technique –Breaks a problem into smaller identical problems –An.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
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-
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.
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.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
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.
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.
© 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 Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Recursion.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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-
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
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 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
Ramamurthy Recursion: The Mirrors B. Ramamurthy CS114A,B.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
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:
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
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.
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 Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
CSC 205 Programming II Lecture 8 Recursion.
Recursion CENG 707.
Recursion CENG 707.
Recursion: The Mirrors
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion: The Mirrors
Recursion.
Recursion: The Mirrors
Recursion: The Mirrors
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion: The Mirrors
Presentation transcript:

COMPSCI 105 S Principles of Computer Science Recursion 1

Agenda  Agenda  What is recursion?  Recursive solutions:  Examples  The Factorial of N  Box Trace Example  Write a String Backward  Tail Recursion  Textbook:  Problem Solving with Algorithms and Data Structures  Chapter 4 – Recursion 20COMPSCI1052

20.1 Introduction Definitions  Problem Domain:  The space consisting of all elements for which the problem is solved  Examples: An array of integers, all people in this room, the days of the month, all “All Blacks” rugby games  Problem Size:  The number of elements of the problem domain  Examples: An array with N elements, the number of people in this room, a list of N cities, the number of games played by the “All Blacks”  GOAL: Design algorithms to solve problems! 20COMPSCI1053

20.1 Introduction Iterative algorithm  Algorithm which solves a problem by applying a function to each element of the problem domain.  Example: Find the tallest person in a group of N>0 students Student FindTallestStudent(Group_of_students){ TallestStudent = Take any student from group; Repeat until nobody left Take next student from group If student is taller than TallestStudent then TallestStudent = student; Return TallestStudent; } Student FindTallestStudent(Group_of_students){ TallestStudent = Take any student from group; Repeat until nobody left Take next student from group If student is taller than TallestStudent then TallestStudent = student; Return TallestStudent; } TallestStudent 20COMPSCI1054

20.1 Introduction Recursion  Recursion is a powerful problem solving technique where a problem is broken into smaller and smaller identical versions of itself until a smaller version is small enough that it has an obvious solution.  Note:  Complex problems can have simple recursive solutions  It is an alternative to iteration (involves loops)  BUT: Some recursion solutions are inefficient and impractical! P1P1 P2P2 … PNPN A base case is a special case whose solution is known 20COMPSCI1055

20.1 Introduction Recursion  Recursion involves a function calling itself.  Example: Find the tallest person in a group of N>0 students FindTallestStudent(Group of students){ If only one student in group return this student else StudentA = Take any student from group StudentB = FindTallestStudent(Remaining Group) return the taller person of StudentA and studentB; } FindTallestStudent(Group of students){ If only one student in group return this student else StudentA = Take any student from group StudentB = FindTallestStudent(Remaining Group) return the taller person of StudentA and studentB; } FindTallestStudent([162,155,169,157]) TallestStudent FindTallestStudent([155,169,157]) FindTallestStudent([169,157]) FindTallestStudent([157]) Taller 20COMPSCI1056

20.2 Recursion Recursive Solutions  Properties of a recursive solution  A recursive method calls itself  Each recursive call solves an identical, but smaller, problem  A test for the base case enables the recursive calls to stop  Base case: a known case in a recursive definition  Eventually, one of the smaller problems must be the base case (problem not allowed to become smaller than base case) 20COMPSCI1057

20.2 Recursion Four Questions  Four questions for constructing recursive solutions  How can you define the problem in terms of a smaller problem of the same type?  How does each recursive call diminish the size of the problem?  What instance of the problem can serve as the base case?  As the problem size diminishes, will you reach this base case? 20COMPSCI1058

20.3 Examples Example – Calculate the Sum  Get the sum by taking the first number + the sum of the rest of the list recursive_sum([2,1,5,6]) 2 + recursive_sum([1,5,6]) 1 + recursive_sum([5,6]) 5 + recursive_sum([6]) 6 + recursive_sum([]) def recursive_sum(num_list): if len(num_list) == 0: return 0 return num_list[0] + recursive_sum(num_list[1:]) def recursive_sum(num_list): if len(num_list) == 0: return 0 return num_list[0] + recursive_sum(num_list[1:]) 20COMPSCI1059

20.3 Examples Example – Bad Recursion 1  Problem:  Compute the sum of all integers from 1 to n def bad_sum(n): return n + bad_sum(n-1) def bad_sum(n): return n + bad_sum(n-1) No base case!!! 20COMPSCI10510

20.3 Examples Example – Bad Recursion 2  Problem:  If n is odd compute the sum of all odd integers from 1 to n, if it is even compute sum of all even integers def bad_sum2(n): if n == 0: return 0 return n + bad_sum2(n-2) def bad_sum2(n): if n == 0: return 0 return n + bad_sum2(n-2) Base case never reached if n is odd!! 20COMPSCI10511

20.4 The Factorial of n Definition  Problem  Compute the factorial of an integer n >=0  An iterative definition of factorial(n)  If n = 0, factorial(0) = 1  If n > 0, factorial(n) = n * (n-1) * (n-2) * … * 1  Examples:  4! = 4 * 3 * 2 * 1 = 24  7! = 7 * 6 * 5 * 4 * 3 * 2 * 1 = 5040 def factorial(n): result = 1 for i in range(n, 1, -1): result = result * i return result def factorial(n): result = 1 for i in range(n, 1, -1): result = result * i return result 20COMPSCI10512

20.4 The Factorial of n The Factorial of n  A recurrence relation  A mathematical formula that generates the terms in a sequence from previous terms factorial(n) = n * [(n-1) * (n-2) * … * 1] = n * factorial(n-1)  A recursive definition of factorial(n) factorial(n) = 1 if n = 0 n * factorial(n-1) if n > 0 def fact(n): if n <= 0: return 1 return n * fact(n-1) def fact(n): if n <= 0: return 1 return n * fact(n-1) 20COMPSCI10513

20.4 The Factorial of n Four Criteria  fact satisfies the four criteria of a recursive solution  fact calls itself.  At each recursive call, the integer whose factorial to be computed is diminished by 1.  The methods handles the factorial 0 differently from all other factorials, where fact(0) is 1. Thus the base case occurs when n is 0.  Given that n is non-negative, item 2 of this assures that the computation will always reach the base case. def fact(n): if n <= 0: return 1 return n * fact(n-1) def fact(n): if n <= 0: return 1 return n * fact(n-1) 20COMPSCI10514

20.4 The Factorial of n Call Tree  A systematic way to trace the actions of a recursive method  Create a new box for each recursive method call  Describe how return value is computed  Provide link to box (or boxes) for recursive method calls within the current method call  Each box corresponds to an activation record  Contains a method’s local environment at the time of and as a result of the call to the method The local environment contains: Value of argument, local variables, return value, address of calling method, …etc 20COMPSCI10515

20.5 Box Trace Box Trace Example  A method’s local environment includes:  The method’s local variables  A copy of the actual value arguments  A return address in the calling routine  The value of the method itself Fact(3) 20COMPSCI10516

20.5 Box Trace Box Trace on Factorial n = 3 return ? 3 * fact(2) n = 3 return ? 3 * fact(2) fact(3) 1 n = 2 return? 2 * fact(1) n = 2 return? 2 * fact(1) fact(2) n = 1 return? 1 * fact(0) n = 1 return? 1 * fact(0) fact(1) n = 0 return? 1 n = 0 return? 1 fact(0) COMPSCI10517

20.6 Writing a String Backward Definition  Problem  Given a string of characters, write it in reverse order  Recursive solution  Each recursive step of the solution diminishes by 1 the length of the string to be written backward  Base case  Write the empty string backward  Examples: print(writeBackward("cat")) print(writeBackward2("cat")) tac 20COMPSCI10518

20.6 Writing a String Backward Two approaches  writeBackward(s)  writeBackward2(s) if the string s is empty: Do nothing – base case else: writeBackward2(s minus its first char) write the first char of s if the string s is empty: Do nothing – base case else: writeBackward2(s minus its first char) write the first char of s if the string s is empty: Do nothing – base case else: write the last char of s writeBackward(s minus its last char) if the string s is empty: Do nothing – base case else: write the last char of s writeBackward(s minus its last char) call method recursively for the string minus the last character call method recursively for the string minus the first character 20COMPSCI10519

20.6 Writing a String Backward writeBackward(s) s = “cat”, write t writeBackward("ca") s = “cat”, write t writeBackward("ca") writeBackward("cat") s = “ca”, write a writeBackward("c") s = “ca”, write a writeBackward("c") writeBackward("ca") s = “c”, write c writeBackward("") s = “c”, write c writeBackward("") writeBackward("c") s = “” return s = “” return writeBackward("c") t ta tac 20COMPSCI10520

20.6 Writing a String Backward writeBackward2(s) s = “cat” writeBackward2("at") s = “cat” writeBackward2("at") writeBackward2("cat") s = “ca” writeBackward2(“t") s = “ca” writeBackward2(“t") writeBackward2("at") s = “c” writeBackward2("") s = “c” writeBackward2("") writeBackward2("t") s = “” return s = “” return writeBackward2("") Write a Write t “” Write c 20COMPSCI10521

20.7 Tail Recursion Tail Recursion  Tail Recursion occurs when the last action performed by a recursive method is a recursive call.  Tail recursion can be done more efficiently using iteration. def recursive_sum(num_list): if len(num_list) == 0: return 0 return num_list[0] + recursive_sum(num_list[1:]) def recursive_sum(num_list): if len(num_list) == 0: return 0 return num_list[0] + recursive_sum(num_list[1:]) def get_sum(num_list): sum_numbers = 0 for num in num_list: sum_numbers += sum return sum_numbers def get_sum(num_list): sum_numbers = 0 for num in num_list: sum_numbers += sum return sum_numbers 20COMPSCI10522

Summary 07COMPSCI10523  A recursive algorithm passes the buck repeatedly to the same function  Recursive algorithms are well-suited for solving problems in domains that exhibit recursive patterns  Recursive strategies can be used to simplify complex solutions to difficult problems

Exercise  We can determine how many digits a positive integer has by repeatedly dividing by 10 (without keeping the remainder) until the number is less than 10, consisting of only 1 digit. We add 1 to this value for each time we divided by 10. Here is the recursive algorithm: 1. If n < 10 return Otherwise, return 1 + the number of digits in n/10 (ignoring the fractional part).  Implement this recursive algorithm in Python and test it using the values 15, 105, and COMPSCI10524