Discrete Mathematics in Computer Science B8. Functions Malte - - PowerPoint PPT Presentation

discrete mathematics in computer science
SMART_READER_LITE
LIVE PREVIEW

Discrete Mathematics in Computer Science B8. Functions Malte - - PowerPoint PPT Presentation

Discrete Mathematics in Computer Science B8. Functions Malte Helmert, Gabriele R oger University of Basel October 19, 2020 Malte Helmert, Gabriele R oger (University of Basel) Discrete Mathematics in Computer Science October 19, 2020 1


slide-1
SLIDE 1

Discrete Mathematics in Computer Science

  • B8. Functions

Malte Helmert, Gabriele R¨

  • ger

University of Basel

October 19, 2020

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 1 / 34

slide-2
SLIDE 2

Discrete Mathematics in Computer Science

October 19, 2020 — B8. Functions

B8.1 Partial and Total Functions B8.2 Operations on Partial Functions B8.3 Properties of Functions

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 2 / 34

slide-3
SLIDE 3
  • B8. Functions

Partial and Total Functions

B8.1 Partial and Total Functions

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 3 / 34

slide-4
SLIDE 4
  • B8. Functions

Partial and Total Functions

Important Building Blocks of Discrete Mathematics

Important building blocks: ◮ sets ◮ relations ◮ functions In principle, functions are just a special kind of relations: ◮ f : N0 → N0 with f (x) = x2 ◮ relation R over N0 with R = {(x, y) | x, y ∈ N0 and y = x2}.

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 4 / 34

slide-5
SLIDE 5
  • B8. Functions

Partial and Total Functions

Functional Relations

Definition A binary relation R over sets A and B is functional if for every a ∈ A there is at most one b ∈ B with (a, b) ∈ R.

a b c d e 1 2 3 4

functional

A B a b c d e 1 2 3 4

not functional

A B

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 5 / 34

slide-6
SLIDE 6
  • B8. Functions

Partial and Total Functions

Functions – Examples

◮ f : N0 → N0 with f (x) = x2 + 1 ◮ abs : Z → N0 with abs(x) =

  • x

if x ≥ 0 −x

  • therwise

◮ distance : R2 × R2 → R with distance((x1, y1), (x2, y2)) =

  • (x2 − x1)2 + (y2 − y1)2

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 6 / 34

slide-7
SLIDE 7
  • B8. Functions

Partial and Total Functions

Partial Function – Example

Partial function r : Z × Z Q with r(n, d) =

  • n

d

if d = 0 undefined

  • therwise

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 7 / 34

slide-8
SLIDE 8
  • B8. Functions

Partial and Total Functions

Partial Functions

Definition (Partial function) A partial function f from set A to set B (written f : A B) is given by a functional relation G over A and B. Relation G is called the graph of f . We write f (x) = y for (x, y) ∈ G and say y is the image of x under f . If there is no y ∈ B with (x, y) ∈ G, then f (x) is undefined. Partial function r : Z × Z Q with r(n, d) =

  • n

d

if d = 0 undefined

  • therwise

has graph {((n, d), n

d ) | n ∈ Z, d ∈ Z \ {0}} ⊆ Z2 × Q.

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 8 / 34

slide-9
SLIDE 9
  • B8. Functions

Partial and Total Functions

Domain (of Definition), Codomain, Image

Definition (domain of definition, codomain, image) Let f : A B be a partial function. Set A is called the domain of f , set B is its codomain. The domain of definition of f is the set dom(f ) = {x ∈ A | there is a y ∈ B with f (x) = y}. The image (or range) of f is the set img(f ) = {y | there is an x ∈ A with f (x) = y}.

a b c d e 1 2 3 4 A B

f : {a, b, c, d, e} {1, 2, 3, 4} f (a) = 4, f (b) = 2, f (c) = 1, f (e) = 4 domain {a, b, c, d, e} codomain {1, 2, 3, 4} domain of definition dom(f ) = {a, b, c, e} image img(f ) = {1, 2, 4}

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 9 / 34

slide-10
SLIDE 10
  • B8. Functions

Partial and Total Functions

Preimage

The preimage contains all elements of the domain that are mapped to given elements of the codomain. Definition (Preimage) Let f : A B be a partial function and let Y ⊆ B. The preimage of Y under f is the set f −1[Y ] = {x ∈ A | f (x) ∈ Y }.

a b c d e 1 2 3 4 A B

f −1[{1}] = f −1[{3}] = f −1[{4}] = f −1[{1, 2}] =

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 10 / 34

slide-11
SLIDE 11
  • B8. Functions

Partial and Total Functions

Total Functions

Definition (Total function) A (total) function f : A → B from set A to set B is a partial function from A to B such that f (x) is defined for all x ∈ A. → no difference between the domain and the domain of definition

a b c d e 1 2 3 4 A B

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 11 / 34

slide-12
SLIDE 12
  • B8. Functions

Partial and Total Functions

Specifying a Function

