34
CSCE 222 Discrete Structures for Computing David Kebo Houngninou Sets and Functions Chapter 2

Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

CSCE 222Discrete Structures for Computing

David Kebo Houngninou

Sets and Functions

Chapter 2

Page 2: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Outline

• Sets• Sets operations• Relations• Functions

CSCE 222 Discrete Structures for Computing 3Houngninou

Sets

What is a set?

Houngninou CSCE 222 Discrete Structures for Computing 4

Page 3: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Applications

• Combinations: unordered collections of objects• Relations: sets of ordered pairs represent relationships

between objects• Graphs: sets of ordered or unordered pairs also represent

relationships • Functions

Houngninou CSCE 222 Discrete Structures for Computing 5

The Language of Sets

Houngninou CSCE 222 Discrete Structures for Computing

Let S be the set of all integers between 1 and 5, inclusive S = {1,2,3,4,5}We want to determine if x is an element of set S: x Î S?

Let x = 3; is x Î S? YesNow let x = 10; is x Î S? No, so we denote this as x Ï S

6

Page 4: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

The Language of Sets

Houngninou CSCE 222 Discrete Structures for Computing

Let set T = {5, 4, 3, 2, 1} and S = {1, 2, 3, 4, 5}is set T = set S?

Do sets T and S contain the same elements?

Does {2} = 2?{2} is a set with the element 2, while 2 is the number 2

7

Common Sets

Houngninou CSCE 222 Discrete Structures for Computing

C = set of complex numbersR = set of real numbersR+ = set of positive real numbers

Q = set of rational numbersZ = integers = {…,-3,-2,-1,0,1,2,3,…}Z⁺ = positive integers = {1,2,3,…}N = natural numbers = {0,1,2,3….}

8

Page 5: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Set-Roster Notation

Houngninou CSCE 222 Discrete Structures for Computing

Set S = {1,2,3,4,5} For a small set, we can write all the elements in the set. This is the set-roster notation (roster of elements).

For a large set, we can write A = {1,2,3,….100}.That is all integers from 1 to 100, inclusive.

For some sets (e.g. real numbers), the roster notation is impractical.

10

Set-Builder Notation

Houngninou CSCE 222 Discrete Structures for Computing

We define the set of all elements x in set S such that P(x) is true as: {x Î S | P(x)}1. x Î S means “elements x in set S”2. | means “such that”3. P(x) means that property P(x) is satisfied (true)

e.g. {x Î Z | 2 < x ≤ 5} = {3, 4, 5}

11

Page 6: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Set Equality

Houngninou CSCE 222 Discrete Structures for Computing

When are two sets equal?

12

Set Equality

Houngninou CSCE 222 Discrete Structures for Computing 13

B = {1,2,3,4,5}D = {1,2,3,4,5}E = {1,2,3,4}

Is B equal to D?

Is B equal to E?

Page 7: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Set Equality – Formal Definition

Given sets A and B, A equals B (A = B) if and only if:Every element of A is in B, andEvery element of B is in A

∀x [x ∈ A ⟷ x ∈ B]

Houngninou CSCE 222 Discrete Structures for Computing 14

( ) ( )ABBABA ÍÙÍÛ=

The Empty Set { } or ⌀

Houngninou CSCE 222 Discrete Structures for Computing

⌀ or { } is the empty set, the set that has no elements.The empty set has a cardinality of 0.

Note: a set with one element is called the singleton set.

Do not get confused! ⌀ or { } means the empty set.{⌀} is a singleton set containing the empty set.

15

Page 8: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Venn diagrams are visual representations of sets.The Universal set (all objects under consideration) is a rectangle. Particular sets are circles inside the rectangle.

Venn Diagrams

Houngninou CSCE 222 Discrete Structures for Computing 16

U

Set A is a subset of set B if every element of A is in B.Notation: A ⊆ B

Subsets

Houngninou CSCE 222 Discrete Structures for Computing 17

U

A

B

Page 9: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Subsets

Houngninou CSCE 222 Discrete Structures for Computing

Assume we have the following sets:A = Z (set of integers)B = {1,2,3,4,5} (set-roster notation)C = {x Î Z | 2 < x < 5} (set-builder notation)

All the elements of set B are in set A; B is a subset of A: B Í A

