functions

51
MSU/CSE 260 Fall 2009 1 Functions Read Section 1.8

Upload: dustin

Post on 05-Jan-2016

45 views

Category:

Documents


2 download

DESCRIPTION

Functions. Read Section 1.8. 1. Outline. Introduction: definitions, example and terminology, image of a subset of domain, sum and product of functions One-to-one functions: strictly increasing, decreasing functions Onto functions Bijections: identity functions Graphs of Functions - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Functions

MSU/CSE 260 Fall 2009 11

Functions

Read Section 1.8

Page 2: Functions

MSU/CSE 260 Fall 2009 22

Outline Introduction: definitions, example and

terminology, image of a subset of domain, sum and product of functions

One-to-one functions: strictly increasing, decreasing functions

Onto functions Bijections: identity functions Graphs of Functions Inverse Functions Compositions of Functions Some Important Functions: Floor,

Ceiling and Factorial functions

Page 3: Functions

MSU/CSE 260 Fall 2009 33

Function Example

Consider your final grades in CSE 260. Your grades will be one of the values from the set {4, 3.5, 3, 2.5, 2, 1.5, 1, 0}

What kind of properties does this assignment have?

Consider the courses you are taking this semester. { (A123, CSE 260), (A123, CSE 232), (A123, MTH 234), (A123, DYLANTHOMAS 111)}

What kind of properties does this assignment have?

Page 4: Functions

MSU/CSE 260 Fall 2009 44

Introduction

Definition: Let A, B be sets. A function f from A to B, denoted f: A → B, is an assignment where each element of A is assigned exactly one element of B.

Notation: f: A → B We write f (a) = b, if b is the element of B

assigned under f to the element a of A. We also say f maps A to B

Formally, f is a function from A to B if and only if xA !yB f(x) = y.

where ! is the uniqueness quantifier.

Page 5: Functions

MSU/CSE 260 Fall 2009 55

Example

StevensSara

4

0

Adams2

3

1Marie

ff: : AA → → BB

GradesStudents

f(Adams)=1f(Marie)=4f(Stevens)=3f(Sara)=3

A B

Page 6: Functions

MSU/CSE 260 Fall 2009 66

Domain, Co-domain, …

Definition: Let f be a function from A to B, that is, f: A → B. Then A is called the domain of f, and B is the codomain of f.

If f(a) = b, then b is called the image of a, and a is a pre-image of b.

The range of f is the set of all images of elements of A.

How are codomain and range related? Range is a subset of the codomain

Page 7: Functions

MSU/CSE 260 Fall 2009 77

Example

StevensSara

4

0

Adams2

3

1Marie

GradesStudents

f (Adams)=1 f (Marie)=4 f (Stevens)=3 f (Sara)=3

The The imageimage of ‘ of ‘MarieMarie’ is ’ is 44; the ; the pre-imagespre-images of of 33 are are ‘‘StevensStevens’ and ‘’ and ‘SaraSara’; the ’; the rangerange of of ff is is {{11, , 33, , 44}}..

A B

ff: : AA → → BB

Page 8: Functions

MSU/CSE 260 Fall 2009 88

Sum, Products of real-valued Functions

Definition: Let f1 and f2 be functions from A to R. Then f1 + f2 and f1 f2 are also functions from A to R, defined as:

(f1 + f2)(x) = f1(x) + f2(x) (f1 f2)(x) = f1(x) f2(x)

Note that if f1 and f2 do not have the same domain, the above operations do not make sense.

Page 9: Functions

MSU/CSE 260 Fall 2009 99

Image of a subset of a domain

Definition: Let f be a function from A to B, and let S be a subset of A.The image of S, denoted f (S), is the subset of B consisting of the images of the elements of S.

Formally: f (S) = { f (s) | s S}. Note that f (A) is the range of f .

In the previous Example, f ({Adams, Sara}) = {3, 1}

Page 10: Functions

MSU/CSE 260 Fall 2009 1010

One-to-one Functions

Definition: A function f from A to B is said to be one-to-one, or injective, if and only if distinct elements of the domain have distinct images. That is,xA yA f (x) = f (y) → x = y.

1

b

a

c

2

34

Page 11: Functions

MSU/CSE 260 Fall 2009 1111

Onto Functions Definition: A function f from A to B is

