190 likes | 287 Views
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.
E N D
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 fib1 = 1 fib2 = 1 fibn = fibn-1 + fibn-2 • 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …
CS340 Recursive Thinking
CS340 Recursive Thinking • Recursion reduces a problem into one or more simpler versions of itself
CS340 Recursive Thinking (cont.)
CS340 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 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
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.
CS340 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 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 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 Recursive Definitions of Mathematical Formulas
CS340 Recursive Definitions of Mathematical Formulas • Mathematicians often use recursive definitions of formulas • Examples include: • factorials • powers • greatest common divisors (gcd)
CS340 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 Factorial of n: n! (cont.) • The recursive definition can be expressed by the following algorithm: ifn equals 0 n! is 1 else n! = n x (n – 1)! • The last step can be implemented as: return n * factorial(n – 1);
CS340 Infinite Recursion and Stack Overflow • Call factorialwith a negative argument, what will happen? StackOverflowException
Resources • Lecture slides CS112, Ellen Hildreth, http://cs.wellesley.edu/~cs112/