Recursion: CSIS1117 Computer Programming

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Recursion

CSIS1117 Computer Programming

Recursion

Suppose we want to compute the exponentiation


of a positive number y, i.e. yn = y y
y, where n >= 0.
When solving a problem, one basic technique is to
break the problem into sub-problems.
Sometimes, it turns out that at least one of the
sub-problems is a smaller example of the same
problem.

Accomplishing the sub-problems can help to solve the


original problem.

c1117 lecture 11

Recursion

Calculating the exponentiation of y:

If n equals to 0, y0 = 1
If n equals to 1, y1 = y y0
If n equals to 2, y2 = y y1
If n equals to k, yk = y yk-1
Finishing the sub-problem yk-1 can help to solve the
original problem yk.

We can use recursion to solve this kind of problem.

Recursion is a function that calls itself.


It is a very powerful problem solving technique.

c1117 lecture 11

Recursion

The core idea is to identify the recursive structure


of a problem.

Once the recursive structure is known, the


corresponding program is usually very simple.

A general rule for defining a recursive function:

Base case: Find out the simplest case(s) of the problem

E.g. n equals to 0, y0 = 1

Recursive case: Find the rule for solving the problem if


the sub-problem(s) is solved.

E.g. We can express yk in terms of an expression


involving yk-1

c1117 lecture 11

Example 1

Calculating the exponentiation yn:


We can design a function exp(y,n) to compute
the exponentiation based on the recursive
structure.
double exp(double y, int n);

Base case:
if(n == 0) return 1;

Recursive case:
if(n > 0) return y * exp(y, n-1);

c1117 lecture 11

Example 1
The base case
double exp(double y, int n){
if (n == 0)
The recursive case
return 1;
return y * exp(y, n-1);
}

Recursion is a function that calls itself.

See exp.cc

c1117 lecture 11

How does it work?


6

Example 1
2

exp(2,3);

c1117 lecture 11

double exp(double y, int n){


if (n == 0) return 1;
return y * exp(y, n-1);
}4
2
2
double exp(double y, int n){
if (n == 0) return 1;
return y * exp(y, n-1);
}2
2
1
double exp(double y, int n){
if (n == 0) return 1;
return y * exp(y, n-1);
}
1
0
2
double exp(double y, int n){
if (n == 0) return 1;
return y * exp(y, n-1);
}
7

Example 2

Calculate the n-factorial (n!) using recursion.

n! = n (n 1) 3 2 1
Design a function factorial(n) to compute the nfactorial
int factorial(int n);

Base case: when n equals 0 or 1.


if(n == 0 || n == 1)
return 1;

Recursive case: subtask with size n-1, n!=n(n-1!)


if(n > 1) return n * factorial(n-1);

c1117 lecture 11

Example on linked lists

A linked list itself can be described recursively: a


list is either

an empty list; or
a node followed by a (shorter) linked list with one node
lesser.

mylist

dog

A node

c1117 lecture 11

cat

bat

ant

A shorter linked list

Example 3

Finding the length of a linked list pointed by


mylist

Design a function ListLength(Node * ptr) to


calculate the length
int ListLength(Node * ptr){
if(ptr == 0) return 0;
return 1 + ListLength(ptr->next);
}

c1117 lecture 11

10

ListLength(mylist);
ListLength(mylist);

ptr

mylist

int
int ListLength(Node
ListLength(Node ** ptr){
ptr){
if(ptr
if(ptr ==
== 0)
0) return
return 0;
0;
return
return 11 ++ ListLength(ptr->next);
ListLength(ptr->next);
}}
ptr
3 int
int ListLength(Node
ListLength(Node ** ptr){
ptr){
if(ptr
if(ptr ==
== 0)
0) return
return 0;
0;
return
return 11 ++ ListLength(ptr->next);
ListLength(ptr->next);
}}
ptr
2 int
int ListLength(Node
ListLength(Node ** ptr){
ptr){
if(ptr
if(ptr ==
== 0)
0) return
return 0;
0;
return
return 11 ++ ListLength(ptr->next);
ListLength(ptr->next);
}}
ptr
int ListLength(Node
ListLength(Node ** ptr){
ptr){
1 int
if(ptr
if(ptr ==
== 0)
0) return
return 0;
0;
return
return 11 ++ ListLength(ptr->next);
ListLength(ptr->next);
}}
0
ptr
int
int ListLength(Node
ListLength(Node ** ptr){
ptr){
if(ptr
if(ptr ==
== 0)
0) return
return 0;
0;
return
return 11 ++ ListLength(ptr->next);
ListLength(ptr->next);
}}