said to be onto, or surjective, if and only if its range and codomain are the same. That is,

yB xA f(x) = y.

1

c

b

d23

a

Page 12: Functions

MSU/CSE 260 Fall 2009 1212

Bijections

Definition: A function f: A → B is a bijection, or one-to-one correspondence, if it is both one-to-one and onto.

Note that the cardinalities (when dealing with finite sets) of domain and codomain of a bijection are equal.

c

b

d34

a21

Page 13: Functions

MSU/CSE 260 Fall 2009 1313

Summary of Function Types

b

a

c

2

34

23

c

b

d

a

c

b

d34

a1

1 21

1

2

c

b

d34

a

c

b

34

a2

1

onto but not 1-to-1

both 1-to-1 and onto; bijection

Neither 1-to-1 nor onto

Not a function

1-to1 but not onto

Page 14: Functions

MSU/CSE 260 Fall 2009 1414

Monotonic Functions

Definition: Let A and B be subsets of R, the set of real numbers. A function f: A → B is strictly increasing if

xA yA x < y → f (x) < f (y). f: A → B is strictly decreasing if

xA yA x < y → f (x) > f (y). Note that strictly increasing, or strictly

decreasing (strictly monotone) functions must be one-to-one.

Page 15: Functions

MSU/CSE 260 Fall 2009 1515

Inverse Function

Definition: Let function f: A → B be a bijection. The inverse function of f, denoted f -1, is the function, f -1 : B → A, that assigns to each element b of B the element a of A such that f (a) = b.

aA bB f(a) = b → f -1(b) = a. f is called invertible.

c

b

d34

a 1

2f -1(1) = df -1(2) = bf -1(3) = af -1(4) = c

f

Page 16: Functions

MSU/CSE 260 Fall 2009 1616

Inverse Function…

Example: Let f : Z → Z, where f (x) = x + 1.

f is a bijection; what is f -1? Suppose f (x) = y; then x + 1= y; so x = y - 1=

f -1(y) f -1(x) = x 1.

Page 17: Functions

MSU/CSE 260 Fall 2009 1717

Identity Function Let A be a set. The identity function on A

is the function ιA : A → A, where x A ιA (x) = x.

Notes: ιA assigns each element of A to itself. ιA is a bijection.

Page 18: Functions

MSU/CSE 260 Fall 2009 1818

Characteristic and Constant Functions

Let A be a subset of universe U. The characteristic function fA : U → {0, 1}, is such that fA (x) = 1 if x A and fA (x) = 0 if ¬ (x A )

Let A be a set. The constant function f : A → {t} maps each element of A to the same value t.

Page 19: Functions

MSU/CSE 260 Fall 2009 1919

Compositions of Functions

Definition: Let g be a function from A to B and f a function from B to C, that is, g: A → B f: B → C

The composition of f and g, denoted f o g, is function from A to C, defined as follows xA ( f o g)(x) = f (g(x)).

vk

34

p2

1

c

b

d

a(f o g)(a) = 2(f o g)(b) = 3(f o g)(c) = 1(f o g)(d) = 2

g

fqA={a, b, c, d}

B={q, p , k, v}

C={1, 2, 3, 4}

Page 20: Functions

MSU/CSE 260 Fall 2009 2020

Composition of Functions

A B c

g f

f f o go g

A B cA B cccB cB cA B c

Page 21: Functions

MSU/CSE 260 Fall 2009 2121

Example

Consider the two functions f : Z → Z, where f (x) = 2x + 3 g: Z → Z, where g (x) = 3x + 2.

What are f o g, and g o f ? f o g: Z → Z, where

(f o g)(x) = f (g(x)) = f (3x + 2) = 2(3x + 2) + 3

= 6x +7 g o f: Z → Z, where

(g o f )(x) = g( f (x)) = g (2x + 3) = 3(2x + 3) + 2 = 6x + 11

Page 22: Functions

MSU/CSE 260 Fall 2009 2222

Example

3 42

Consider the following functions:

1( ) 4 ( ) ( )

1Compute the following:

1. 5.

2. 6. ( )

3

R R

f x x x g x h x xx

f f f h

f g f h g

. 7. ( )

4.

g f f h g

h g

Page 23: Functions

MSU/CSE 260 Fall 2009 2323

Example

fggf

