View
221
Download
0
Category
Tags:
Preview:
Citation preview
Sets & Set OperationsTuples & 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)– Empty set: set with no elements (denoted { } or )– Others: NN, ZZ, RR, 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 = NN
• 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)
• SupersetsA B iff B A
A B iff B A
Proofs about Set Equalityand the Empty Set
• Prove: A = B iff A B B AA = 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
IntersectionA B {x | xA xB}{1, 2, 3} {2, 3, 4} = {2, 3}
Prove: A B ABy definition, A B A xAB xA
1. xA negate conclusion2. xAB premise3. xA xB def of 4. xA 3, simplification5. xA xA 1&4, conjunction6. F 5, contradiction
Set Operations: Intersection
IntersectionA B {x | xA xB}{1, 2, 3} {2, 3, 4} = {2, 3}
Prove: A B ABy definition, A B A xAB xA
1. xA negate conclusion2. xAB premise3. xA xB def of 4. xA 3, simplification5. xA xA 1&4, conjunction6. F 5, contradiction
A simpler (direct) proof
A B
Set Operations: Union
UnionA B {x | xA xB}{1, 2, 3} {2, 3, 4} = {1, 2, 3, 4}No duplicates!
Prove: A A BBy definition, A AB xA xA xB
1. xA premise2. 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 ABy definition, A – B A xA–B xA
1. x A – B premise
2. x A x B definition
3. x A simplification
A B
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: ~, , , .
A
Basic Set IdentitiesSet Algebra Name
A ~A = EA ~A =
Complementation lawExclusion law
A E = AA = A
Identity laws
A E = EA =
Domination laws
A A = AA A = A
Idempotent laws
Duals: and E
Basic Set Identities (continued…)Set Algebra Name
~(~A) = A Double Compl.
A B = B A A B = B A
Commutative laws
(A B) C = A (B C) (A B) C = A (B C)
Associative laws
A (B C) = (A B) (A C) A (B C) = (A B) (A C)
Distributive laws
~ (A B) = ~A ~B~ (A B) = ~A ~B
De Morgan’s laws
Note the similarity with logic
Example: Set Laws• Absorption
A (A B) = AA (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.
A B
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 Data Model
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 valuese.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.
StudentID Name Address Phone
12345 C. Brown 12 Apple St. 555-1234
67890 L. Van Pelt 34 Pear Ave. 555-5678
22222 P. Patty 56 Grape Blvd. 555-9999
33333 Snoopy 12 Apple St. 555-1234
Course StudentID Grade
CS101 12345 A
CS101 67890 B
EE200 12345 C
EE200 22222 B+
EE200 33333 B
CS101 33333 A-
PH100 67890 C+
Course Day Hour
CS101 M 9AM
CS101 W 9AM
CS101 F 9AM
EE200 Tu 10AM
EE200 W 1PM
EE200 Th 10AM
PH100 Tu 11AM
Course Prerequisite
CS101 CS100
EE200 EE005
EE200 CS100
CS120 CS101
CS121 CS120
CS205 CS101
CS206 CS121
CS206 CS205
Course Room
CS101 Turing Aud.
EE200 25 Ohm Hall
PH100 Newton Lab.
Relational Database Examplesnap cr
cdh csgcp
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 domainse.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 schemae.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 ExamplesTable Key
• snap StudentIDName, Address, Phone (possible key?)
• cp Course Prerequisite
• cdh Course Day HourCan a course meet twice on the same day?If not: Course Day
• cr Course RoomDoes 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
Recommended