Docsity

Program Structures - Intermediate Programming - Lecture Slides | CS 251L, Study notes of Computer Science

Partial preview of the text

Download Program Structures - Intermediate Programming - Lecture Slides | CS 251L and more Study notes Computer Science in PDF only on Docsity!

CS251 Intermediate Programming

Lecture 4

Recursion, Part 2

the computer in the lobby.to building 3 now and create an account usingtoday. If you can't log into the computers, go • We'll be programming on the computers• Quiz 1 on September 17.

theframe-by-frame through recursive backtracking to assemblethe whole call stack, recursive functions "unwind" the stacklocation within the larger problem. Since a function cannot seeRecursive functions use the call stack to keep track of theirresults (using non-recursive methods).similar sub-problems, solve those recursively, then combine theto problem solving: break the problem into one or more self-Recursive functions take a Divide/Conquer/Combine approachbehavior to solve the sub-problem as the whole problem.Recursive functions have self-references: they use the samesub-elements have the same structure as their parents.Recursive structures have hierarchical self-similarity: the child

fi

nal solution.

take the form of a tree.computations into a tree form. Recursive structures often Most recursive functions can be visualized by expanding their

Most real programs use a combination of program structures.

1

2

3

1 2 3 1 2 3

Towers of Hanoi (2)

Solution walkthrough:

1

2

3

Step 1

Step 2

Step 3

Step 4

1

2

3

1

2

3

1

2

3

Towers of Hanoi (3)

Solution walkthrough: Step 5

Step 6

Step 7

Step 8

1

2

3

1

2

3

1 2 3 1 2 3 1

2

3

Towers of Hanoi (5)

Solution walkthrough: Step 13

Step 14

Step 15

Step 16

Towers of Hanoi (6)

The general problem is how to move a tower of size

(^) n (^) from peg

(^) o (^) (origin) to peg

d (destination). The general solution is to use a recursive algorithm.

disk fromThe base case applies to a tower of size 1. In this case, we simply move the

(^) o

to

d .

of sizeThe recursive case applies to tower sizes > 1. In this case, we move the tower

(^) n (^) - 1 from peg

o (^) to peg

(^) s (^) (spare) using peg

(^) d (^) as the spare. Then we

move the

n th disk from peg

(^) o

to peg

(^) d

. Finally, we move the tower of size

n -

(^) 1

(the one we temporarily parked on peg

(^) s ) to peg

(^) d,

(^) using peg

(^) o

as the spare.

areNotice that, like the fibonacci example and unlike the factorial example, there

two

(^) recursive function calls for this case.

o

s

d

(^1)

2 3

Less Rigorous Description of the General Solution

Towers of Hanoi (8)

Let's consider the^ Recursive Call Tree

(^) n (^) = 4 case:

#Count from

 to (spare)

o

s

d

} (^1)

(^23)

To move a tower of size

(^) n (^) from peg

(^) o (^) to peg

(^) d , using peg

(^) s (^) as a spare:

  1. If

(^) n (^) = 1, move the disk from

(^) o (^) to (^) d .

  1. If

(^) n (^) > 1,

2.1 Move the tower of size

(^) n (^) - 1 from

(^) o (^) to (^) s , using

(^) d (^) as the spare.

2.2 Move 1 disk from

(^) o (^) to (^) d .

2.3 Move the tower of size

(^) n (^) - 1 from

(^) s (^) to (^) d , using

(^) o (^) as the spare.

Towers of Hanoi (9)

Let's consider the^ Recursive Call Tree

(^) n (^) = 4 case:

#Count from

 to (spare)

o

s

d

} (^1)

(^23)

To move a tower of size

(^) n (^) from peg

(^) o (^) to peg

(^) d , using peg

(^) s (^) as a spare:

  1. If

(^) n (^) = 1, move the disk from

(^) o (^) to (^) d .

  1. If

(^) n (^) > 1,

2.1 Move the tower of size

(^) n (^) - 1 from

(^) o (^) to (^) s , using

(^) d (^) as the spare.

2.2 Move 1 disk from

(^) o (^) to (^) d .

2.3 Move the tower of size

(^) n (^) - 1 from

(^) s (^) to (^) d , using

(^) o (^) as the spare.

Towers of Hanoi (10)

Let's consider the^ Recursive Call Tree

(^) n (^) = 4 case:

#Count from

 to (spare)

There is no combine phase to the recursion. The base cases, in the order they are reached, give the moves of the solution.

4 3 2 1

o

s

d

} (^1)

(^23)

To move a tower of size

(^) n (^) from peg

(^) o (^) to peg

(^) d , using peg

(^) s (^) as a spare:

  1. If

(^) n (^) = 1, move the disk from

(^) o (^) to (^) d .

  1. If

(^) n (^) > 1,

2.1 Move the tower of size

(^) n (^) - 1 from

(^) o (^) to (^) s , using

(^) d (^) as the spare.

2.2 Move 1 disk from

(^) o (^) to (^) d .

2.3 Move the tower of size

(^) n (^) - 1 from

(^) s (^) to (^) d , using

(^) o (^) as the spare.

Lab 1: Towers of Hanoi

  1. Has a recursive method to move a tower of 1. Has a main method so it can be run as a standalone program.

(^) n (^) disks from some peg

(^) o (^) to some

peg

(^) d (^) using spare peg

s .

  1. Has a non-recursive method to move one disk from peg

o (^) to peg

(^) d .

You don’t need to keep track of where the disks are. Just print to the console that you are

moving a disk from

(^) o (^) to

(^) d

. There is only one possible disk this could refer to.

o

s

d

(^1)

2 3

solution? Where is this information stored? How is the program keeping track of where the disks are at various stages of Answer the following question: Run your class with at least two different tower sizes, both Create, compile, and run a class that:

(^) 

3.

To move a tower of size

(^) n (^) from peg

(^) o (^) to peg

(^) d , using peg

(^) s (^) as a spare:

  1. If

(^) n (^) = 1, move the disk from

(^) o (^) to

(^) d .

  1. If

(^) n (^) > 1,

2.1 Move the tower of size

(^) n (^) - 1 from

(^) o (^) to (^) s , using

(^) d (^) as the spare.

2.2 Move 1 disk from

(^) o (^) to

(^) d .

2.3 Move the tower of size

(^) n

(^) - 1 from

(^) s (^) to

(^) d , using

(^) o (^) as the spare.

2.

2.

2.

To move a tower of size

(^) n (^) from peg

(^) o (^) to peg

(^) d , using peg

(^) s (^) as a spare:

  1. If

(^) n (^) = 1, move the disk from

(^) o (^) to

(^) d .

  1. If

(^) n (^) > 1,

2.1 Move the tower of size

(^) n (^) - 1 from

(^) o (^) to (^) s , using

(^) d (^) as the spare.

2.2 Move 1 disk from

(^) o (^) to

(^) d .

2.3 Move the tower of size

(^) n

(^) - 1 from

(^) s (^) to

(^) d , using

(^) o (^) as the spare.

n-1 o
s
n-1 s
d
o
d