xxxxgxfgfg

x

x

xxxfxgfgf

xxxxxxfff

xxhx

xgxxxf

RR

that Note

1)4(

1)4())(( 3.

)1(

)1(41

1

14

1

1

1

1))(( 2.

)4(4)4()4( 1.

:following theCompute

)( 1

1)( 4)(

:functions following heConsider t

233

32

22

2

3

22

3333

42

3

Page 24: Functions

MSU/CSE 260 Fall 2009 2424

Example….

3 42

434 4 4

2

34 4 4

2 2 2

1Recall that ( ) 4 ( ) ( )

1

14. ( ( )) 5. ( ) ( ) 4( )

1

1 1 16. ( ) 4

1 1 1

f x x x g x h x xx

h g h g x f h f x x xx

f h g fx x x

34 4

2 2

1 17. ( ) 4

1 1

Note that at least for this example we have

( ) ( )

In fact, the above is true always.

f h gx x

f h g f h g

Page 25: Functions

MSU/CSE 260 Fall 2009 2525

Graph of a Function Definition: Let f: A → B. The graph of f is

the set of ordered pairsGf = {(x, f (x)) | x A}.

Page 26: Functions

MSU/CSE 260 Fall 2009 2626

Graph of a Function…. The graph of the function f : Z → Z,

where f (n) = 2n + 1, is Gf = {(n, 2n + 1) | n Z}

n

f f ((nn))

(0,1)

(1,3)

(2,5)

(-1,-1)

Page 27: Functions

MSU/CSE 260 Fall 2009 2727

Important Integer Functions Whole numbers constitute the backbone

of discrete mathematics. We often need to convert fractions or arbitrary real numbers to integers. These integer functions will help us do that.

Besides the identity function, some important functions are:

The floor function, The ceiling function, The mod function.

Page 28: Functions

MSU/CSE 260 Fall 2009 2828

Floor Function Definition: The floor function from R to Z

assigns to the real number x, the largest integer ≤ x.The value of the floor function at x is denoted by x.

xR nZ x = n n x < n + 1. Examples:

18 = 18 3.75 = 3 – 4.5 = – 5

Page 29: Functions

MSU/CSE 260 Fall 2009 2929

Ceiling Function The ceiling function from R to Z assigns

to the real number x the smallest integer ≥ x. The value of the ceiling function at x is denoted by x.

xR nZ x = n n – 1 < x n. xR x – 1 < x x x < x + 1.

Examples: 18= 18 3.75 = 4 – 4.5 = – 4

Page 30: Functions

MSU/CSE 260 Fall 2009 3030

Floor and Ceiling Functions, recap

0* *

xx xxxx xx

Page 31: Functions

MSU/CSE 260 Fall 2009 3131

Properties of x and x xR nZ x = n n x < n + 1. xR nZ x = n n – 1 < x n. xR nZ x = n x – 1 < n x. xR nZ x = n x n < x +1. xR x – 1 < x x x < x + 1. xR – x = – x xR – x = – x xR mZ x + m = x + m xR mZ x + m = x + m

0* *

xx xxxx xx

Page 32: Functions

MSU/CSE 260 Fall 2009 3232

Example

Prove or disprove the following statements

about real numbers:

a)

b) 2 2

c) {0,1}

x x

x x

x y x y

d)

x 1e)

2 2

xy x y

x

Page 33: Functions

MSU/CSE 260 Fall 2009 3333

Example: Solution

Prove or disprove the following statements

about real numbers:

a) Answer: True

b) 2 2

x x

x x

1

Answer: False, try 2

c) {0,1} Answer: True

1d) Answer: Fasle, try , 3

4x 1

e) 2 2

x

x y x y

xy x y x y

x

Answer: Fasle, try 4x

Page 34: Functions

MSU/CSE 260 Fall 2009 3434

Integer Functions

Page 35: Functions

MSU/CSE 260 Fall 2009 3535

Page 36: Functions

MSU/CSE 260 Fall 2009 3636

Example

Prove or disprove the following statements

about real numbers:

a)

b) 2 2

c) {0,1}

x x

x x

x y x y

d)

x 1e)

2 2

xy x y

x

Page 37: Functions

MSU/CSE 260 Fall 2009 3737

Example: Solution

Prove or disprove the following statements

about real numbers:

