200 likes | 607 Views
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
E N D
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 • Other sets may be constructed using set comprehension, the power set operator, and the cartesian product • Use extension for listing elements • Examples
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
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 {}
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
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}
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 =
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}
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
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}
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}
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}
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}
Tuple membership • If a1…an are sets and then • Example
Tuple equality • if x1 = y1 and x2 = y2 … xn = yn then (x1,x2,..,xn) = (y1,y2,…,yn)
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
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 ….