20
Sets

Sets

  • Upload
    hewitt

  • View
    47

  • Download
    0

Embed Size (px)

DESCRIPTION

Sets. Sets. A set is a well-defined collection of values of the same kind (objects) Objects can be numbers, people, letters, days, may be sets themselves Examples. Sets. Small sets may be introduced by listing their elements - PowerPoint PPT Presentation

Citation preview

Page 1: Sets

Sets

Page 2: Sets

Sets

• A set is a well-defined collection of values of the same kind (objects)• Objects can be numbers, people, letters, days, may be sets

themselves• Examples

Page 3: Sets

Sets

• Small sets may be introduced by listing their elements• Other sets may be constructed using set comprehension, the power

set operator, and the cartesian product• Use extension for listing elements• Examples

Page 4: Sets

Membership of a set

• We write x s to indicate that x is an element of set s• We write (x s ) as x s• Examples

3 Primes

6 Primes

Page 5: Sets

Equality of sets & Null set

• Two sets of values of the same kind are equal if and only if they have the same members

• Examples

s == {2,2,5,5,3}

t == {2,3,5}

s = t• A null set is set with no members• Denoted as or {}

Page 6: Sets

Subsets

• One set is said to be a subset of another if all the members of the one are also the members of the other

• Example

A is a set of all primes and B == {2,3,5} then

B is a subset of A denoted by B A

Page 7: Sets

Union of sets

• Two sets of the same kind, A and B, whose members are the members of A and B together

• Denoted by A B• Example:

A == {1,2,3,4,5} B == {2,4,5,6,7}

A B == {1,2,3,4,5,6,7}

Page 8: Sets

Intersection

• Two sets of the same kind, A and B, whose members are the members that A and B have in common

• Denoted by A B• Example

A == {1,2,3,4,5} B == {2,4,6,7}

A B == {2,4}• Two sets are disjoint if they have no member in common

C == {6,7,8} A C =

Page 9: Sets

Set Difference

• Two sets A, B of the same kind, whose members are the members of A but not of B

• Denoted by A\B• Example

A == {1,2,3,4,5} B == {2,4}

A\B == {1,3,5}

Page 10: Sets

Set Comprehension

• Defining a set by stating a property that distinguishes its member from other values of the same kind

• Suppose D denotes some declarations, P denotes a predicate constraining the value, and E denotes an expression denoting a term, then

{D|P•E} is called set comprehension term• Denotes a set of values consisting of all values of the

term E for everything declared in D satisfying the constraint P

Page 11: Sets

Set Comprehension

• Example

{x : | x 5 • x2} denotes the same set as {0,1,4,9,16,25}

• We can omit heavy dot {x : | x 5} • We can omit constraint and constraint bar {x : • x2}

Page 12: Sets

Defining sets using predicates

• BigCountries == {c : country | c has more than 40 million inhabitants}

• MultipleOfSixes == {n : | m : n = m 6}• BiggestCities == {macropolis : city |

co : country macropolis is in co ci : city ci is in co ci is macropolis

macropolis is bigger than ci}

Page 13: Sets

Power Sets

• The power set of a set A is the set of all its subsets.• Denoted by pA --- power set of A• Example

A == {x,y}

pA == {, {x}, {y}, {x,y}}• A B has the same meaning as A pB • Example• X == {1}, pX == {, {1}}, ppX == {, {}, {{1}}, {, {1}}}• Exercise : list the power set for {1,2,3}

Page 14: Sets

Cartesian Product

• Countries == {UK, USA, Malaysia, Iran …}• Capitals == {London, Washington, KL, Tehran, …}• CountriesAndCapitals == {(UK,London), (USA,Washington),

(Malaysia,KL), (Iran,Tehran),…}

• CountriesAndCapitals == {co : country; ca : city | ca is the capital of co}•

Page 15: Sets

Tuple membership

• If a1…an are sets and

then

• Example

Page 16: Sets

Tuple equality

• if x1 = y1 and x2 = y2 … xn = yn then (x1,x2,..,xn) = (y1,y2,…,yn)

Page 17: Sets

Component selection

Page 18: Sets

Types

Page 19: Sets

Sets of numbers

• Integers, denoted by , is the set of positive and negative whole numbers including zero. Its type is p

• Natural numbers, denoted by , is the set of whole numbers from zero onwards. The natural numbers are a subset of the integers. Its type is p

• Restricted set of numbers, whose numbers lying in a certain range is called a subrange, eg. 1..4 denotes {1,2,3,4}

• Finite set : 1..4• Infinite set : , ‘the set of natural numbers that are prime’• If X denotes some finite set, then the number of elements in the set

is called its cardinality or size, and denoted by #X

Page 20: Sets

Other forms of type

• Types of ordered pair (cartesian product) involving integer • In general if a specification has the basic types X and Y, we can

have a list of other types:

pX, pY, ppX, ppY, ….

XX, XY, YX, YY

(pX) (pX), p(XX), (pX) Y ….