This action might not be possible to undo. Are you sure you want to continue?

(TEXTBOOK: §4.3, §4.4)

Gongjun Yan

2008/10/29

1

§4.3: Recursive Definitions

A recursive definition of a set always consists of

three distinct clauses:

The basis clause (or simply basis) establishes that

certain objects are in the set.

The inductive clause (or simply induction) establishes the

ways in which elements of the set can be combined to

produce new elements of the set.

The extremal clause asserts that unless an object

satisfying basis clause and inductive clause, the

object is not a member of the set.

2008/10/29

2

Example 1

Examples of Recursive Definition of Set

Example 1. Definition of the Set of Natural

Numbers

The set N is the set that satisfies the following three

clauses:

Basis Clause: 0 e N

Inductive Clause: For any element x in N, x + 1 is

in N.

Extremal Clause: Nothing is in N unless it is

obtained from the Basis and Inductive Clauses.

29 October 2008 Set Theory

3

Example 2

Example 2. Definition of the Set of Even Integers

The set EI is the set that satisfies the following three

clauses:

Basis Clause: 0 e EI

Inductive Clause: For any element x in EI, x + 2,

and x - 2 are in EI.

Extremal Clause: Nothing is in EI unless it is

obtained from the Basis and Inductive Clauses.

29 October 2008

4

Tips

Tips for recursively defining a set:

For the "Basis Clause", try simplest elements in the

set such as smallest numbers (0, or 1), simplest

expressions, or shortest strings. Then see how other

elements can be obtained from them, and

generalize that generation process for the "Inductive

Clause".

Game Time

29 October 2008

5

generalized union

29 October 2008 Set Theory

6

Recursive Definition of Function

Some functions can also be defined recursively.

Condition: The domain of the function you wish to

define recursively must be a set defined recursively.

How to define function recursively:

First the values of the function for the basis elements of

the domain are specified.

Then the value of the function at an element, say x, of

the domain is defined using its value at the parent(s) of

the element x.

29 October 2008

7

Example

Example 3: The function f(n) = n! for natural

numbers n can be defined recursively as follows:

Basis Clause: f(0) = 0! = 1

Inductive Clause: For all natural number n, f(n+1) =

(n+1) f(n).

Using this definition, 3! can be found as follows:

Since 0 ! = 1, 1 ! = 1 * 0 ! = 1 * 1 = 1 ,

Hence 2 ! = 2 * 1 ! = 2 * 1 = 2 .

Hence 3 ! = 3 * 2 ! = 3 * 2 * 1 = 6 .

29 October 2008

8

Extremal Clause

Note that here Extremal Clause is not necessary

because the set of natural numbers can be defined

recursively

that has the extremal clause in it.

So there is no chance of other elements to come into

the function being defined.

29 October 2008

9

Examples

Example 4: The function f(n) = 2

n

for natural

numbers n can be defined recursively as follows:

Basis Clause: f(0) = 1

Inductive Clause: For all natural number n, f(n+1)

= 2 f(n) .

Example 5: The function f(n) = 2n + 1 for natural

numbers n can be defined recursively as follows:

Basis Clause: f(0) = 1

Inductive Clause: For all natural number n, f(n+1) =

f(n) + 2 .

29 October 2008

10

More examples

Example 6: The function L from the set S of strings

over {a, b} to the set of natural numbers that gives

the length of a string can be defined recursively as

follows:

Basis Clause: For symbols a and b of the

alphabet, L(a) = 1 and L(b) = 1.

Inductive Clause: For any string x and y of S, L(xy)

= L(x) + L(y) , where xy is the concatenation of

strings x and y.

29 October 2008

11

The Fibonacci Series

The Fibonacci series f

n≥0

is a famous series defined

by:

f

0

:≡ 0, f

1

:≡ 1, f

n≥2

:≡ f

n−1

+ f

n−2

2008/10/29

12

0

1 1

2 3

5 8

13

Tree Structures --- Rooted Trees

The set of rooted trees

Root

edges connecting these vertices

can be defined recursively by these steps:

BASIS STEP: A single vertex r is a rooted tree.