a) Answer: True

b) 2 2

x x

x x

1

Answer: False, try 2

c) {0,1} Answer: True

1d) Answer: False, try , 3

4x 1

e) 2 2

x

x y x y

xy x y x y

x

Answer: False, try 4x

Page 38: Functions

MSU/CSE 260 Fall 2009 3838

The mod Function When dividing an integer n by a number

m , the quotient of the division is n/m. What about a simple notation for the remainder of this division?

That’s what the mod function is about: n mod m m is called modulus n = m n/m + n mod m quotient remainder

Page 39: Functions

MSU/CSE 260 Fall 2009 3939

Example

Formally, the mod function is a mapping:

mod : Z Z+ → N wheren mod m = n – m n/m

Examples:

5 mod 3 = 5 – (3 5/3 ) = 5 – (3 1.6 ) = 5 – (3 1 ) = 2

Page 40: Functions

MSU/CSE 260 Fall 2009 4040

Example

n mod m = n – (m n/m ) Examples: -5 mod 3 = -5 – (3 -5/3 )

= -5 – (3 -1.6 ) = -5 – (3 (-2)) = 1.

We also write:5 2 mod 3, 9 0 mod 3, -5 1 mod 3.

Page 41: Functions

MSU/CSE 260 Fall 2009 4141

mod Function

m

0

1

2m-1

+––

Page 42: Functions

MSU/CSE 260 Fall 2009 4242

List Search Methods Problem: Given a list of elements, how

fast can we decide whether or not a given input element belongs to the list?

Linear search Binary search; need to sort the list first Hash table

Page 43: Functions

MSU/CSE 260 Fall 2009 4343

Hash Functions

Page 44: Functions

MSU/CSE 260 Fall 2009 4444

Hash Functions A hash function h: keys → integers maps

“keys” to “small” integers (buckets) Ideal features:

The function should be easy to compute The range values should be “evenly”

distributed Given an image, it should not be “easy” to find

its pre-image Applications

Searching/indexing Information hiding File signature

Page 45: Functions

MSU/CSE 260 Fall 2009 4545

Hashing for Indexing A hash function h: keys → integers maps

“keys” to “small” integers (buckets) Ideally this mapping is done in a

“random” manner so that the bucket values are evenly distributed despite irregularities in the keys.

For simplicity, we will assume that the keys are also integers, denoted by k, and the number of buckets is demoted by m. Note that the buckets are indexed 0 through m - 1.

Page 46: Functions

MSU/CSE 260 Fall 2009 4646

Example

Storing CSE 260, both sections, PIDs\A Using Hash Function h(PID) = k mod 31

Histogram

0123456789

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Bin

Frequency

Page 47: Functions

MSU/CSE 260 Fall 2009 4747

Simple Hash Functions h(k) = k mod m

Suggestion: Choose m to be a prime number that isn’t close to a power of 2.

h(k) = k(k + 3) mod m

Page 48: Functions

MSU/CSE 260 Fall 2009 4848

Hashing for Hiding Information Here, the hash function maps a string to

another string with the property of being very difficult to reverse the result of the hash.

Used in hiding user’s password

Page 49: Functions

MSU/CSE 260 Fall 2009 4949

How password is checked.

Page 50: Functions

MSU/CSE 260 Fall 2009 5050

Hashing for file signature The hash function maps a large string (e.g., a

file) to a fixed size string called digest Examples:

MD5 (Message-Digest algorithm 5), gives a 128-bit hash (digest)

SHA-1 (Secure Hash Algorithm) is a most commonly used from SHA series of cryptographic hash functions, designed by the National Security Agency

SHA-1 produces the 160-bit hash value. Original SHA (or SHA-0) also produce 160-bit hash value, but SHA-0 has been withdrawn by the NSA shortly after publication and was superseded by the revised version commonly referred to as SHA-1. The other functions of SHA series produce 224-, 256-, 384- and 512-bit hash values.

Page 51: Functions

MSU/CSE 260 Fall 2009 5151

Secure Hashes in Python

>>> from hashlib import md5>>> md5("cse201").hexdigest()'fa8190eb6032a99f61d822c2474980bf‘

>>> from hashlib import sha1>>> sha1("cse201").hexdigest()'44dd02666ee30406837b6b5897c6d013fdf4

1dc1'