# Designing Classes and Programs Todays topics Sets Definitions Operations Proving Set Identities Reading: Sections 1.6-1.7 Upcoming Functions CompSci 102 Michael Frank 5.1 Introduction to Set Theory (1.6) A set is a new type of structure, representing an unordered collection (group, plurality) of zero or more distinct

(different) objects. Set theory deals with operations between, relations among, and statements about sets. Sets are ubiquitous in computer software systems. All of mathematics can be defined in terms of some form of set theory (using predicate logic). CompSci 102 Michael Frank 5.2 Nave set theory Basic premise: Any collection or class of objects (elements) that we can describe (by any means whatsoever) constitutes a set. But, the resulting theory turns out to be logically inconsistent!

This means, there exist nave set theory propositions p such that you can prove that both p and p follow logically from the axioms of the theory! The conjunction of the axioms is a contradiction! This theory is fundamentally uninteresting, because any possible statement in it can be (very trivially) proved by contradiction! More sophisticated set theories fix this problem. CompSci 102 Michael Frank 5.3 Basic notations for sets For sets, well use variables S, T, U, We can denote a set S in writing by listing all of its elements in curly braces: {a, b, c} is the set of whatever 3 objects are

denoted by a, b, c. Set builder notation: For any proposition P(x) over any universe of discourse, {x| P(x)} is the set of all x such that P(x). CompSci 102 Michael Frank 5.4 Basic properties of sets Sets are inherently unordered: No matter what objects a, b, and c denote, {a, b, c} = {a, c, b} = {b, a, c} = {b, c, a} = {c, a, b} = {c, b, a}. All elements are distinct (unequal);

multiple listings make no difference! If a=b, then {a, b, c} = {a, c} = {b, c} = {a, a, b, a, b, c, c, c, c}. This set contains (at most) 2 elements! CompSci 102 Michael Frank 5.5 Definition of Set Equality Two sets are declared to be equal if and only if they contain exactly the same elements. In particular, it does not matter how the set is defined or denoted. For example: The set {1, 2, 3, 4} = {x | x is an integer where x>0 and x<5 } = {x | x is a positive integer whose square is >0 and <25}

CompSci 102 Michael Frank 5.6 Infinite Sets Conceptually, sets may be infinite (i.e., not finite, without end, unending). Symbols for some special infinite sets: N = {0, 1, 2, } The Natural numbers. Z = {, -2, -1, 0, 1, 2, } The Zntegers. R = The Real numbers, such as 374.1828471929498181917281943125 Blackboard Bold or double-struck font (,,) is also often used for these special number sets. Infinite sets come in different sizes! CompSci 102

More on this after module #4 (functions). Michael Frank 5.7 Venn Diagrams John Venn 1834-1923 CompSci 102 Michael Frank 5.8 Basic Set Relations: Member of

xS (x is in S) is the proposition that object x is an lement or member of set S. e.g. 3N, a{x | x is a letter of the alphabet} Can define set equality in terms of relation: S,T: S=T (x: xS xT) Two sets are equal iff they have all the same members. xS : (xS) CompSci 102 x is not in S Michael Frank 5.9 The Empty Set (null, the empty set) is the unique set

that contains no elements whatsoever. = {} = {x|False} No matter the domain of discourse, we have the axiom x: x. CompSci 102 Michael Frank 5.10 Subset and Superset Relations ST (S is a subset of T) means that every element of S is also an element of T. ST x (xS xT) S, SS. ST (S is a superset of T) means TS. Note S=T ST ST.

S / T means (ST), i.e. x(xS xT) CompSci 102 Michael Frank 5.11 Proper (Strict) Subsets & Supersets ST (S is a proper subset of T) means that ST but T / S . Similar for ST. Example: {1,2} {1,2,3} S T

Venn Diagram equivalent of ST CompSci 102 Michael Frank 5.12 Sets Are Objects, Too! The objects that are elements of a set may themselves be sets. E.g. let S={x | x {1,2,3}} then S={, {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}} Note that 1 {1} {{1}} !!!! CompSci 102

Michael Frank 5.13 Cardinality and Finiteness |S| (read the cardinality of S) is a measure of how many different elements S has. E.g., ||=0, |{1,2,3}| = 3, |{a,b}| = 2, |{{1,2,3},{4,5}}| = ____ If |S|N, then we say S is finite. Otherwise, we say S is infinite. What are some infinite sets weve seen? CompSci 102 Michael Frank 5.14

