cs1022 computer programming & principles lecture 2 relations
TRANSCRIPT
CS1022 Computer Programming &
Principles
Lecture 2Relations
Plan of lecture• Equivalence relations• Set partition• Partial order• Total order• Database management systems
2CS1022
Equivalence relation• A relation R on A which is– Reflexive (x A (x, x) R), – Symmetric ((x, y) R) ((y, x) R)), and– Transitive (((x, y) R and (y, z) R) (x, z) R)
is called an equivalence relation• Equivalence relation generalises/abstracts equality– Pairs share some common features
• Example:– Relation “has same age” on a set of people – related
people belong to “same age sub-group”– Natural way to split up elements (partition)
3CS1022
Set partition (1)• Underlying set A of equivalence relation R can be
split into disjoint (no intersection) sub-sets– Elements of subsets are all related and equivalent to
each other– “Equivalent” is well-defined (and can be checked)
• Very important concept– Underpins classification systems
4CS1022
Set partition (2)• A partition of a set A is a collection of non-empty
subsets A1, A2, , An, such that– A A1 A2 An
– i, j, 1 i n, 1 j n, i j, Ai Aj
• Subsets Ai, 1 i n, are blocks of the partition• Sample Venn diagram: 5 blocks
5CS1022
A1
A2
A3A4 A5
A • Blocks do not overlap• Their intersection is empty
Set partition (3)• Subsets/partitions consist of related elements of A • Equivalence class Ey, of any y A, is the set
Ey z A : z R y • Theorem: – Let R be an equivalence relation on a non-empty set A– The distinct equivalence classes form a partition of A
• Proof in 4 parts:1. Show that equivalent classes are non-empty subsets of A2. Show that if x R y then Ex Ey
3. Show that A Ex1 Ex2
Exn
4. Show that for any two Ex and Ey, Ex Ey 6CS1022
Ey is the set of things that are in the equivalence class with respect to y. See later.
• Example: relation R on R (real numbers) defined asx R y if and only if x y is an integer (Z).
x y is an integer is an equivalence relation.(x y) Z
• Proof:– Since x x 0 for any real number x, then R is reflexive– If x y is an integer then y x (x y) is the negative of an
integer, which is an integer, hence R is symmetric– If x y and y z are integers then x z (x y) + (y z) is
the sum of two integers, which is another integer, so R is transitive
– Therefore R is an equivalence relation
Set partition (4)
7CS1022Uses a bit of number theory. Examples: 7-5 = -(5-7); (7-5)+(5-2) = (7-2).
• Our previous example isy R, Ey z R : (z y) Z
• We can obtain the following equivalence classes:E0 z R : (z 0) Z Z (all integers)
E½ z R : (z ½) Z , 1½, ½, ½, 1½, 2½,
E2 z R : (z 2) Z , 1 2, 2, 1 2, 2 2,
How would it work if we have a set of ordered pairs of names and ages, e.g. {(Jill, 35), (Bill, 37), (Phil, 35) (Mary, 35) (Will, 37)}, and the relation is 'same age as'?
Set partition (5)
8CS1022
R is the set of reals. Z is the set of integers.
• A relation R on A which is– Reflexive (x A (x, x) R), – Anti-symmetric ((x, y) R) and (x y)) ((y, x) R)), and– Transitive (((x, y) R and (y, z) R) (x, z) R)
Is a partial order– Sets on which partial order is defined are “posets”
• Partial orders help us defining precedence– Decide when an element precedes another– Establish an order among elements
• Sample partial orders– “” on the set R of real numbers– “” on subsets of some universe set
Partial order (1)
9CS1022"is taller than" a partial order?
• If R is a partial order on A and x R y, x y, we call – x a predecessor of y– y a successor of y
• An element may have many predecessors• If x is a predecessor of y and there is no z, x R z and
z R y, then x is an immediate predecessor of y– We represent this as x y
Partial order (2)
10CS1022
• Immediate predecessors graphically represented as a Hasse diagram:– Vertices are elements of poset A– If x y, vertex x is placed below y and joined by edge
• A Hasse diagram has complete information about original partial order– Provided we infer which elements are predecessors of
others, by working our way upwards on chain of edges
Partial order (3)
11CS1022
6
12 18
32
1
• Example: relation “is a divisor of” on setA = 1, 2, 3, 6, 12, 18
– Defines a partial order• Table of predecessors & immediate predecessors
Partial order (4)
12CS1022
element predecessors immediatepredecessors
1 none none2 1 13 1 16 1, 2, 3 2, 3
12 1, 2, 3, 6 618 1, 2, 3, 6 6 Why the branch?
• A total order on a set A is a partial ordering in which every pair of elements are related.
• The Hasse diagram of a total order is a long chain• Examples of total orders:– Relation “” on Real numbers– Lexicographical ordering of words in a dictionary
• In computing:– Sorting algorithms require total ordering of elements– Posets with minimal/maximal elements useful too
Total order
13CS1022
• Data with lifespan– Data stored in variables only exist while program runs– Data shown on screen only exist while being shown
• Some data must be persistent– It should be kept for hours, days, months or even years
• Simple way to make data persistent: files– Data saved electronically on your hard-disk– Records (lines) contain numbers, strings, etc.
• A “flat file” (e.g. a lexicon) allows sequential processing of records:– To access line/record n, we must read previous n1 lines
Database management systems (1)
14CS1022
• Alternative to “flat files”: databases• Databases allow efficient access to records– We can directly access record n– We can search for records which meet some criteria
• Database management system (DBMS)– Means to create and manage databases (notice plural)– Infra-structure to organise/store data, records– Means to access/query records
• No need to create our own database systems– Existing solutions – stable technology, free, efficient, scale up– MySQL (www.mysql.com)– Trend: databases in the “cloud” (Amazon)
Database management systems (2)
15CS1022
• Data in DB must be thought out– Only data needed should be there– Same data should not appear in two (or more) places– If data can be obtained from other data, then it should
not be stored (e.g., unit price and total batch price)• Challenge of database design/management– Selecting the “right” data– Organising data (what should go with what and where)– Deciding when to re-design databases (due to problems)
Database management systems (3)
16CS1022
• Data in DB stored as tables• Table T1: Personal details (student record system)
Tables (1)
17CS1022
ID No. Name Sex DOB MaritalStatus
Address
4000123 Jones Female 1.2.83 Single 2 The Motte, Newton5001476 Singh Male 4.5.84 Married 4A New Road, Seaforth5112391 Smith Female 21.3.84 Single 17 The Crescent, Seaforth5072411 Smith Male 12.12.84 Single 21 Pudding Lane, Witham5532289 Ching Male 15.8.83 Single 4A New Road, Seaforth5083001 Grant Male 9.7.83 Married 18 Iffley Road, Reading5196236 McKay Female 21.3.84 Single 133 Uff Road, Reading4936201 French Female 7.10.77 Married 11 Finn Road, Newtown
• ID. Numbers should be unique• Names can be repeated (e.g., Smith)• Problems when listing students just by name
• A table with n columns A1, A2, , An is a subset of the Cartesian product
A1 A2 An
– All records should be members of the product
Tables (2)
18CS1022
• Table T2: Course resultsTables (3)
19CS1022
Name Intro to Maths
Programming DiscreteMaths
ComputerSystems
Cummings A B C AJones B C B DGrant C B A CSingh C B A DFrench D E C CMcKay A A B ACookson C A A B
• Table is Cartesian product of Names Marks Marks Marks Marks
• Rows are elements(Jones, B, C, B, D)(Grant, C, B, A, C)
• DBMS provides operations on tables to – Extract information– Modify tables– Combine tables
• Some of these operations are– Project (as in "to project")– Join – Select
Database operations
20CS1022
• Selects columns of a table to form a new table• T3 = project(T1,{Name, Address}) yields
Project operation
21CS1022
Name Address
Jones 2 The Motte, NewtonSingh 4A New Road, SeaforthSmith 17 The Crescent, SeaforthSmith 21 Pudding Lane, WithamChing 4A New Road, SeaforthGrant 18 Iffley Road, ReadingMcKay 133 Uff Road, ReadingFrench 11 Finn Road, Newtown
• Takes two tables and joins their information• Puts together tuples which agree on attributes• join(T3, T2) yields
Join operation
22CS1022
Name Address Intro to Maths
Programming DiscreteMaths
ComputerSystems
Jones 2 The Motte, Newton B C B DGrant 18 Iffley Road, Reading C B A CSingh 4A New Road, Seaforth C B A DFrench 11 Finn Road, Newtown D E C CMcKay 133 Uff Road, Reading A A C A
• Extracts rows of a table which satisfy criteria• select(T1, (Sex = Male and Marital Status = Married))
Select operation
23CS1022
ID No. Name Sex DOB MaritalStatus
Address
5001476 Singh Male 4.5.84 Married 4A New Road, Seaforth5083001 Grant Male 9.7.83 Married 18 Iffley Road, Reading
Contrast with
T5 = {(I,N,S,D,M,A) :
(I,N,S,D,M,A) T1 and S = Male and M = Married}
You should now know:• What equivalence relations are• Definition of set partition• Partial and total order of a relation• Concepts of database management systems
Summary
24CS1022
Further reading• R. Haggarty. “Discrete Mathematics for
Computing”. Pearson Education Ltd. 2002. (Chapter 4)
25CS1022