Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to...

16
Lecture 7 Lecture 7 Recursion is a fundamental Recursion is a fundamental programming technique that can programming technique that can provide an elegant solution to provide an elegant solution to certain kinds of problems certain kinds of problems Today: Today: thinking in a recursive manner thinking in a recursive manner programming in a recursive manner programming in a recursive manner the correct use of recursion the correct use of recursion recursion examples recursion examples

Transcript of Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to...

Page 1: Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.

Lecture 7Lecture 7

Recursion is a fundamental programming Recursion is a fundamental programming technique that can provide an elegant solution to technique that can provide an elegant solution to certain kinds of problemscertain kinds of problems

Today:Today:• thinking in a recursive mannerthinking in a recursive manner• programming in a recursive mannerprogramming in a recursive manner• the correct use of recursionthe correct use of recursion• recursion examplesrecursion examples

Page 2: Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.

2

Recursive ThinkingRecursive Thinking

A A recursive definitionrecursive definition is one which uses the word or concept is one which uses the word or concept being defined in the definition itselfbeing defined in the definition itself

Sometimes it is easier to define a concept in terms of itself, Sometimes it is easier to define a concept in terms of itself, as applied to smaller inputs.as applied to smaller inputs.

Example: the fibonacci numbers are defined as follows:Example: the fibonacci numbers are defined as follows:

f(0) = 0f(0) = 0

f(1) = 1f(1) = 1

f(n) = f(n-1) + f(n-2) for n>1.f(n) = f(n-1) + f(n-2) for n>1.

Page 3: Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.

3

Recursive DefinitionsRecursive Definitions

Consider the following list of numbers:Consider the following list of numbers:

24, 88, 40, 3724, 88, 40, 37

Such a list can be defined asSuch a list can be defined as

A LIST is a: numberA LIST is a: number or a: number comma LISTor a: number comma LIST

That is, a LIST is defined to be a single number, or a That is, a LIST is defined to be a single number, or a number followed by a comma followed by a LISTnumber followed by a comma followed by a LIST

The concept of a LIST is used to define itselfThe concept of a LIST is used to define itself

Page 4: Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.

4

Recursive DefinitionsRecursive Definitions

The recursive part of the LIST definition is used several The recursive part of the LIST definition is used several times, terminating with the non-recursive part:times, terminating with the non-recursive part:

number comma LISTnumber comma LIST

24 , 88, 40, 3724 , 88, 40, 37

number comma LISTnumber comma LIST

88 , 40, 3788 , 40, 37

number comma LISTnumber comma LIST

40 , 3740 , 37

numbernumber

3737

Page 5: Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.

5

Infinite RecursionInfinite Recursion

All recursive definitions must have a non-recursive part to All recursive definitions must have a non-recursive part to avoid avoid infinite recursioninfinite recursion

The non-recursive part is often called the The non-recursive part is often called the base casebase case

Page 6: Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.

6

Recursive DefinitionsRecursive Definitions

N!, for any positive integer N, is defined to be the product N!, for any positive integer N, is defined to be the product of all integers between 1 and N inclusiveof all integers between 1 and N inclusive

This definition can be expressed recursively as:This definition can be expressed recursively as:

1! = 11! = 1

N! = N * (N-1)!N! = N * (N-1)!

The concept of the factorial is defined in terms of another The concept of the factorial is defined in terms of another factorialfactorial

Eventually, the base case of 1! is reachedEventually, the base case of 1! is reached

Page 7: Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.

7

Recursive DefinitionsRecursive Definitions

5!5!

5 * 4!5 * 4!

4 * 3!4 * 3!

3 * 2!3 * 2!

2 * 1!2 * 1!

11

2

6

24

120

Page 8: Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.

8

Recursive ProgrammingRecursive Programming

A method in Java can invoke itself; if set up that way, it is A method in Java can invoke itself; if set up that way, it is called a called a recursive methodrecursive method

The code of a recursive method must be structured to The code of a recursive method must be structured to handle both the base case and the recursive casehandle both the base case and the recursive case

