Docsity

Recursion - Introduction to Programming II - Lecture Slides | CS 112, Study notes of Computer Science

Toggle sidebar
bg1
Intro to Programming II
Recursion
Chris Brooks
Department of Computer Science
University of San Francisco
Departmentof Computer Science —University of San Francisco – p.1/??
17-0: Recursion
Recursion is a fundamental problem-solving technique
Involves decomposing a problem into:
A base case that can be solved directly
A recursive step that indicates how to handle more
complex cases.
A common recursive example is factorial:
long factorial(int input)
if (input == 1)
return 1;
else
return input * factorial(input - 1);
Departmentof Computer Science —University of San Francisco – p.2/??
17-1: Recursion
A more interesting example is the Towers of Hanoi.
It’s hard to write an iterative program to solve this, but the
recursive version is startlingly simple:
void towers(int ndisks, Tower startTower, Tower goalTower, Tower tempTower)
if (ndisks == 0)
return;
else
towers(ndisks - 1, startTower, tempTower, goalTower);
moveDisk(startTower, goalTower);
towers(ndisks - 1, tempTower, goalTower, startTower);
Departmentof Computer Science —University of San Francisco – p.3/??
17-2: Infinite recursion
A common error in recursion is forgetting the base case.
This can lead to infinite recursion
double factorial(int input)
return input * factorial(input - 1);
This eill eventually have a stack overflow.
Departmentof Computer Science —University of San Francisco – p.4/??
17-3: Exercise: Fibonacci numbers
The Fibonacci numbers are defined as follows:
f(1) = f(2) = 1 f(n) = f(n−1) + f(n−2)
The first few numbers are 1,1,2,3,5,8,13,21,...
Write a class called Fibonacci. It should have a method
called getFib(int n) that calculates the nth Fibonacci
number, plus a main method to test it.
Departmentof Computer Science —University of San Francisco – p.5/??
17-4: Exercise: Fibonacci numbers
What is a problem with the naive way of implementing
Fibonacci?
Can you think of a way around this?
How would you implement Fibonacci iteratively?
Departmentof Computer Science —University of San Francisco – p.6/??
pf2

Partial preview of the text

Download Recursion - Introduction to Programming II - Lecture Slides | CS 112 and more Study notes Computer Science in PDF only on Docsity!

Intro to Programming II

Recursion

Chris Brooks Department of Computer Science University of San Francisco ?? Department of Computer Science — University of San Francisco – p.1/

Recursion 17-0:

Recursion is a fundamental problem-solving technique Involves decomposing a problem into: A base case that can be solved directly A recursive step that indicates how to handle more complex cases. A common recursive example is factorial: input) factorial(int long

  1. == (input if 1; return else 1); - factorial(input * input return ?? Department of Computer Science — University of San Francisco – p.2/

Recursion 17-1:

A more interesting example is the Towers of Hanoi. It’s hard to write an iterative program to solve this, but the recursive version is startlingly simple: tempTower) Tower goalTower, Tower startTower, Tower ndisks, towers(int void

  1. == (ndisks if return; else goalTower); tempTower, startTower, 1, - towers(ndisks goalTower); moveDisk(startTower, startTower); goalTower, tempTower, 1, - towers(ndisks ?? Department of Computer Science — University of San Francisco – p.3/

Infinite recursion 17-2:

A common error in recursion is forgetting the base case. infinite recursion This can lead to input) factorial(int double 1); - factorial(input * input return This eill eventually have a stack overflow. ?? Department of Computer Science — University of San Francisco – p.4/

Exercise: Fibonacci numbers 17-3:

The Fibonacci numbers are defined as follows:

  1. − n( f 1) + − n( f ) =n( f (2) = 1 f (1) = f The first few numbers are 1,1,2,3,5,8,13,21,... Write a class called Fibonacci. It should have a method called getFib(int n) that calculates the nth Fibonacci number, plus a main method to test it. ?? Department of Computer Science — University of San Francisco – p.5/

Exercise: Fibonacci numbers 17-4:

What is a problem with the naive way of implementing Fibonacci? Can you think of a way around this? How would you implement Fibonacci iteratively? ?? Department of Computer Science — University of San Francisco – p.6/

Recursion: Traversing a Maze 17-5:

Solving a maze is the sort of problem that requires trial-and-error. When you’re stuck, back up and undo the last thing you did. This sort of approach works well with recursion. We’ll represent the maze as a two-dimensional array. 1 = clear, 0 = blocked. Start in the upper left, get to the lower right. ?? Department of Computer Science — University of San Francisco – p.7/

Exercise: Solving a maze 17-6:

Change the rules so that you always try to go left, then right, then up, then down. Write a Maze constructor that takes two arguments: row and col and generates a random maze of that size. ?? Department of Computer Science — University of San Francisco – p.8/

Recursion in graphics 17-7:

We can use recursion to easily create interesting graphical effects. For example, recursively tiling a surface. ?? Department of Computer Science — University of San Francisco – p.9/

Recursion in graphics: Exercise 17-8:

Add your own pictures to the applet. Change the applet so that the recursive part of the picture is in the lower right. ?? Department of Computer Science — University of San Francisco – p.10/

Fractals 17-9:

We can also use recursion to draw fractals Example: Koch snowflake Rule: Each line segment is replaced by a “wedge” with sides that are the same length as the replaced piece. As we increase the depth, it begins to look like a snowflake. ?? Department of Computer Science — University of San Francisco – p.11/

Koch Snowflake: exercise 17-10:

Change the color scheme of the applet. Change the default max and min values Change the initial triangle to have its “point” downward. ?? Department of Computer Science — University of San Francisco – p.12/