Some common ways of specifying a function: ◮ Listing the mapping explicitly, e. g. f (a) = 4, f (b) = 2, f (c) = 1, f (e) = 4 or f = {a → 4, b → 2, c → 1, e → 4} ◮ By a formula, e. g. f (x) = x2 + 1 ◮ By recurrence, e. g. 0! = 1 and n! = n(n − 1)! for n > 0 ◮ In terms of other functions, e. g. inverse, composition

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 12 / 34

slide-13
SLIDE 13
  • B8. Functions

Partial and Total Functions

Relationship to Functions in Programming

def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) → Relationship between recursion and recurrence

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 13 / 34

slide-14
SLIDE 14
  • B8. Functions

Partial and Total Functions

Relationship to Functions in Programming

def foo(n): value = ... while <some condition>: ... value = ... return value → Does possibly not terminate on all inputs. → Value is undefined for such inputs. → Theoretical computer science: partial function

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 14 / 34

slide-15
SLIDE 15
  • B8. Functions

Partial and Total Functions

Relationship to Functions in Programming

import random counter = 0 def bar(n): print("Hi! I got input", n) global counter counter += 1 return random.choice([1,2,n]) → Functions in programming don’t always compute mathematical functions (except purely functional languages). → In addition, not all mathematical functions are computable.

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 15 / 34

slide-16
SLIDE 16
  • B8. Functions

Operations on Partial Functions

B8.2 Operations on Partial Functions

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 16 / 34

slide-17
SLIDE 17
  • B8. Functions

Operations on Partial Functions

Restrictions and Extensions

Definition (restriction and extension) Let f : A B be a partial function and let X ⊆ A. The restriction of f to X is the partial function f |X : X B with f |X(x) = f (x) for all x ∈ X. A function f ′ : A′ B is called an extension of f if A ⊆ A′ and f ′|A = f . The restriction of f to its domain of definition is a total function. What’s the graph of the restriction? What’s the restriction of f to its domain?

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 17 / 34

slide-18
SLIDE 18
  • B8. Functions

Operations on Partial Functions

Function Composition

Definition (Composition of partial functions) Let f : A B and g : B C be partial functions. The composition of f and g is g ◦ f : A C with (g ◦ f )(x) =      g(f (x)) if f is defined for x and g is defined for f (x) undefined

  • therwise

Corresponds to relation composition of the graphs. If f and g are functions, their composition is a function. Example: f : N0 → N0 with f (x) = x2 g : N0 → N0 with g(x) = x + 3 (g ◦ f )(x) =

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 18 / 34

slide-19
SLIDE 19
  • B8. Functions

Operations on Partial Functions

Properties of Function Composition

Function composition is ◮ not commutative:

◮ f : N0 → N0 with f (x) = x2 ◮ g : N0 → N0 with g(x) = x + 3 ◮ (g ◦ f )(x) = x2 + 3 ◮ (f ◦ g)(x) = (x + 3)2

◮ associative, i. e. h ◦ (g ◦ f ) = (h ◦ g) ◦ f → analogous to associativity of relation composition

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 19 / 34

slide-20
SLIDE 20
  • B8. Functions

Operations on Partial Functions

Function Composition in Programming

We implicitly compose functions all the time. . . def foo(n): ... x = somefunction(n) y = someotherfunction(x) ... Many languages also allow explicit composition of functions,

  • e. g. in Haskell:

incr x = x + 1 square x = x * x squareplusone = incr . square

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 20 / 34

slide-21
SLIDE 21
  • B8. Functions

Properties of Functions

B8.3 Properties of Functions

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 21 / 34

slide-22
SLIDE 22
  • B8. Functions

Properties of Functions

Properties of Functions

◮ Partial functions map every element of their domain to at most one element of their codomain, total functions map it to exactly one such value. ◮ Different elements of the domain can have the same image. ◮ There can be values of the codomain that aren’t the image of any element of the domain. ◮ We often want to exclude such cases → define additional properties to say this quickly

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 22 / 34

slide-23
SLIDE 23
  • B8. Functions

Properties of Functions

Injective Functions

An injective function maps distinct elements of its domain to distinct elements of its co-domain. Definition (Injective Function) A function f : A → B is injective (also one-to-one or an injection) if for all x, y ∈ A with x = y it holds that f (x) = f (y).

a b c d 1 2 3 4 5

injective

A B a b c d 1 2 3 4 5

not injective

A B

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 23 / 34

slide-24
SLIDE 24
  • B8. Functions

Properties of Functions

Injective Functions – Examples

Which of these functions are injective? ◮ f : Z → N0 with f (x) = |x| ◮ g : N0 → N0 with g(x) = x2 ◮ h : N0 → N0 with h(x) =

  • x − 1

if x is odd x + 1 if x is even

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 24 / 34

slide-25
SLIDE 25
  • B8. Functions

Properties of Functions

Composition of Injective Functions

