Recursion Think ESCHER Recurrence Relation A rule which

  • Slides: 14
Download presentation
Recursion Think ESCHER

Recursion Think ESCHER

Recurrence Relation A rule which defines a sequence value in terms of one or

Recurrence Relation A rule which defines a sequence value in terms of one or more earlier values Ø Example S(1) = 2 S(n) = 2 * S(n-1) for n >= 2 Ø What are the values which follow 2 in the sequence? Ø

Some Practise Examples Ø The sequence T is defined recursively as follows l l

Some Practise Examples Ø The sequence T is defined recursively as follows l l T(1) = 1 T(n) = T(n-1) + 3 for n>=2 Ø What are the first 7 values in the sequence T?

A famous sequence Ø The Fibonacci sequence of numbers was introduced in the 13

A famous sequence Ø The Fibonacci sequence of numbers was introduced in the 13 th century by an Italian merchant and mathematician. l l l F(1) = 1 F(2) = 1 F(n) = F(n-2) + F(n-1) for n >2 Ø What are the first 8 numbers in the sequence

Recursion Definitions Ø Recursion is a computation using a recurrence relation Ø A function

Recursion Definitions Ø Recursion is a computation using a recurrence relation Ø A function is recursive (or “uses recursion” ) if it calls itself.

Bernstein nerd humor Ø There are two kinds of people in the world, those

Bernstein nerd humor Ø There are two kinds of people in the world, those who divide the world into two kinds of people and those who do not.

Trees The following recursive definition defines a “line-drawing tree”: A line-drawing tree of complexity

Trees The following recursive definition defines a “line-drawing tree”: A line-drawing tree of complexity 1 is a single line segment 20 pixels long (the trunk). A line-drawing tree of complexity n > 1 is a line segment 20 n pixels long (the trunk), with two line-drawing trees of complexity n-1 branching at 45 degree angles from its end. For example, here, from left to right, are line-drawing trees of complexities 1, 2, and 3:

Defining a function recursively Ø A function is defined recursively if the value of

Defining a function recursively Ø A function is defined recursively if the value of f(0) is given and the value of f(n+1) is given in terms of f(n). Ø Example l l f(0) = 1 f(n+1) = f(n) * f(n+1) Ø What does that function produce?

Programming recursion Ø A method calls itself directly or indirectly (to be explained later).

Programming recursion Ø A method calls itself directly or indirectly (to be explained later). Ø Here’s our function l l f(0) = 1 f(n) = n * f(n-1) for n >0 Ø What would the method look like? Ø What are the first 10 values?

Factorial Method int factorial (int n) { int value; if (n == 0) {

Factorial Method int factorial (int n) { int value; if (n == 0) { value = 1; return value; } else { value = n * factorial (n-1);

What Happens in Recursion? Ø When a method is called, an activation record (or

What Happens in Recursion? Ø When a method is called, an activation record (or invocation record) is created. Ø Method calls/returns happened in a last-infirst-out manner Ø Activation records contain l l Local variables and their values The location in the caller of the method call State of the current method Other things

Algorithm for Selection Sort Selection. Sort (list L; integer j) // recursively sorts the

Algorithm for Selection Sort Selection. Sort (list L; integer j) // recursively sorts the items from 1 to j in list L into increasing order. if j = 1 then sort is complete, write out the sorted list else find the index i of the maximum item in L between 1 and j exchange L[i] and L[j] Selection. Sort(L, j -1) end if end function Selection. Sort

Recursive Binary Search Ø ? ?

Recursive Binary Search Ø ? ?