Docsity

Function, Recursion and Analysis - Discrete Mathematics - Lecture Slides, Slides of Discrete Mathematics

Partial preview of the text

Download Function, Recursion and Analysis - Discrete Mathematics - Lecture Slides and more Slides Discrete Mathematics in PDF only on Docsity!

CSE

Lecture

Function,

Recursion

Analysis

Docsity.com

Motivation

•^

Verification

-^

Complexity

of

the

execution

Input x

Output y

y= Function (x)

Recursion

Docsity.com

I.

Definition

•^

A

function

f:

A

B

maps

elements

in

domain

A

to

codomain

B

such

that

for

each

a

ϵ

A,

f(a)

is

exact

one

element

in

B.

•^

f:

A

B

A:

Domain B:

Codomain f(A):

range

or

image

of

function

f(A)

B

Docsity.com

Examples

(2)

f(x) (1)

f(x)=x

2 ,

x

(3)

f(x)

NOT

a

function

x

f(x)

f(x)

x Docsity.com

Function:

iClicker

Let X={1,2,3,4}. Determine which ofthe following relation is a function A. f={(1,3), (2,4), (3,3)} B. g={(1,2), (3,4), (1,4), (2,3)} C. h={(1,4), (2,3), (3,2), (4,1)} D. w={(1,1), (2,2), (3,3), (4,4)} E. Two of the above.

Docsity.com

Formulation

of

Recursive

Functions

Overview of Recursive Functions

Examples^ 1.

Fibonacci Sequence

The Tower of Hanoi

Merge Sort

Docsity.com

Recursive

Functions:

iClicker

A. We can convert a recursivefunction into a program with iterativeloops. B. Recursive function takes much lesslines of codes for some problems. C. Recursive function always runsslower due to its complexity. D. Two of the above E. None of the above.

Docsity.com

Fibonacci

Sequence:

Problem

Fibonacci

sequence:

Start

with

a

pair

of

rabbits.

For

every

month,

each

pair

bears

a

new

pair,

which

becomes

productive

from

their

second

month.

Calculate

the

number

of

new

pairs

in

month

i,

i

Docsity.com

Fibonacci

Sequence:

iClicker

The

following

is

Fibonacci

sequence:

•^

A.

•^

B.

•^

C.

•^

D.

•^

E.

None

of

the

above

Docsity.com

Fibonacci

Sequence:

Recursion

Function

Fibonacci(n)

If

n<

return

n

Else

return(Fibonacci(n

‐1)+Fibonacci(n

Index:

F(i):

Docsity.com

Fibonacci

sequence:

Golden

Ratio

-^

Derivation:

-^

Let

-^

We

have:

1

5 2

n X

ā†’āˆž

=

(^1) āˆ’

=

n n

n^

f f

X

1 2

2 1

1

2

1

1

1

1

1

āˆ’ āˆ’

āˆ’ āˆ’

āˆ’

āˆ’

āˆ’

āˆ’

  • = + = + =

n n

n n

n

n

n

n n

f f

f f

f

f

f

f f n

X

X

ā†’āˆž

X

X

1 1

=

Docsity.com

Fibonacci

Sequence

and

the

golden

ratio

0

1

2

3

4

5

6

7

8

9

0

1

1

2

3

5

8

13

21

34

1

2

(^1) āˆ’ fn fn

Docsity.com

Tower

of

Hanoi:

Problem

Hanoi

(4,

1,

3):

Move

4

disks

from

pole

1

to

pole

Docsity.com

Tower

of

Hanoi:

Function

Setting:

n,

A=1,

B=

(move

n

disks

from

pole

to

pole

Output:

a

sequence

of

moves

(a,

b)

Function

Hanoi(n,

A,

B)

If

n=1,

return

(A,

B),

Else

{ C

<=

other

pole(not

A

nor

B)

return

Hanoi(n

‐1,

A,

C),

(A,

B),

Hanoi(n

‐1,

C,

B)

}

Docsity.com