1 / 25

Sets & Set Operations Tuples & Relations

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)

Download Presentation

Sets & Set Operations Tuples & Relations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sets & Set OperationsTuples & Relations

  2. 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

  3. Set Equality, Subsets, Supersets • Set Equality • A = B iff A and B have the same elements • A = B  xA  xB • Subsets • A  B  xA  xB (subset or equal) • A  B  A  B  x(xB  xA) (proper subset) • Supersets A  B iff B  A A  B iff B  A

  4. Proofs about Set Equalityand the Empty Set • Prove: A = B iff A  B  B  A A = B  xA  xB definition of set equality  (xA  xB)  (xB  xA) 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  xA definition of subset  F  xA x   is false (for if not there is an element of E in the empty set, contrary to the definition)  T

  5. Set Operations: Intersection Intersection A  B  {x | xA  xB} {1, 2, 3}  {2, 3, 4} = {2, 3} Prove: A  B  A By definition, A  B  A  xAB  xA 1. xA negate conclusion 2. xAB premise 3. xA  xB def of  4. xA 3, simplification 5. xA  xA 1&4, conjunction 6. F 5, contradiction

  6. A B Set Operations: Intersection Intersection A  B  {x | xA  xB} {1, 2, 3}  {2, 3, 4} = {2, 3} Prove: A  B  A By definition, A  B  A  xAB  xA 1. xA negate conclusion 2. xAB premise 3. xA  xB def of  4. xA 3, simplification 5. xA  xA 1&4, conjunction 6. F 5, contradiction A simpler (direct) proof

  7. A B Set Operations: Union Union A  B  {x | xA  xB} {1, 2, 3}  {2, 3, 4} = {1, 2, 3, 4} No duplicates! Prove: A  A  B By definition, A  AB  xA  xA  xB 1. xA premise 2. xA  xB 1, law of addition

  8. A B Set Operations: Set Difference Difference (minus) A – B  {x | xA  xB} {1, 2, 3} – {2, 3, 4} = {1} Remove elements of B from A Prove: A – B  A By definition, A – B A  xA–B  xA 1. x  A – B premise 2. x  A  x  B definition 3. x  Asimplification

  9. A Set Operations: Complement Complement ~ A  E – A  {x | xE  xA} ~{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: ~, , , .

  10. Basic Set Identities Duals:  and E

  11. Basic Set Identities (continued…) Note the similarity with logic

  12. 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

  13. 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

  14. Cartesian Product • A  B = {(x, y) | xA  yB} 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

  15. Cartesian Product (cont’d) • n-fold Cartesian Product A1  …  An = {(x1, …, xn) | xA1  …  xnAn} 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

  16. 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

  17. Relational Data Model

  18. 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.

  19. Relational Database Example snap cr cp cdh csg

  20. 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)

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

More Related