Each call to the method sets up a new execution Each call to the method sets up a new execution environment, with new parameters and local variablesenvironment, with new parameters and local variables

As always, when the method completes, control returns to As always, when the method completes, control returns to the method that invoked it (which may be an earlier the method that invoked it (which may be an earlier invocation of itself)invocation of itself)

Page 9: Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.

9

Recursive ProgrammingRecursive Programming

Consider the problem of computing the sum of all the Consider the problem of computing the sum of all the numbers between 1 and any positive integer Nnumbers between 1 and any positive integer N

This problem can be recursively defined as:This problem can be recursively defined as:

i = 1

N

i = 1

N-1

i = 1

N-2

= N + = N + (N-1) +

= etc.

Page 10: Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.

10

Recursive ProgrammingRecursive Programming

main

sum

sum

sum

sum(3)

sum(1)

sum(2)

result = 1

result = 3

result = 6

Page 11: Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.

11

Recursive ProgrammingRecursive Programming

Note that just because we can use recursion to solve a Note that just because we can use recursion to solve a problem, doesn't mean we shouldproblem, doesn't mean we should

For instance, we usually would not use recursion to solve For instance, we usually would not use recursion to solve the sum of 1 to N problem, because the iterative version is the sum of 1 to N problem, because the iterative version is easier to understandeasier to understand

However, for some problems, recursion provides an elegant However, for some problems, recursion provides an elegant solution, often cleaner than an iterative versionsolution, often cleaner than an iterative version

You must carefully decide whether recursion is the correct You must carefully decide whether recursion is the correct technique for any problemtechnique for any problem

Page 12: Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.

12

Indirect RecursionIndirect Recursion

A method invoking itself is considered to be A method invoking itself is considered to be direct recursiondirect recursion

A method could invoke another method, which invokes A method could invoke another method, which invokes another, etc., until eventually the original method is another, etc., until eventually the original method is invoked againinvoked again

For example, method For example, method m1m1 could invoke could invoke m2m2, which invokes , which invokes m3m3, which in turn invokes , which in turn invokes m1m1 again again

This is called This is called indirect recursionindirect recursion, and requires all the same , and requires all the same care as direct recursioncare as direct recursion

It is often more difficult to trace and debugIt is often more difficult to trace and debug

Page 13: Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.

13

Indirect RecursionIndirect Recursion

m1 m2 m3

m1 m2 m3

m1 m2 m3

Page 14: Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.

Maze TraversalMaze Traversal

We can use recursion to find a path through a mazeWe can use recursion to find a path through a maze

From each location, we can search in each directionFrom each location, we can search in each direction

Recursion keeps track of the path through the mazeRecursion keeps track of the path through the maze

The base case is an invalid move or reaching the final The base case is an invalid move or reaching the final destinationdestination

See See MMazeSearcazeSearch.javah.java (page 472)(page 472) See See Maze.javaMaze.java (page 474)(page 474)

Page 15: Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.

Towers of HanoiTowers of Hanoi

The The Towers of HanoiTowers of Hanoi is a puzzle made up of three vertical is a puzzle made up of three vertical pegs and several disks that slide on the pegspegs and several disks that slide on the pegs

The disks are of varying size, initially placed on one peg The disks are of varying size, initially placed on one peg with the largest disk on the bottom with increasingly with the largest disk on the bottom with increasingly smaller ones on topsmaller ones on top

The goal is to move all of the disks from one peg to another The goal is to move all of the disks from one peg to another under the following rules:under the following rules:• We can move only one disk at a timeWe can move only one disk at a time

• We cannot move a larger disk on top of a smaller oneWe cannot move a larger disk on top of a smaller one

Page 16: Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.

Towers of HanoiTowers of Hanoi

An iterative solution to the Towers of Hanoi is quite An iterative solution to the Towers of Hanoi is quite complexcomplex

A recursive solution is much shorter and more elegantA recursive solution is much shorter and more elegant

See See SolveTowersSolveTowers.java.java (page 479) (page 479) See See TowersOfHanoiTowersOfHanoi.java.java (page 480)(page 480)