250 likes | 465 Views
Sets & Set Operations Tuples & Relations. Sets. Sets are collections The things in the collection are called elements or members Sets have no duplicates Elements in a set have no order Special sets Universe: all elements under consideration (denoted U or E)
E N D
Sets • Sets are collections • The things in the collection are called elements or members • Sets have no duplicates • Elements in a set have no order • Special sets • Universe: all elements under consideration (denoted U or E) • Empty set: set with no elements (denoted { } or ) • Others: N, Z, R, etc. • Notation { } • Enumeration, e.g., {1, 2, 3}, {1, 2, …}, {1, 2, … , 100}, etc. • Set building: { x | P(x) } all elements in E that satisfy predicate P (e.g., { x | x>5 x<10 } = {6, 7, 8, 9} when E = N • Element of: x A • Cardinality: |A| or #A • Size or number of elements in A, e.g., |{a,b}| = 2
Set Equality, Subsets, Supersets • Set Equality • A = B iff A and B have the same elements • A = B xA xB • Subsets • A B xA xB (subset or equal) • A B A B x(xB xA) (proper subset) • Supersets A B iff B A A B iff B A
Proofs about Set Equalityand the Empty Set • Prove: A = B iff A B B A A = B xA xB definition of set equality (xA xB) (xB xA) P Q (P Q) (Q P) A B B A definition of subset • Prove: A (i.e., is a subset of every set) A x xA definition of subset F xA x is false (for if not there is an element of E in the empty set, contrary to the definition) T
Set Operations: Intersection Intersection A B {x | xA xB} {1, 2, 3} {2, 3, 4} = {2, 3} Prove: A B A By definition, A B A xAB xA 1. xA negate conclusion 2. xAB premise 3. xA xB def of 4. xA 3, simplification 5. xA xA 1&4, conjunction 6. F 5, contradiction
A B Set Operations: Intersection Intersection A B {x | xA xB} {1, 2, 3} {2, 3, 4} = {2, 3} Prove: A B A By definition, A B A xAB xA 1. xA negate conclusion 2. xAB premise 3. xA xB def of 4. xA 3, simplification 5. xA xA 1&4, conjunction 6. F 5, contradiction A simpler (direct) proof
A B Set Operations: Union Union A B {x | xA xB} {1, 2, 3} {2, 3, 4} = {1, 2, 3, 4} No duplicates! Prove: A A B By definition, A AB xA xA xB 1. xA premise 2. xA xB 1, law of addition
A B Set Operations: Set Difference Difference (minus) A – B {x | xA xB} {1, 2, 3} – {2, 3, 4} = {1} Remove elements of B from A Prove: A – B A By definition, A – B A xA–B xA 1. x A – B premise 2. x A x B definition 3. x Asimplification
A Set Operations: Complement Complement ~ A E – A {x | xE xA} ~{1, 2, 3} = {4} if E= {1, 2, 3, 4} Prove: A ~A = A ~A = A ~A A ~A set equality A ~A T is subset of every set A ~A identity x A x ~A x def of and x A x E x A x def of ~ F x comm., contradict., dominat. T Note: Unary operators have precedence over binary operators. Use parentheses for the rest. Possible to define precedence: ~, , , .
Basic Set Identities Duals: and E
Basic Set Identities (continued…) Note the similarity with logic
A B Example: Set Laws • Absorption A (A B) = A A (A B) = A • Venn Diagram “Proof” Prove: A (A B) = A A (A B) = (A ) (A B) ident. = A ( B) distrib. = A dominat. = A ident. Every set is a predicate Every predicate is a set
Tuples • Things (usually a small number) arranged in order • 2-tuples • Also known as pairs: (x, y) • Ordered: (x, y) (y, x) unless x = y • n-tuples • n elements in order: (x1, x2, …, xn) • Typically, elements are taken from known sets • x females, y males (Mary, Jim) – might mean: Mary and Jim are a married couple • x people, y cars (Mary, red sports car17) – might mean: Mary owns red sports car17 • x, y, z integers (3, 4, 7) – might mean: 3 + 4 = 7
Cartesian Product • A B = {(x, y) | xA yB} e.g., A = {1, 2} B = {a, b, c} A B = {(1, a), (1, b), (1, c), (2, a), (2, b), (2, c)} • |A B| = |A| · |B| = 2 · 3 = 6
Cartesian Product (cont’d) • n-fold Cartesian Product A1 … An = {(x1, …, xn) | xA1 … xnAn} e.g., A = {1, 2} B = {a, b, c} C = {, } A B C = {(1,a,), (1,a,), (1,b,), (1,b,), (1,c,), (1,c,), (2,a,), (2,a,), (2,b,), (2,b,), (2,c,), (2,c,)} • Can get large: A = set of students at BYU (30,000) B = set of BYU student addresses (10,000) C = set of BYU student phone#’s (60,000) |A| |B| |C| = 1.8 1013
Relations • Relation • Subset of the cross product • Not necessarily a proper subset • R A B or R A B C • Examples: • A = {1, 2} & B = {a, b, c} R = {(1, a), (2, b), (2, c)} • A = {1, 2} & B = {a, b, c} & C = {, } R = {(1, a, ), (2, c, )} • Marriage: subset of the cross product of males and females
Relational Database • A persistent collection of relations • Information about various kinds of objects (persons, places, things, events) • Each relation holds information about various kinds of objects (persons, places, things, events, etc.) • Each relation, or table, is characterized by a set of attributes or properties from a domain e.g., person(SSN, Name, Address, Phone) • Each individual object, or record, is a tuple of values e.g., (123-45-6789, Pat Carter, 12 Main, 555-5555) • Set of records make up the relation, i.e., subset of the cross-product of the attributes’ domains Assume DB exists – in reality, create tables, insert tuples, etc. DBMS – manage storage, integrity/security, crash recovery, etc.
Relational Database Example snap cr cp cdh csg
Relational Schemas • Each table has a schema • Name • Set of attributes • Domain for each attribute • Example: • Names: snap, cp, cdh, cr, csg • Attributes: table headers • Domains: studentID: integer all the rest are strings, but we could be more specific (e.g. time, day, grade)
Relational Tables • Tables consist of n-tuples, where n is the arity or degree of the relation (i.e., the number of attributes) • Each n-tuple t D1 D2 … Dn, where the Di’s are the domains e.g., a 3-tuple t of cdh is an element string string string or string day time or course day time depending on how specific we make our domains • A table is a set of tuples, all with the same schema e.g., cdh Dcourse Dday Dhour
Tables & Keys • Because a table is a set of tuples, there are no duplicates • There is always a set of attributes whose values uniquely identify a tuple (even if it is all of them) • A set of attributes whose values always uniquely identify a tuple constitutes a key • Typically, one or two attributes make up a key • Keys must be declared: we cannot assume uniqueness • e.g., Name is not a key; there could be another C. Brown • Some systems add a tuple identifier as the key
Keys Examples Table Key • snap StudentID Name, Address, Phone (possible key?) • cp Course Prerequisite • cdh Course Day Hour Can a course meet twice on the same day? If not: Course Day • cr Course Room Does a course always meet in the same room? If so: Course • csg Course StudentID
Predicates and Tuples • A table name for tuples of arity n is an n-place predicate • cdh('CS101','M','9AM') • Asserts that CS101 meets on Monday at 9:00 am • Predicates give each tuple a meaning in the ordinary sense of predicates • The subset of D1 D2 … Dn present in the database are those assigned T; all others are assigned F (CWA) • Interpretation: • Domain • For each predicate and every substitution, T or F Every relation is a set, every set is a predicate, hence every relation is a predicate And vice-versa
Database Tuples • Database tuples (strictly speaking) are not true subsets of D1 D2 … Dn because we can alter the column order if we do so “correctly” • More properly defined, a tuple in a relation is a set of attribute-value pairs • e.g. {(Course, 'CS101'), (Day, 'M'), (Hour, '9AM') } = {(Day, 'M'), (Course, 'CS101'), (Hour, '9AM') } • Normally, we factor out the attribute and fix the order • Implication: we can interchange columns cr = Course Room = Room Course CS101 Turing Aud. Turing Aud. CS101 EE200 25 Ohm Hall 25 Ohm Hall EE200 PH100 Newton Lab. Newton Lab. PH100