preparation data structures 02 recursion

40

Upload: andres-mendez-vazquez

Post on 10-Aug-2015

48 views

Category:

Education


3 download

TRANSCRIPT

Data Structures

Recursion

Andres Mendez-Vazquez

May 6, 2015

1 / 19

Images/cinvestav-1.jpg

Outline

1 Introduction

2 Recursive Method

Questions to Answer When Designing a Recursion

3 Design guidelines for successful recursion

Examples of Code with In�nite Recursion

Example

2 / 19

Images/cinvestav-1.jpg

What is recursion?

Fact

Sometimes, the best way to solve a problem is by solving a smaller version

of the exact same problem �rst

Example

You can build a house by hiring a contractor.

The contractor in turn hires several subcontractors to complete

portions of the house.

Each subcontractor might hire other subcontractors to help.

3 / 19

Images/cinvestav-1.jpg

What is recursion?

Fact

Sometimes, the best way to solve a problem is by solving a smaller version

of the exact same problem �rst

Example

You can build a house by hiring a contractor.

The contractor in turn hires several subcontractors to complete

portions of the house.

Each subcontractor might hire other subcontractors to help.

3 / 19

Images/cinvestav-1.jpg

Another Example

Example: Principal Idea You do your part until your friend �nishesand tells you

4 / 19

Images/cinvestav-1.jpg

Recursive Method

De�nition

A method that calls itself is a recursive method.

The invocation is a recursive call or recursive invocation.

The previous example can be seen in code as follow

5 / 19

Images/cinvestav-1.jpg

Recursive Method

De�nition

A method that calls itself is a recursive method.

The invocation is a recursive call or recursive invocation.

The previous example can be seen in code as follow

/∗∗ Counts down from a g i v en p o s i t i v e i n t e g e r .@param i n t e g e r an i n t e g e r > 0 ∗/p u b l i c s t a t i c vo i d countDown ( i n t i n t e g e r ){

System . out . p r i n t l n ( i n t e g e r ) ;i f ( i n t e g e r > 1)

countDown ( i n t e g e r − 1 ) ;} // end countDown

5 / 19

Images/cinvestav-1.jpg

Example with 3

Tracing the recursive call countDown(3)

//Clientpublic static void main(...){

countDown(3);

...}

public static void countDown(3){

System.out.println(3);

countDown(3 – 1);}

if (3 > 1)

public static void countDown(2){

System.out.println(2);

countDown(2 – 1);}

if (2 > 1)

public static void countDown(1){

System.out.println(1);

}

if (1 > 1)

6 / 19

Images/cinvestav-1.jpg

Outline

1 Introduction

2 Recursive Method

Questions to Answer When Designing a Recursion

3 Design guidelines for successful recursion

Examples of Code with In�nite Recursion

Example

7 / 19

Images/cinvestav-1.jpg

Questions to answer when designing a recursive solution

First

What part of the solution can you contribute directly?

Second

What smaller but identical problem has a solution that, when taken with

your contribution, provides the solution to the original problem?

Third

When does the process end? That is, what smaller but identical problem

has a known solution, and have you reached this problem, or base case?

8 / 19

Images/cinvestav-1.jpg

Questions to answer when designing a recursive solution

First

What part of the solution can you contribute directly?

Second

What smaller but identical problem has a solution that, when taken with

your contribution, provides the solution to the original problem?

Third

When does the process end? That is, what smaller but identical problem

has a known solution, and have you reached this problem, or base case?

8 / 19

Images/cinvestav-1.jpg

Questions to answer when designing a recursive solution

First

What part of the solution can you contribute directly?

Second

What smaller but identical problem has a solution that, when taken with

your contribution, provides the solution to the original problem?

Third

When does the process end? That is, what smaller but identical problem

has a known solution, and have you reached this problem, or base case?

8 / 19

Images/cinvestav-1.jpg

For countDown

Answer 1

The method countDown displays the given integer �rst.

