Recursion in Computer Science: Factorials, Exponentiation, and Binary Search, Study notes of Data Structures and Algorithms

An introduction to recursion in computer science, covering the concepts of factorials, integer exponentiation, recursive c++ functions, and binary search. It includes examples and exercises for students to practice.

Typology: Study notes

Pre 2010

Uploaded on 08/09/2009

koofers-user-h1u
koofers-user-h1u 🇺🇸

9 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI-1200 Computer Science II Spring 2006
Lecture 9 Introduction to Recursion
Review from Lecture 8
Returning const references to member variables provides access to important variables without the cost of
copying and without the danger of allowing them to be changed.
Lists vs. vectors
Iterators
Special properties of vector iterators
The erase function from vectors and lists.
Sieve of Eratosthenes
Today’s Lecture
Introduction to recursion: factorials and exponentiation
How recursion works
Iteration vs. recursion
Rules for writing recursive functions
Examples that we will work on together:
Printing a vector in reverse order
Binary search
We will look at more sophisticated problems in Lecture 10.
9.1 Recursive Definitions of Factorial and Integer Exponentiation
The factorial is defined for non-negative integers as
n! = (n·(n1)! n > 0
1n== 0
Computing integer powers is defined as:
np=(n·np1p > 0
1p== 0
9.2 Recursive C++ Functions
C++, like other modern programming languages, allows functions to call themselves. This gives a direct method of
implementing recursive functions.
Here’s the implementation of factorial:
int fact(int n) {
if (n == 0) {
return 1;
} else {
int result = fact(n-1);
return n * result;
}
}
pf3
pf4

Partial preview of the text

Download Recursion in Computer Science: Factorials, Exponentiation, and Binary Search and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CSCI-1200 Computer Science II — Spring 2006

Lecture 9 — Introduction to Recursion

Review from Lecture 8

  • Returning const references to member variables provides access to important variables without the cost of copying and without the danger of allowing them to be changed.
  • Lists vs. vectors
  • Iterators
  • Special properties of vector iterators
  • The erase function from vectors and lists.
  • Sieve of Eratosthenes

Today’s Lecture

  • Introduction to recursion: factorials and exponentiation
  • How recursion works
  • Iteration vs. recursion
  • Rules for writing recursive functions
  • Examples that we will work on together:
    • Printing a vector in reverse order
    • Binary search

We will look at more sophisticated problems in Lecture 10.

9.1 Recursive Definitions of Factorial and Integer Exponentiation

  • The factorial is defined for non-negative integers as

n! =

n · (n − 1)! n > 0 1 n == 0

  • Computing integer powers is defined as:

np^ =

n · np−^1 p > 0 1 p == 0

9.2 Recursive C++ Functions

C++, like other modern programming languages, allows functions to call themselves. This gives a direct method of implementing recursive functions.

  • Here’s the implementation of factorial:

int fact(int n) { if (n == 0) { return 1; } else { int result = fact(n-1); return n * result; } }

  • And here’s the implementation of exponentiation:

int intpow(int n, int p) { if (p == 0) { return 1; } else { return n * intpow( n, p-1 ); } }

9.3 The Mechanism of Recursive Function Calls

  • For each recursive call (or any function call), a program creates an activation record to keep track of:
    • Completely separate instances of the parameters and local variables for the newly-called function.
    • The location in the calling function code to return to when the newly-called function is complete. (Who asked for this function to be called? Who wants the answer?)
    • Which activation record to return to when the function is done. For recursive functions this can be confusing since there are multiple activation records waiting for an answer from the same function.
  • This is illustrated in the following diagram of the call fact(4). Each box is an activation record, the solid lines indicate the function calls, and the dashed lines indicate the returns. Inside of each box we list the parameters and local variables and make notes about the computation.

1

fact(4) n= result = fact(3) return 4*

fact(3) n= result = fact(2) return 3*

fact(2) n= result = fact(1) return 2*

fact(1) n= result = fact(0) return 1*

fact(0) n= return 1

2

1

24

6

9.4 Iteration vs. Recursion

  • Each of the above functions could also have been written using a for or while loop, i.e. iteratively.
  • For example, here is an iterative version of factorial:

int ifact(int n) { int result = 1; for (int i=1; i<=n; ++i) result = result * i; return result; }

  • Exercise: What will this print when called in the following code?

int main() { vector a; a.push_back(3); a.push_back(5); a.push_back(11); a.push_back(17); print_vec(a); }

  • Exercise: How can you change the second print vec function as little as possible so that this code prints the contents of the vector in reverse order?

9.8 Binary Search

  • Suppose you have a vector v (where T is a placeholder for a specific type), sorted so that:

v[0] <= v[1] <= v[2] <= ...

  • Now suppose that you want to find if a particular value x is in the vector somewhere. How can you do this without looking at every value in the vector?
  • The solution is an algorithm called binary search. Let’s write the recursive version of this algorithm. We’re going to write it for general vectors using the template syntax. This function will work on vectors of any type, as long as the basic operators such as < and == are defined for that type. Here’s the prototype for the driver function:

template bool binsearch(const vector& v, const T& x);

9.9 Looking Ahead to Lecture 10

  • The problems we looked at today can be easily solved using non-recursive techniques. This will not be true when we look at these two problems next time: - Sorting a vector using the mergesort technique. - Solving a “word search” problem in a grid of letters allowing non-straight paths.