Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1022 Computer Programming & Principles Lecture 2 Relations.

Similar presentations


Presentation on theme: "CS1022 Computer Programming & Principles Lecture 2 Relations."— Presentation transcript:

1 CS1022 Computer Programming & Principles Lecture 2 Relations

2 Plan of lecture Equivalence relations Set partition Partial order Total order Database management systems 2 CS1022

3 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) 3 CS1022

4 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 4 CS1022

5 Set partition (2) A partition of a set A is a collection of non-empty subsets A 1, A 2, , A n, such that – A  A 1  A 2    A n –  i, j, 1  i  n, 1  j  n, i  j, A i  A j   Subsets A i, 1  i  n, are blocks of the partition Sample Venn diagram: 5 blocks 5 CS1022 A1A1 A2A2 A3A3 A4A4 A5A5 A Blocks do not overlap Their intersection is empty

6 Set partition (3) Subsets/partitions consist of related elements of A Equivalence class E y, of any y  A, is the set E y   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 A 2.Show that if x R y then E x  E y 3.Show that A  E x 1  E x 2    E x n 4.Show that for any two E x and E y, E x  E y   6 CS1022 E y is the set of things that are in the equivalence class with respect to y. See later.

7 Example: relation R on R (real numbers) defined as x 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) 7 CS1022 Uses a bit of number theory. Examples: 7-5 = -(5-7); (7-5)+(5-2) = (7-2).

8 Our previous example is  y  R, E y   z  R : (z  y)  Z  We can obtain the following equivalence classes: E 0   z  R : (z  0)  Z   Z (all integers) E ½   z  R : (z  ½)  Z   ,  1½,  ½, ½, 1½, 2½,  E  2   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) 8 CS1022 R is the set of reals. Z is the set of integers.

9 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) 9 CS1022 "is taller than" a partial order?

10 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) 10 CS1022

11 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) 11 CS1022

12 6 1218 32 1 Example: relation “is a divisor of” on set A =  1, 2, 3, 6, 12, 18  – Defines a partial order Table of predecessors & immediate predecessors Partial order (4) 12 CS1022 elementpredecessorsimmediate predecessors 1none 211 311 61, 2, 32, 3 121, 2, 3, 66 181, 2, 3, 66 Why the branch?

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

14 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 n  1 lines Database management systems (1) 14 CS1022

15 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)www.mysql.com – Trend: databases in the “cloud” (Amazon) Database management systems (2) 15 CS1022

16 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) 16 CS1022

17 Data in DB stored as tables Table T1: Personal details (student record system) Tables (1) 17 CS1022 ID No.NameSexDOBMarital Status Address 4000123JonesFemale1.2.83Single2 The Motte, Newton 5001476SinghMale4.5.84Married4A New Road, Seaforth 5112391SmithFemale21.3.84Single17 The Crescent, Seaforth 5072411SmithMale12.12.84Single21 Pudding Lane, Witham 5532289ChingMale15.8.83Single4A New Road, Seaforth 5083001GrantMale9.7.83Married18 Iffley Road, Reading 5196236McKayFemale21.3.84Single133 Uff Road, Reading 4936201FrenchFemale7.10.77Married11 Finn Road, Newtown ID. Numbers should be unique Names can be repeated (e.g., Smith) Problems when listing students just by name

18 A table with n columns A 1, A 2, , A n is a subset of the Cartesian product A 1  A 2    A n – All records should be members of the product Tables (2) 18 CS1022

19 Table T2: Course results Tables (3) 19 CS1022 NameIntro to Maths ProgrammingDiscrete Maths Computer Systems CummingsABCA JonesBCBD GrantCBAC SinghCBAD FrenchDECC McKayAABA CooksonCAAB Table is Cartesian product of Names  Marks  Marks  Marks  Marks Rows are elements (Jones, B, C, B, D) (Grant, C, B, A, C)

20 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 20 CS1022

21 Selects columns of a table to form a new table T3 = project(T1,{Name, Address}) yields Project operation 21 CS1022 NameAddress Jones2 The Motte, Newton Singh4A New Road, Seaforth Smith17 The Crescent, Seaforth Smith21 Pudding Lane, Witham Ching4A New Road, Seaforth Grant18 Iffley Road, Reading McKay133 Uff Road, Reading French11 Finn Road, Newtown

22 Takes two tables and joins their information Puts together tuples which agree on attributes join(T3, T2) yields Join operation 22 CS1022 NameAddressIntro to Maths ProgrammingDiscrete Maths Computer Systems Jones2 The Motte, NewtonBCBD Grant18 Iffley Road, ReadingCBAC Singh4A New Road, SeaforthCBAD French11 Finn Road, NewtownDECC McKay133 Uff Road, ReadingAACA

23 Extracts rows of a table which satisfy criteria select(T1, (Sex = Male and Marital Status = Married)) Select operation 23 CS1022 ID No.NameSexDOBMarital Status Address 5001476SinghMale4.5.84Married4A New Road, Seaforth 5083001GrantMale9.7.83Married18 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}

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

25 Further reading R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd. 2002. (Chapter 4) 25 CS1022


Download ppt "CS1022 Computer Programming & Principles Lecture 2 Relations."

Similar presentations


Ads by Google