c1117 lecture 11

dog

cat

bat

ant

11

Example 4

Print the elements in a linked list pointed by


mylist, one by one.

Design a function PrintList(Node * ptr) to print


the elements
void PrintList(Node * ptr){
if(ptr == 0) return;
cout << ptr->info << endl;
PrintList(ptr->next);
}

c1117 lecture 11

12

void
void PrintList(Node
PrintList(Node ** ptr){
ptr){ ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
PrintList(ptr->next);
PrintList(ptr->next);
}}
void
void PrintList(Node
PrintList(Node ** ptr){
ptr){ ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
PrintList(ptr->next);
PrintList(ptr->next);
}}
void
void PrintList(Node
PrintList(Node ** ptr){
ptr){ ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
PrintList(ptr->next);
PrintList(ptr->next);
}}
void
void PrintList(Node
PrintList(Node ** ptr){
ptr){ ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
PrintList(ptr->next);
PrintList(ptr->next);
}}
void
void PrintList(Node
PrintList(Node ** ptr){
ptr){
ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
PrintList(ptr->next);
PrintList(ptr->next);
c1117 lecture}
}11

PrintList(mylist);
PrintList(mylist);

mylist

dog

cat

dog
cat
bat
ant

bat

ant

13

What happens if the order of the last two


statements change?
void PrintList(Node * ptr){
if(ptr == 0) return;
PrintList(ptr->next);
cout << ptr->info << endl;
}

The function is called before the node info is


printed. The linked list is printed in reverse order !!

See print-list.cc as an example

c1117 lecture 11

14

void
void PrintList(Node
PrintList(Node ** ptr){
ptr){ ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
PrintList(ptr->next);
PrintList(ptr->next);
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
}} void PrintList(Node * ptr){
void PrintList(Node * ptr){ ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
PrintList(ptr->next);
PrintList(ptr->next);
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
}}
void
void PrintList(Node
PrintList(Node ** ptr){
ptr){ ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
PrintList(ptr->next);
PrintList(ptr->next);
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
}}
void
void PrintList(Node
PrintList(Node ** ptr){
ptr){ ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
PrintList(ptr->next);
PrintList(ptr->next);
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
}}
void
void PrintList(Node
PrintList(Node ** ptr){
ptr){
ptr
if(ptr
if(ptr ==
== 0)
0) return;
return;
PrintList(ptr->next);
PrintList(ptr->next);
cout
cout <<
<< ptr->info
ptr->info <<
<< endl;
endl;
c1117 lecture}
}11

PrintList(mylist);
PrintList(mylist);

mylist

dog

cat

ant
bat
cat
ant

bat

ant

15

Example on Array

Write a recursive function to find the sum of


the elements in an array.

Base case: an array with size zero.


Recursive case: an element followed by an (shorter)
array with one element lesser.
1

A element

c1117 lecture 11

10

18 15

30 21

A shorter array with


one element lesser

16

Example on Array

We can use an index to represent the starting


position of the array that need to pass
int sum(int x[], int pos, int size){
if(size == 0) return 0;
return x[pos] + sum(x, pos+1, size-1);
}
int a[] = {1, 2, 10, 9, 18};
int size = sizeof(a) / sizeof(int);
cout << sum(a, 0, size) << endl;

Use the variable pos to represent


the elements that have been sum.
c1117 lecture 11

Can we do better?
17

Recursive vs iterative

Every recursive function can be rewritten using


iterative (loop) approach or vice versa.
Which one is better?

Recursive solutions are usually simpler to program, test,


debug, and verify.
However, its in general slower than iterative approach.
Some recursive solutions are extremely slow comparing
to their iterative counterparts. See fib.cc.
Note that each function call incurs some overhead.

Memory for running a function


Time for invoking and returning to a function.

c1117 lecture 11

18

You might also like