Answer 2

The smaller problem is counting down from integer - 1.

Answer 3

The if statement asks if the process has reached the base case. In our case

integer = 1.

9 / 19

Images/cinvestav-1.jpg

For countDown

Answer 1

The method countDown displays the given integer �rst.

Answer 2

The smaller problem is counting down from integer - 1.

Answer 3

The if statement asks if the process has reached the base case. In our case

integer = 1.

9 / 19

Images/cinvestav-1.jpg

For countDown

Answer 1

The method countDown displays the given integer �rst.

Answer 2

The smaller problem is counting down from integer - 1.

Answer 3

The if statement asks if the process has reached the base case. In our case

integer = 1.

9 / 19

Images/cinvestav-1.jpg

Design guidelines for successful recursion

First

The method must be given an input value.

Second

The method de�nition must contain logic that involves this input value and

leads to di�erent cases.

Third

One or more of these cases should provide a solution that does not require

recursion. These are the base cases, or stopping cases.

Fourth

One or more cases must include a recursive invocation of the method using

a �smaller� argument.

10 / 19

Images/cinvestav-1.jpg

Design guidelines for successful recursion

First

The method must be given an input value.

Second

The method de�nition must contain logic that involves this input value and

leads to di�erent cases.

Third

One or more of these cases should provide a solution that does not require

recursion. These are the base cases, or stopping cases.

Fourth

One or more cases must include a recursive invocation of the method using

a �smaller� argument.

10 / 19

Images/cinvestav-1.jpg

Design guidelines for successful recursion

First

The method must be given an input value.

Second

The method de�nition must contain logic that involves this input value and

leads to di�erent cases.

Third

One or more of these cases should provide a solution that does not require

recursion. These are the base cases, or stopping cases.

Fourth

One or more cases must include a recursive invocation of the method using

a �smaller� argument.

10 / 19

Images/cinvestav-1.jpg

Design guidelines for successful recursion

First

The method must be given an input value.

Second

The method de�nition must contain logic that involves this input value and

leads to di�erent cases.

Third

One or more of these cases should provide a solution that does not require

recursion. These are the base cases, or stopping cases.

Fourth

One or more cases must include a recursive invocation of the method using

a �smaller� argument.

10 / 19

Images/cinvestav-1.jpg

So, be careful

In�nite recursion

A recursive method that does not check for a base case, or that misses the

base case, will execute �forever.� This situation is known as in�nite

recursion.

Which is known as

This situation is known as in�nite recursion.

11 / 19

Images/cinvestav-1.jpg

So, be careful

In�nite recursion

A recursive method that does not check for a base case, or that misses the

base case, will execute �forever.� This situation is known as in�nite

recursion.

Which is known as

This situation is known as in�nite recursion.

11 / 19

Images/cinvestav-1.jpg

Outline

1 Introduction

2 Recursive Method

Questions to Answer When Designing a Recursion

3 Design guidelines for successful recursion

Examples of Code with In�nite Recursion

Example

12 / 19

Images/cinvestav-1.jpg

Example of code with in�nite recursion

Code

/∗∗ Counts down from a g i v en p o s i t i v e i n t e g e r .@param i n t e g e r an i n t e g e r > 0 ∗/p u b l i c s t a t i c vo i d countDown ( i n t i n t e g e r ){

System . out . p r i n t l n ( i n t e g e r ) ;countDown ( i n t e g e r − 1 ) ;

} // end countDown

13 / 19

Images/cinvestav-1.jpg

Recursive Methods That Return a Value

The nice stu� about recursion

You can return values

For example

We can compute the sum 1+ 2+ 3+ ...+ n

14 / 19

Images/cinvestav-1.jpg

Recursive Methods That Return a Value

The nice stu� about recursion

You can return values

For example

We can compute the sum 1+ 2+ 3+ ...+ n

14 / 19

Images/cinvestav-1.jpg

How the code will look