B Í A means that for all x, if x Î B, then x Î A

What about set C ?

18

Set A is a proper subset of B if A is a subset of B but is not equal to B. Notation: A ⊂ BEvery element of A is in B and there is at least one element of B that is not in A.

Proper subsets

Houngninou CSCE 222 Discrete Structures for Computing 19

U

A

B

Page 10: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Subset vs. Proper Subset

Houngninou CSCE 222 Discrete Structures for Computing

Assume we have the following sets:B = {1,2,3,4,5}D = {1,2,3,4,5}E = {1,2,3,4}Note that D = B

Recall the definition of a subset: ∀x, if x Î D, then x Î B.We can state that D is a subset of B.We can also state that B is a subset of itself.However, E is a proper subset of B since there is at least one element in B that is not in E (5).

20

Power set

Houngninou CSCE 222 Discrete Structures for Computing

The power set of a set S is the set of all subsets of S.Notation: P(S)e.g. • The power set of ⌀ (cardinality 0) is {⌀} (cardinality 1)• The power set of {0} (cardinality 1) is {⌀, {0}} (cardinality 2)• The power set of {0,1} (cardinality 2) is {⌀,{0},{1},{0,1}}

(cardinality 4)

Note: if |S| = n, then |P(S)| = 2n

21

Page 11: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Operations on Sets

Earlier, we discussed operations on statement variables (∨, ∧,∼)

There are similar operations that we perform on sets:• Union• Intersection• Complement• Difference

Houngninou CSCE 222 Discrete Structures for Computing 22

Sets Union

• The union of sets A and B is the set of all elements that are in at least one of A or B

• Denoted as A ∪B

Houngninou CSCE 222 Discrete Structures for Computing 23

Shaded regionrepresents A È B.

Page 12: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Sets Intersection

• The intersection of sets A and B is the set of all elements that common to both A or B

• Denoted as A ∩B

Houngninou CSCE 222 Discrete Structures for Computing 24

Shaded regionrepresents A Ç B.

Sets Difference

• The difference B minus A is the set of all elements that are in B and not A

• Denoted as B - A

Houngninou CSCE 222 Discrete Structures for Computing 25

Shaded regionrepresents B – A.

Page 13: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Set Complement

• The complement of A is the set of all elements in U that are not in A

• Denoted as Ac

Houngninou CSCE 222 Discrete Structures for Computing 26

Shaded regionrepresents Ac.

Formal Definitions of Set Operators

Houngninou CSCE 222 Discrete Structures for Computing 27

{ }{ }{ }{ }AxUxA

AxBxUxABBxAxUxBABxAxUxBA

c ÏÎ=

ÏÙÎÎ=-ÎÙÎÎ=ÇÎÚÎÎ=È

||||

Page 14: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Set identities are similar to the logical equivalences we learned in Ch.1. Here are De Morgan’s Laws for sets:

We also have identity, domination, idempotent, double complementation, commutative, associative, absorption, complement.Read Table 1 in Section 2.2, page 130

Set Identities

Houngninou CSCE 222 Discrete Structures for Computing 29

Relations

Page 15: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Cartesian Product

Houngninou CSCE 222 Discrete Structures for Computing

The Cartesian product of sets A and B is the set of all ordered pairs (a, b) where a is in set A and b is in set B

Or:

Given A = {3,5,7}, B = {2,4}A⨯B = {(3,2), (3,4), (5,2), (5,4), (7,2), (7,4)}B⨯A = {(2,3), (2,5), (2,7), (4,3), (4,5), (4,7)}B⨯B = {(2,2), (2,4), (4,2), (4,4)}

If |A| = n and |B| = m, What is |A × B|?

( ){ }BbAabaBA ÎÎ=´ and |,

31

Relations

Houngninou CSCE 222 Discrete Structures for Computing

Given two sets A and B: A relation R from A to B is a subset of A⨯B.

R Í A⨯B

Given an ordered pair (x,y) in A⨯B:x is related to y by R iff (x,y) is in R

A is the domain of R and B is the co-domain of R

32

Page 16: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Arrow Diagram of a Relation

Houngninou CSCE 222 Discrete Structures for Computing

We can also represent relations graphically using an arrow diagram:

Represent sets as regions, with elements of sets as points in region.

For each x in the domain region and y in the co-domain region, draw an arrow from x to y iff (x,y) Î R

34

Let A = {1, 2, 3} and B = {1, 3, 5} For all (x, y ) ∈ A ´ BLet’s define relations S and T from A to B as follows:

Draw arrow diagrams for S and T.

Example: Arrow Diagrams of Relations

Houngninou CSCE 222 Discrete Structures for Computing 35

Page 17: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Example: Solution

It is possible to have arrows from the same element of A pointing to different directions.It is possible to have an element of A that does not have an arrow coming out of it.

Houngninou CSCE 222 Discrete Structures for Computing 36

Functions

Page 18: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Functions

Houngninou CSCE 222 Discrete Structures for Computing

A function f from set A to set B is an assignment of exactly oneelement of B to each element of A. f: A → B and f(a) = b.

Ways to specify functions:• give a formula, e.g. f(x) = x + 1• give an algorithm to compute the function

Every function is a relation but not vice versa!

38

Function Important Terminologies

For a function f: A→B with f(a) = b

• A is the domain• B is the codomain• b is the image of a • a is a preimage of b (preimage is not necessarily unique) • B is the range of f (images of all elements of A)

Houngninou CSCE 222 Discrete Structures for Computing 42

Page 19: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Function Terminologies

Suppose f: Z → Z is f(x) = x2

• Domain is Z (set of integers)• Codomain is Z• Image of 3 is 9• Preimages of 16 are 4 and -4• Range is the set of all integers that are perfect squares

Houngninou CSCE 222 Discrete Structures for Computing 43

Arrow Diagrams

An arrow diagram defines a function if the diagram has the following properties:

1. Every element of X has an arrow coming out of it.2. No element of X has two arrows to two different

elements of Y.

Houngninou CSCE 222 Discrete Structures for Computing 44

Page 20: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Example

Houngninou CSCE 222 Discrete Structures for Computing 45

X = {a, b, c}, Y = {1, 2, 3, 4}We define a function f from X to Y as follow:

Houngninou CSCE 222 Discrete Structures for Computing 46

What is the domain and co-domain of f?

The domain X of f is {a, b, c}The co-domain Y of f is {1, 2, 3, 4}

Page 21: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Houngninou CSCE 222 Discrete Structures for Computing 48

What is the range of f ?

The range of f is defined as {y ∈ Y | y = f(x), for x in X}i.e., what values of Y are mapped to by X?The range is {2,4}

Houngninou CSCE 222 Discrete Structures for Computing 49

Is c a preimage of 2? Yes, since f(c) = 2

Is b a preimage of 3?No, since f(b) ≠ 3

Page 22: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Function Equality

• Given functions F: X→Y and G: X→Y• Then:

Houngninou CSCE 222 Discrete Structures for Computing 52

XxxGxFGF Î"=Û= )()(

Classes of functions

• One-to-One Functions• Onto Functions• One-to-One Correspondence• Inverse Functions

Houngninou CSCE 222 Discrete Structures for Computing 54

Page 23: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

One-to-One Functions

Houngninou CSCE 222 Discrete Structures for Computing 55

Suppose we have the following two functions

(1)

(2)

One-to-One Functions

Houngninou CSCE 222 Discrete Structures for Computing 56

For function (1), each x maps to the same y: F(x1) = F(x2)

(1)

Page 24: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

One-to-One Functions

Houngninou CSCE 222 Discrete Structures for Computing 57

(2)

For function (2), each x maps to a unique y: F(x1) ≠ F(x2)

Function (2) is called a one-to-one function.

One-to-One Functions: Definition

• Let F be a function from set X to set Y• F is one-to-one iff for all elements x1 and x2 in X:

– If F(x1) = F(x2), then x1 = x2, or– If x1 ≠ x2, then F(x1) ≠ F(x2)

• Symbolically:

Also called an “injective function” or an “injection”

Houngninou CSCE 222 Discrete Structures for Computing 58

( ) ( )( ) ( )212121 ,,one-to-one is : xxxFxFXxxYXF =®=Î"Û®

Page 25: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Onto Functions

Houngninou CSCE 222 Discrete Structures for Computing 61

Suppose we have the following two functions

(1)

(2)