RECURSIVE STEP: Suppose that T

1

,T

2

,…,T

n

are disjoint rooted

trees with roots r

1

,r

2

,…,r

n

respectively. Then the graph formed by

starting with a root r, which is not in any of the rooted trees

T

1

,T

2

,…,T

n

, and adding an edge from r to each of the vertices

r

1

,r

2

,…,r

n

is also a rooted tree.

2008/10/29

13

Examples of Rooted Trees

2008/10/29

14

Tree Structures --- Binary Trees

The set of extended binary trees can be defined

recursively by these steps:

BASIS STEP: A empty set is an extended binary tree.

RECURSIVE STEP: If T

1

and T

2

are disjoint extended

binary trees, there is an extended binary tree, denoted

by T

1

-T

2

, containing of a root r together with edges

connecting the root to each of the roots of the left

subtree T

1

and the right subtree T

2

when these trees

are nonempty.

2008/10/29

15

Example of Extended Binary Trees

2008/10/29

16

Recursive Algorithms (§4.4)

Recursive definitions can be used to describe

algorithms as well as functions and sets.

Example: A procedure to compute a

n

.

procedure power(a≠0: real, neN)

if n = 0 then

return 1

else

return a · power(a, n−1)

2008/10/29

17

Efficiency of Recursive Algorithms

The time complexity of a recursive algorithm may

depend critically on the number of recursive calls it

makes.

Example: Modular exponentiation to a power n can

take log(n) time if done right, but linear time if

done slightly differently.

Task: Compute b

n

mod m, where

m≥2, n≥0, and 1≤b<m.

2008/10/29

18

Modular Exponentiation

Alg. #1

Uses the fact that b

n

= b·b

n−1

and that

x·y mod m = x·(y mod m) mod m.

(Prove the latter theorem at home.)

procedure mpower(b≥1,n≥0,m>b eN)

{Returns b

n

mod m.}

if n=0 then return 1 else

return (b·mpower(b,n−1,m)) mod m

Note this algorithm takes Θ(n) steps!

2008/10/29

19

Modular Exponentiation

Alg. #2

Uses the fact that b

2k

= b

k· 2

= (b

k

)

2

.

procedure mpower(b,n,m) {same signature}

if n=0 then return 1

else if 2|n then

return mpower(b,n/2,m)

2

mod m

else return (mpower(b,n−1,m)· b) mod m

What is its time complexity?

2008/10/29

20

Θ(log n) steps

A Slight Variation

Nearly identical but takes Θ(n) time instead!

procedure mpower(b,n,m) {same signature}

if n=0 then return 1

else if 2|n then

return (mpower(b,n/2,m)·

mpower(b,n/2,m)) mod m

else return (mpower(b,n−1,m)· b) mod m

2008/10/29

21

The number of recursive calls made is critical.

Recursive Euclid’s Algorithm

Compute Greatest common divisor

procedure gcd(a,beN)

if a = 0 then return b

else return gcd(b mod a, a)

Example: gcd(5,8) = gcd(8%5,5)=gcd(3,5)=

gcd(5%3,3) = gcd(2,3) = gcd(3%2,2) = gcd(1,2)

=gcd(2%1,1)=gcd(0,1) = 1

2008/10/29

22

Sequential Search

Input: L is an array, i and j are positive integers, i j,

and x is the key to be searched for in L.

Output: If x is in L between indexes i and j, then

output its index, else output 0.

Algorithm:

if i j , then

{

if L(i) = x, then return i ;

else return SeqSearch(L, i+1, j, x)

}

else return 0.

29 October 2008

23

Ackermann’s Function

Find the value A(1,0), A(0,1), A(1,1), and A(2,2)

according to the following recursive definition.

2008/10/29

24

( )

( )

¦

¦

¹

¦

¦

´

¦

> > ÷ ÷

= >

= >

=

=

2 and 1 if ) 1 , ( , 1

1 and 1 if 2

0 and 1 if 0

0 if 2

.

n m n m A m A

n m

n m

m n

n m A

Game Time

Game Time

29 October 2008

25

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue reading from where you left off, or restart the preview.

scribd