The Power Set Operation The power set P(S) of a set S is the set of all subsets of S. P(S) : {x | xS}. E.g. P({a,b}) = {, {a}, {b}, {a,b}}. Sometimes P(S) is written 2S. Note that for finite S, |P(S)| = 2|S|. It turns out S:|P(S)|>|S|, e.g. |P(N)| > |N|. There are different sizes of infinite sets ! CompSci 102 Michael Frank 5.15 Review: Set Notations So Far Variable objects x, y, z; sets S, T, U. Literal set {a, b, c} and set-builder {x|P(x)}.

relational operator, and the empty set . Set relations =, , , , , , etc. Venn diagrams. Cardinality |S| and infinite sets N, Z, R. Power sets P(S). CompSci 102 Michael Frank 5.16 Nave Set Theory is Inconsistent There are some nave set descriptions that lead to

pathological structures that are not well-defined. (That do not have self-consistent properties.) These sets mathematically cannot exist. E.g. let S = {x | xx }. Is SS? Therefore, consistent set theories must restrict the language that can be used to describe sets. For purposes of this class, dont worry about it! CompSci 102 Michael Frank Bertrand Russell 1872-1970

5.17 Ordered n-tuples These are like sets, except that duplicates matter, and the order makes a difference. For nN, an ordered n-tuple or a sequence or list of length n is written (a1, a2, , an). Its first element is a1, etc. Contrast with Note that (1, 2) (2, 1) (2, 1, 1). sets {} Empty sequence, singlets, pairs, triples, quadruples, quintuples, , n-tuples. CompSci 102 Michael Frank 5.18

Cartesian Products of Sets For sets A, B, their Cartesian product AB : {(a, b) | aA bB }. E.g. {a,b}{1,2} = {(a,1),(a,2),(b,1),(b,2)} Note that for finite A, B, |AB|=|A||B|. Note that the Cartesian product is not commutative: i.e., AB: AB=BA. Extends to A1 A2 An... CompSci 102 Michael Frank Ren Descartes (1596-1650) 5.19 Review of 1.6

Sets S, T, U Special sets N, Z, R. Set notations {a,b,...}, {x|P(x)} Set relation operators xS, ST, ST, S=T, ST, ST. (These form propositions.) Finite vs. infinite sets. Set operations |S|, P(S), ST. Next up: 1.5: More set ops: , , . CompSci 102 Michael Frank 5.20 Start 1.7: The Union Operator

For sets A, B, theirnion AB is the set containing all elements that are either in A, or () in B (or, of course, in both). Formally, A,B: AB = {x | xA xB}. Note that AB is a superset of both A and B (in fact, it is the smallest such superset): A, B: (AB A) (AB B) CompSci 102 Michael Frank 5.21 Union Examples {a,b,c}{2,3} = {a,b,c,2,3} {2,3,5}{3,5,7} = {2,3,5,3,5,7} ={2,3,5,7} Think The United States of America

includes every person who worked in any U.S. state last year. (This is how the IRS sees it...) CompSci 102 Michael Frank 5.22 The Intersection Operator For sets A, B, their intersection AB is the set containing all elements that are simultaneously in A and () in B. Formally, A,B: AB={x | xA xB}. Note that AB is a subset of both A and B (in fact it is the largest such subset):

A, B: (AB A) (AB B) CompSci 102 Michael Frank 5.23 Intersection Examples {a,b,c}{2,3} = ___ {2,4,6}{3,4,5} = ______ {4} Think The intersection of Main St. and 9th St. is just that part of the road surface that lies on both streets.

CompSci 102 Michael Frank 5.24 Disjointedness Two sets A, B are called disjoint (i.e., unjoined) iff their intersection is empty. (AB=) Example: the set of even integers is disjoint with the set of odd integers. CompSci 102 Michael Frank

Help, Ive been disjointed! 5.25 Inclusion-Exclusion Principle How many elements are in AB? |AB| = |A| |B| |AB| Example: How many students are on our class email list? Consider set E I M, I = {s | s turned in an information sheet} M = {s | s sent the TAs their email address} Some students did both! |E| = |IM| = |I| |M| |IM| CompSci 102

Michael Frank 5.26 Set Difference For sets A, B, the difference of A and B, written AB, is the set of all elements that are in A but not B. Formally: A B : x xA xB x xA xB Also called: The complement of B with respect to A. CompSci 102 Michael Frank 5.27