Theorem If f : A → B and g : B → C are injective functions then also g ◦ f is injective. Proof. Consider arbitrary elements x, y ∈ A with x = y. Since f is injective, we know that f (x) = f (y). As g is injective, this implies that g(f (x)) = g(f (y)). With the definition of g ◦ f , we conclude that (g ◦ f )(x) = (g ◦ f )(y). Overall, this shows that g ◦ f is injective.

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 25 / 34

slide-26
SLIDE 26
  • B8. Functions

Properties of Functions

Surjective Functions

A surjective function maps at least one elements to every element

  • f its co-domain.

Definition (Surjective Function) A function f : A → B is surjective (also onto or a surjection) if its image is equal to its codomain,

  • i. e. for all y ∈ B there is an x ∈ A with f (x) = y.

a b c d e 1 2 3 4

surjective

A B a b c d e 1 2 3 4

not surjective

A B

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 26 / 34

slide-27
SLIDE 27
  • B8. Functions

Properties of Functions

Surjective Functions – Examples

Which of these functions are surjective? ◮ f : Z → N0 with f (x) = |x| ◮ g : N0 → N0 with g(x) = x2 ◮ h : N0 → N0 with h(x) =

  • x − 1

if x is odd x + 1 if x is even

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 27 / 34

slide-28
SLIDE 28
  • B8. Functions

Properties of Functions

Composition of Surjective Functions

Theorem If f : A → B and g : B → C are surjective functions then also g ◦ f is surjective. Proof. Consider an arbitary element z ∈ C. Since g is surjective, there is a y ∈ B with g(y) = z. As f is surjective, for such a y there is an x ∈ A with f (x) = y and thus g(f (x)) = z. Overall, for every z ∈ C there is an x ∈ A with (g ◦ f )(x) = g(f (x)) = z, so g ◦ f is surjective.

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 28 / 34

slide-29
SLIDE 29
  • B8. Functions

Properties of Functions

Bijective Functions

A bijective function pairs every element of its domain with exactly

  • ne element of its codomain and every element of the codomain is

paired with exactly one element of the domain. Definition (Bijective Function) A function is bijective (also a one-to-one correspondence or a bijection) if it is injective and surjective.

a b c d 1 2 3 4

bijection

A B

Corollary The composition of two bijective functions is bijective.

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 29 / 34

slide-30
SLIDE 30
  • B8. Functions

Properties of Functions

Bijective Functions – Examples

Which of these functions are bijective? ◮ f : Z → N0 with f (x) = |x| ◮ g : N0 → N0 with g(x) = x2 ◮ h : N0 → N0 with h(x) =

  • x − 1

if x is odd x + 1 if x is even

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 30 / 34

slide-31
SLIDE 31
  • B8. Functions

Properties of Functions

Inverse Function

Definition Let f : A → B be a bijection. The inverse function of f is the function f −1 : B → A with f −1(y) = x iff f (x) = y.

a b c d 1 2 3 4

f

A B a b c d 1 2 3 4

f −1

A B

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 31 / 34

slide-32
SLIDE 32
  • B8. Functions

Properties of Functions

Inverse Function and Composition

Theorem Let f : A → B be a bijection.

1 For all x ∈ A it holds that f −1(f (x)) = x. 2 For all y ∈ B it holds that f (f −1(y)) = y. 3 (f −1)−1 = f

Proof sketch.

1 For x ∈ A let y = f (x). Then f −1(f (x)) = f −1(y) = x 2 For y ∈ B there is exactly one x with y = f (x). With this x

it holds that f −1(y) = x and overall f (f −1(y)) = f (x) = y.

3 Def. of inverse: (f −1)−1(x) = y iff f −1(y) = x iff f (x) = y. Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 32 / 34

slide-33
SLIDE 33
  • B8. Functions

Properties of Functions

Inverse Function

Theorem Let f : A → B and g : B → C be bijections. Then (g ◦ f )−1 = f −1 ◦ g−1. Proof. We need to show that for all x ∈ C it holds that (g ◦ f )−1(x) = (f −1 ◦ g−1)(x). Consider an arbitrary x ∈ C and let y = (g ◦ f )−1(x). By the definition of the inverse (g ◦ f )(y) = x. Let z = f (y). With (g ◦ f )(y) = g(f (y)), we know that x = g(z). From z = f (y) we get f −1(z) = y and from x = g(z) we get g−1(x) = z. This gives (f −1 ◦ g−1)(x) = f −1(g−1(x)) = f −1(z) = y.

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 33 / 34

slide-34
SLIDE 34
  • B8. Functions

Properties of Functions

Summary

◮ injective function: maps distinct elements of its domain to distinct elements of its co-domain. ◮ surjective function: maps at least one elements to every element of its co-domain. ◮ bijective function: injective and surjective → one-to-one correspondence ◮ Bijective functions are invertible. The inverse function of f maps the image of x under f to x.

Malte Helmert, Gabriele R¨

  • ger (University of Basel)

Discrete Mathematics in Computer Science October 19, 2020 34 / 34