Onto Functions

Houngninou CSCE 222 Discrete Structures for Computing 62

For function (1), there is at least one y that is not F(x) for any x

(1)

Page 26: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Onto Functions

Houngninou CSCE 222 Discrete Structures for Computing 63

For function (2), every y = F(x) for at least one xFunction (2) is called an onto function.

(2)

Onto Functions Definition

• Let F be a function from set X to set Y• F is onto iff

– Given any y in Y, there is at least one x in X where y = F(x)• Symbolically:

Also called a surjective function or a surjection

Houngninou CSCE 222 Discrete Structures for Computing 64

yxFXxYyYXF =Î$Î"Û® )(|,onto is :

Page 27: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

One-to-One Correspondence

Houngninou CSCE 222 Discrete Structures for Computing 67

Suppose we have the following function

Is F one-to-one? Yes, each F(x) has a distinct x Is F onto? Yes, for all y, there is an x such that y = F(x)Therefore, there is a one-to-one correspondence from X to Y

One-to-One Correspondence• Let F be a function from set X to set Y

• F has one-to-one correspondence from set X to set Y iff F is one-to-one, and F is onto

Also called a bijection.

Houngninou CSCE 222 Discrete Structures for Computing 68

Page 28: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Inverse Function

• Suppose function F has a one-to-one correspondence from set X to set Y

• Then there is a function from Y to X that “undoes” the action of F

• This function is called the inverse function for F

Houngninou CSCE 222 Discrete Structures for Computing 70

Inverse Function Definition

Given F: X→Y that is a one-to-one correspondence

There is a function F-1: Y→X where

F-1(y) is called the inverse function for F

Houngninou CSCE 222 Discrete Structures for Computing 71

)()(1 xFyxyF =Û=-

Page 29: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Inverse Function Arrow Diagram

Houngninou CSCE 222 Discrete Structures for Computing 72

Inverse Function Example

F(x) = 4x – 5

1. Each x maps to a unique y, so F is one-to-one

2. Every y = F(x) for some x, so F is onto

Thus, F has a one-to-one correspondence

Therefore, we can determine F-1

Houngninou CSCE 222 Discrete Structures for Computing 73

Page 30: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Inverse Function Example

Houngninou CSCE 222 Discrete Structures for Computing 74

45)(

4554)(

1 +==

=+-==

- yxyF

xyxyxF

Floor and Ceiling

Floor and ceiling functions work on real numbers For a real number x:• The floor of x rounds x down to the nearest integer value• The ceiling of x rounds x up to the nearest integer value

Houngninou CSCE 222 Discrete Structures for Computing 75

Page 31: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Houngninou CSCE 222 Discrete Structures for Computing 76

x = 3.141592654

Ceiling(x) = 4

Floor(x) = 3

Round UP to nearest integer

Round DOWN to nearest integer

Floor Definition

Houngninou CSCE 222 Discrete Structures for Computing 77

Page 32: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Ceiling Definition

Houngninou CSCE 222 Discrete Structures for Computing 78

Houngninou CSCE 222 Discrete Structures for Computing 79

x = - 3.141592654

Ceiling(x) = -3

Floor(x) = -4

Round UP to nearest integer

Round DOWN to nearest integer

Page 33: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

Examples

Houngninou CSCE 222 Discrete Structures for Computing 80

1. How many bytes are needed to store 100 bits of data? ⌈100/8⌉ = ⌈12.5⌉ = 13 bytes

2. A cell is a unit of 53 bytes. If the data rate is 500 kilobits per second, how many complete cells can be transmitted in 1 minute?

In 1 minute, 500⨯1000⨯60 = 30Mbits can be transmitted.Each cell is 53⨯8 = 424bits.⌊30M/424⌋ = 70,754 complete cells

Application to C/C++/Java

Houngninou CSCE 222 Discrete Structures for Computing 81

double x, x1, x2;

x1 = floor(x); // floor of xx2 = ceil(x); // ceiling of x

Page 34: Sets and Functionscourses.cse.tamu.edu/davidkebo/csce-222/lectures/CSCE222... · 2019-09-17 · •Relations:sets of ordered pairs represent relationships between objects •Graphs:sets

End

Houngninou CSCE 222 Discrete Structures for Computing 82