Set Difference Examples {1,2,3,4,5,6} {2,3,5,7,9,11} = ___________ {1,4,6} Z N { , 1, 0, 1, 2, } {0, 1, } = {x | x is an integer but not a nat. #} = {x | x is a negative integer} = { , 3, 2, 1} CompSci 102 Michael Frank 5.28 Set Difference - Venn Diagram AB is whats left after B

takes a bite out of A Chomp! Set AB Set A CompSci 102 Set B Michael Frank 5.29 Set Complements The universe of discourse can itself be considered a set, call it U. When the context clearly defines U, we say

that for any set AU, the complement of A, written A, is the complement of A w.r.t. U, i.e., it is UA. E.g., If U=N, {3,5} {0,1,2,4,6,7,...} CompSci 102 Michael Frank 5.30 More on Set Complements An equivalent definition, when U is clear: A {x | x A} A A

U CompSci 102 Michael Frank 5.31 Set Identities Identity: A = A = AU Domination: AU = U , A =

Idempotent: AA = A = AA Double complement: A A Commutative: AB = BA , AB = BA Associative: A(BC)=(AB)C , A(BC)=(AB)C CompSci 102 Michael Frank 5.32 DeMorgans Law for Sets Exactly analogous to (and provable from) DeMorgans Law for propositions. A B A B

A B A B CompSci 102 Michael Frank 5.33 Proving Set Identities To prove statements about sets, of the form E1 = E2 (where the Es are set expressions), here are three useful techniques: 1. Prove E1 E2 and E2 E1 separately. 2. Use set builder notation & logical equivalences. 3. Use a membership table. CompSci 102 Michael Frank

5.34 Method 1: Mutual subsets Example: Show A(BC)=(AB)(AC). Part 1: Show A(BC)(AB)(AC). Assume xA(BC), & show x(AB)(AC). We know that xA, and either xB or xC. Case 1: xB. Then xAB, so x(AB)(AC). Case 2: xC. Then xAC , so x(AB)(AC). Therefore, x(AB)(AC). Therefore, A(BC)(AB)(AC). Part 2: Show (AB)(AC) A(BC). CompSci 102

Michael Frank 5.35 Method 2: Membership Tables Just like truth tables for propositional logic. Columns for different set expressions. Rows for all combinations of memberships in constituent sets. Use 1 to indicate membership in the derived set, 0 for non-membership. Prove equivalence with identical columns. CompSci 102

Michael Frank 5.36 Membership Table Example Prove (AB)B = AB. A 0 0 1 1 CompSci 102 B AB ABB AB 0 0 0

0 1 1 0 0 0 1 1 1 1 1 0 0 Michael Frank 5.37 Membership Table Exercise

Prove (AB)C = (AC)(BC). A 0 0 0 0 1 1 1 1 CompSci 102 B 0 0 1 1 0

0 1 1 C AB ABC AC 0 1 0 1 0 1 0 1 BC Michael Frank ACBC

5.38 Review of 1.6-1.7 Sets S, T, U Special sets N, Z, R. Set notations {a,b,...}, {x|P(x)} Relations xS, ST, ST, S=T, ST, ST. Operations |S|, P(S), , , , , S Set equality proof techniques:

CompSci 102 Mutual subsets. Derivation using logical equivalences. Michael Frank 5.39 Generalized Unions & Intersections Since union & intersection are commutative and associative, we can extend them from operating on ordered pairs of sets (A,B) to operating on sequences of sets (A1,,An), or even on unordered sets of sets, X={A | P(A)}. CompSci 102

Michael Frank 5.40 Generalized Union Binary union operator: AB n-ary union: AA2An : ((((A1 A2) ) An) (grouping & order is irrelevant) n Big U notation: UA i i 1

Or for infinite sets of sets: UA A X CompSci 102 Michael Frank 5.41 Generalized Intersection Binary intersection operator: AB n-ary intersection: A1A2An((((A1A2))An) (grouping & order is irrelevant) Big Arch notation: n Ii 1 Ai Or for infinite sets of sets:

A I A X CompSci 102 Michael Frank 5.42 Representations A frequent theme of this course will be methods of representing one discrete structure using another discrete structure of a different type. E.g., one can represent natural numbers as Sets: 0:, 1:{0}, 2:{0,1}, 3:{0,1,2}, Bit strings:

0:0, 1:1, 2:10, 3:11, 4:100, CompSci 102 Michael Frank 5.43 Representing Sets with Bit Strings For an enumerable u.d. U with ordering x1, x2, , represent a finite set SU as the finite bit string B=b1b2bn where i: xiS (i

Michael Frank 5.44