CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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

Lecture Objectives  Learn how to think recursively  Trace a recursive method  Prove a recursive method is correct  Write recursive algorithms and methods.
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Recursion Chapter 5.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 19 Recursion.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 19 Recursion.
Recursion Chapter 7.
1 CSE1301 Computer Programming Lecture 27 Recursion (Part 1)
Discrete Mathematics Recursion and Sequences
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Recursion.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Recursion Chapter 5.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
© 2006 Pearson Addison-Wesley. All rights reserved 3-1 Chapter 3 Recursion: The Mirrors.
RECURSION Chapter 5. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Problem Solving and Algorithms
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.
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.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Lecture#16 Discrete Mathematics. Recursion Now, 1 is an odd positive integer by the definition base. With k = 1, = 3, so 3 is an odd positive integer.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
Agenda Lecture Content:  Recurrence Relations  Solving Recurrence Relations  Iteration  Linear homogenous recurrence relation of order k with constant.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion.
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
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.
Starting Out with C++, 3 rd Edition Chapter 19 Recursion.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
RECURSIONL27-L28. To learn and the following concepts To design a recursive algorithm To solve problems using recursion To understand the relationship.
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! =
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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.
Chapter 18 Recursion CS1: Java Programming Colorado State University
CS212: Data Structures and Algorithms
Recursive Algorithms Section 5.4.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
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.
Recursion DRILL: Please take out your notes on Recursion
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Chapter 15 Recursion.
Chapter 19 Recursion.
Recursion Chapter 5.
7.2 Recursive Definitions of Mathematical Formulas
Intro to Recursion.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Algorithm design and Analysis
Recursion Taken from notes by Dr. Neil Moore
Recursive Algorithms 1 Building a Ruler: drawRuler()
7.2 Recursive Definitions of Mathematical Formulas
Presentation transcript:

CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING

The problem with rabbits… A man puts a pair of rabbits in a place surrounded on all sides by a wall. How many pairs of rabbits can be produced from that pair in a year if it is supposed that every month each pair begets a new pair which from the second month on becomes productive?

Fibonacci’s rabbits.. Fibonacci numbers were invented to model the growth of a rabbit colony fib 1 = 1 fib 2 = 1 fib n = fib n-1 + fib n-2 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

Recursive Thinking CS340 4

Recursive Thinking Recursion reduces a problem into one or more simpler versions of itself CS340 5

Recursive Thinking (cont.) CS340 6

Recursive Thinking (cont.) Recursion of Process Nested Dreams A child couldn't sleep, so her mother told a story about a little frog, who couldn't sleep, so the frog's mother told a story about a little bear, who couldn't sleep, so bear's mother told a story about a little weasel...who fell asleep....and the little bear fell asleep;...and the little frog fell asleep;...and the child fell asleep. CS340 7

Steps to Design a Recursive Algorithm  Base case:  for a small value of n, it can be solved directly  Recursive case(s)  Smaller versions of the same problem  Algorithmic steps:  Identify the base case and provide a solution to it  Reduce the problem to smaller versions of itself  Move towards the base case using smaller versions CS340 8

Finding… a needle in a haystack This is a classical computational thinking problem Write a function: find_needle Inputs of your function: the number that you are looking for and an array of numbers Outputs of your function: the index of the array where the number was found, OR a message if the number is not in the array.

Recursive Thinking (cont.) Consider searching for a target value in an array With elements sorted in increasing order Compare the target to the middle element If the middle element does not match the target search either the elements before the middle element or the elements after the middle element Instead of searching n elements, we search n/2 elements CS340 10

Recursive Thinking (cont.) Recursive Algorithm to Search an Array if the array is empty return -1 as the search result else if the middle element matches the target return the subscript of the middle element as the result else if the target is less than the middle element recursively search the array elements before the middle element and return the result else recursively search the array elements after the middle element and return the result CS340 11

Recursive Algorithm for Finding the Length of a String if the string is empty (has no characters) the length is 0 else the length is 1 plus the length of the string that excludes the first character CS340 12

Recursive Definitions of Mathematical Formulas CS340 13

Recursive Definitions of Mathematical Formulas Mathematicians often use recursive definitions of formulas Examples include: factorials powers greatest common divisors (gcd) CS340 14

Factorial of n: n! The factorial of n, or n! is defined as follows: 0! = 1 n! = n x (n -1)! (n > 0) The base case: n equal to 0 The second formula is a recursive definition CS340 15

Factorial of n: n! (cont.)  The recursive definition can be expressed by the following algorithm: if n equals 0 n! is 1 else n! = n x (n – 1)!  The last step can be implemented as: return n * factorial(n – 1); CS340 16

Infinite Recursion and Stack Overflow  Call factorial with a negative argument, what will happen? CS StackOverflowException

Resources Lecture slides CS112, Ellen Hildreth,

QUESTIONS??