Partial Code

/∗∗ @param n an i n t e g e r > 0@re tu rn the sum 1 + 2 + . . . + n ∗/

p u b l i c s t a t i c i n t sumOf ( i n t n ){i n t sum ;

// What do we put he r e ?

r e t u r n sum ;} // end sumOf

15 / 19

Images/cinvestav-1.jpg

Debugging a recursive method

Something Notable

1 Does the method have at least one input value?

2 Does the method contain a statement that tests an input value and

leads to di�erent cases?

3 Did you consider all possible cases? Does at least one of these cases

cause at least one recursive call?

4 Do these recursive calls involve smaller arguments, smaller tasks, or

tasks that get closer to the solution?

5 If these recursive calls produce or return correct results, will the

method produce or return a correct result?

6 Is at least one of the cases a base case that has no recursive call?

7 Are there enough base cases?

8 Does each base case produce a result that is correct for that case?

9 If the method returns a value, does each of the cases return a value?

16 / 19

Images/cinvestav-1.jpg

Debugging a recursive method

Something Notable

1 Does the method have at least one input value?

2 Does the method contain a statement that tests an input value and

leads to di�erent cases?

3 Did you consider all possible cases? Does at least one of these cases

cause at least one recursive call?

4 Do these recursive calls involve smaller arguments, smaller tasks, or

tasks that get closer to the solution?

5 If these recursive calls produce or return correct results, will the

method produce or return a correct result?

6 Is at least one of the cases a base case that has no recursive call?

7 Are there enough base cases?

8 Does each base case produce a result that is correct for that case?

9 If the method returns a value, does each of the cases return a value?

16 / 19

Images/cinvestav-1.jpg

Debugging a recursive method

Something Notable

1 Does the method have at least one input value?

2 Does the method contain a statement that tests an input value and

leads to di�erent cases?

3 Did you consider all possible cases? Does at least one of these cases

cause at least one recursive call?

4 Do these recursive calls involve smaller arguments, smaller tasks, or

tasks that get closer to the solution?

5 If these recursive calls produce or return correct results, will the

method produce or return a correct result?

6 Is at least one of the cases a base case that has no recursive call?

7 Are there enough base cases?

8 Does each base case produce a result that is correct for that case?

9 If the method returns a value, does each of the cases return a value?

16 / 19

Images/cinvestav-1.jpg

Debugging a recursive method

Something Notable

1 Does the method have at least one input value?

2 Does the method contain a statement that tests an input value and

leads to di�erent cases?

3 Did you consider all possible cases? Does at least one of these cases

cause at least one recursive call?

4 Do these recursive calls involve smaller arguments, smaller tasks, or

tasks that get closer to the solution?

5 If these recursive calls produce or return correct results, will the

method produce or return a correct result?

6 Is at least one of the cases a base case that has no recursive call?

7 Are there enough base cases?

8 Does each base case produce a result that is correct for that case?

9 If the method returns a value, does each of the cases return a value?

16 / 19

Images/cinvestav-1.jpg

Debugging a recursive method

Something Notable

1 Does the method have at least one input value?

2 Does the method contain a statement that tests an input value and

leads to di�erent cases?

3 Did you consider all possible cases? Does at least one of these cases

cause at least one recursive call?

4 Do these recursive calls involve smaller arguments, smaller tasks, or

tasks that get closer to the solution?

5 If these recursive calls produce or return correct results, will the

method produce or return a correct result?

6 Is at least one of the cases a base case that has no recursive call?

7 Are there enough base cases?

8 Does each base case produce a result that is correct for that case?

9 If the method returns a value, does each of the cases return a value?

16 / 19

Images/cinvestav-1.jpg

Debugging a recursive method

Something Notable

1 Does the method have at least one input value?

2 Does the method contain a statement that tests an input value and

leads to di�erent cases?

3 Did you consider all possible cases? Does at least one of these cases

cause at least one recursive call?

