+ All Categories
Home > Documents > ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem...

ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem...

Date post: 21-Dec-2015
Category:
View: 216 times
Download: 1 times
Share this document with a friend
Popular Tags:
35
ELC 310 Day 24
Transcript
Page 1: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

ELC 310

Day 24

Page 2: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-2

Agenda

• Questions?• Problem set 5 Parts A Corrected

Good results

• Problem set 5 Part B Due• Problem set 6 posted

Due Dec 13

• Discussion on Recursion

Page 3: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

Chapter 11

Recursion

Page 4: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-4

Recursion

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

• Chapter 11 focuses on:

thinking in a recursive manner programming in a recursive manner the correct use of recursion recursion examples

Page 5: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-5

Outline

Recursive Thinking

Recursive Programming

Using Recursion

Recursion in Graphics

Page 6: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-6

Recursive Thinking

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

• When defining an English word, a recursive definition is often not helpful

• But in other situations, a recursive definition can be an appropriate way to express a concept

• Before applying recursion to programming, it is best to practice thinking recursively

Page 7: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-7

Recursive Definitions

• Consider the following list of numbers:

24, 88, 40, 37

• Such a list can be defined as follows:

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

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

• The concept of a LIST is used to define itself

Page 8: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-8

Recursive Definitions

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

number comma LIST 24 , 88, 40, 37

number comma LIST 88 , 40, 37

number comma LIST 40 , 37

number 37

Page 9: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-9

Infinite Recursion

• All recursive definitions have to have a non-recursive part

• If they didn't, there would be no way to terminate the recursive path

• Such a definition would cause infinite recursion

• This problem is similar to an infinite loop, but the non-terminating "loop" is part of the definition itself

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

Page 10: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-10

Recursive Definitions

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

• This definition can be expressed recursively as:

1! = 1

N! = N * (N-1)!

• A factorial is defined in terms of another factorial

• Eventually, the base case of 1! is reached

Page 11: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-11

Recursive Definitions

5!

5 * 4!

4 * 3!

3 * 2!

2 * 1!

1

2

6

24

120

Page 12: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-12

Outline

Recursive Thinking

Recursive Programming

Using Recursion

Recursion in Graphics

Page 13: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-13

Recursive Programming

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

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

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

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

Page 14: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-14

Recursive Programming

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

• This problem can be recursively defined as:

3

1

2

1

1

11

21

1

N

i

N

i

N

i

N

i

iNNN

iNNiNi

Page 15: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-15

Recursive Programming

// This method returns the sum of 1 to num

public int sum (int num)

{

int result;

if (num == 1)

result = 1;

else

result = num + sum (n-1);

return result;

}

Page 16: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-16

Recursive Programming

main

sum

sum

sum

sum(3)

sum(1)

sum(2)

result = 1

result = 3

result = 6

Page 17: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-17

Recursive Programming

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

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

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

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

Page 18: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-18

Iterative Programming

// This method returns the sum of 1 to num

public int sum (int num)

{

int result = 0;

for (int i = num; i>0; i--)

result += i;

return result;

}

Page 19: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-19

Indirect Recursion

• A method invoking itself is considered to be direct recursion

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

• For example, method m1 could invoke m2, which invokes m3, which in turn invokes m1 again

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

• It is often more difficult to trace and debug

Page 20: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-20

Indirect Recursion

m1 m2 m3

m1 m2 m3

m1 m2 m3

Page 21: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-21

Outline

Recursive Thinking

Recursive Programming

Using Recursion

Recursion in Graphics

Page 22: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-22

Maze Traversal

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

• From each location, we can search in each direction

• Recursion keeps track of the path through the maze

• The base case is an invalid move or reaching the final destination

• See MazeSearch.java (page 583)• See Maze.java (page 584)

Page 23: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-23

Maze TraversalMazeSearch11101100011111011101111001100001010101011101110101111010000111001101111110111110000000000001111111111111The maze was successfully traversed!77701100011113077707771001000070707030077707770703337070000773003707777770333370000000000007777777777777

1 = open path

0 = blocked path

3 = failed path

7 = successful path

Page 24: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-24

Towers of Hanoi

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

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

• The goal is to move all of the disks from one peg to another under the following rules:

We can move only one disk at a time

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

Page 25: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-25

Towers of Hanoi

Original Configuration Move 1

Move 3Move 2

Page 26: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-26

Towers of Hanoi

Move 4 Move 5

Move 6 Move 7 (done)

Page 27: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-27

Towers of Hanoi

• Recursive Rules Move the top N-1 disks from Src to Aux (using Dst as an

intermediary peg) Move the bottom disks from Src to Dst Move N-1 disks from Aux to Dst (using Src as an

intermediary peg)

• An iterative solution to the Towers of Hanoi is quite complex

• A recursive solution is much shorter and more elegant

• See SolveTowers.java (page 590) • See TowersOfHanoi.java (page 591)

Page 28: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-28

Outline

Recursive Thinking

Recursive Programming

Using Recursion

Recursion in Graphics

Page 29: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-29

Tiled Pictures

• Consider the task of repeatedly displaying a set of images in a mosaic

Three quadrants contain individual images Upper-left quadrant repeats pattern

• The base case is reached when the area for the images shrinks to a certain size

• See TiledPictures.java (page 594)

Page 30: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-30

Tiled Pictures

Page 31: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-31

Fractals

• A fractal is a geometric shape made up of the same pattern repeated in different sizes and orientations

• The Koch Snowflake is a particular fractal that begins with an equilateral triangle

• To get a higher order of the fractal, the sides of the triangle are replaced with angled line segments

• See KochSnowflake.java (page 597)• See KochPanel.java (page 600)

Page 32: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-32

Koch Snowflakes

< x5, y5>

< x1, y1>

Becomes

< x5, y5>

< x1, y1>

< x4, y4>

< x2, y2>

< x3, y3>

Page 33: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-33

Koch Snowflakes

Page 34: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-34

Koch Snowflakes

Page 35: ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.

© 2004 Pearson Addison-Wesley. All rights reserved 11-35

Summary

• Chapter 11 has focused on:

thinking in a recursive manner programming in a recursive manner the correct use of recursion recursion examples


Recommended