4 Do these recursive calls involve smaller arguments, smaller tasks, or

tasks that get closer to the solution?

5 If these recursive calls produce or return correct results, will the

method produce or return a correct result?

6 Is at least one of the cases a base case that has no recursive call?

7 Are there enough base cases?

8 Does each base case produce a result that is correct for that case?

9 If the method returns a value, does each of the cases return a value?

16 / 19

Images/cinvestav-1.jpg

Debugging a recursive method

Something Notable

1 Does the method have at least one input value?

2 Does the method contain a statement that tests an input value and

leads to di�erent cases?

3 Did you consider all possible cases? Does at least one of these cases

cause at least one recursive call?

4 Do these recursive calls involve smaller arguments, smaller tasks, or

tasks that get closer to the solution?

5 If these recursive calls produce or return correct results, will the

method produce or return a correct result?

6 Is at least one of the cases a base case that has no recursive call?

7 Are there enough base cases?

8 Does each base case produce a result that is correct for that case?

9 If the method returns a value, does each of the cases return a value?

16 / 19

Images/cinvestav-1.jpg

Debugging a recursive method

Something Notable

1 Does the method have at least one input value?

2 Does the method contain a statement that tests an input value and

leads to di�erent cases?

3 Did you consider all possible cases? Does at least one of these cases

cause at least one recursive call?

4 Do these recursive calls involve smaller arguments, smaller tasks, or

tasks that get closer to the solution?

5 If these recursive calls produce or return correct results, will the

method produce or return a correct result?

6 Is at least one of the cases a base case that has no recursive call?

7 Are there enough base cases?

8 Does each base case produce a result that is correct for that case?

9 If the method returns a value, does each of the cases return a value?

16 / 19

Images/cinvestav-1.jpg

Debugging a recursive method

Something Notable

1 Does the method have at least one input value?

2 Does the method contain a statement that tests an input value and

leads to di�erent cases?

3 Did you consider all possible cases? Does at least one of these cases

cause at least one recursive call?

4 Do these recursive calls involve smaller arguments, smaller tasks, or

tasks that get closer to the solution?

5 If these recursive calls produce or return correct results, will the

method produce or return a correct result?

6 Is at least one of the cases a base case that has no recursive call?

7 Are there enough base cases?

8 Does each base case produce a result that is correct for that case?

9 If the method returns a value, does each of the cases return a value?

16 / 19

Images/cinvestav-1.jpg

Outline

1 Introduction

2 Recursive Method

Questions to Answer When Designing a Recursion

3 Design guidelines for successful recursion

Examples of Code with In�nite Recursion

Example

17 / 19

Images/cinvestav-1.jpg

Now, what if we solve a recursive function

What we want to solve

n choose k (combinations)

We have (nk

)=

(n − 1

k

)+

(n − 1

k − 1

), with 1 < k < n (1)

Base Case (n1

)= n (k = 1) ,

(nn

)= 1(k = n) (2)

18 / 19

Images/cinvestav-1.jpg

Now, what if we solve a recursive function

What we want to solve

n choose k (combinations)

We have (nk

)=

(n − 1

k

)+

(n − 1

k − 1

), with 1 < k < n (1)

Base Case (n1

)= n (k = 1) ,

(nn

)= 1(k = n) (2)

18 / 19

Images/cinvestav-1.jpg

Now, what if we solve a recursive function

What we want to solve

n choose k (combinations)

We have (nk

)=

(n − 1

k

)+

(n − 1

k − 1

), with 1 < k < n (1)

Base Case (n1

)= n (k = 1) ,

(nn

)= 1(k = n) (2)

18 / 19

Images/cinvestav-1.jpg

Base Code so you can start

Base Code

c l a s s Combinat ions {p u b l i c s t a t i c i n t Combinat ions ( i n t n , i n t k ){

}

p u b l i c s t a t i c vo i d main ( S t r i n g [ ] a r g s ){

}}

19 / 19