250 likes | 370 Views
CS1022 Computer Programming & Principles. Lecture 4.2 Relations (2). Plan of lecture. Equivalence relations Set partition Partial order Total order Database management systems. Equivalence relation. A relation R on A which is Reflexive ( x A ( x , x ) R ),
E N D
CS1022Computer Programming & Principles Lecture 4.2 Relations (2)
Plan of lecture • Equivalence relations • Set partition • Partial order • Total order • Database management systems CS1022
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) Rand (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) CS1022
Set partition (1) • Underlying set A of equivalence relation Rcan 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 CS1022
Set partition (2) • A partition of a set A is a collection of non-empty subsets A1, A2, , An, such that • A A1A2 An • i, j, 1 i n, 1 j n, i j, AiAj • Subsets Ai, 1 i n, are blocks of the partition • Sample Venn diagram: 5 blocks • Blocks do not overlap • Their intersection is empty A A2 A1 A3 A4 A5 CS1022
Set partition (3) • Subsets/partitions consist of related elements of A • Equivalence class Ey, of any y A, is the set Ey z A : zR 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: • Show that equivalent classes are non-empty subsets of A • Show that if xR y then Ex Ey • Show that A Ex1Ex2 Exn • Show that for any two Ex and Ey, ExEy Eyis the set of things that are in the equivalence class with respect to y. See later. CS1022
Set partition (4) • Example: relation R on R (real numbers) defined as xRy if, and only if, x y is an integer (Z). x yis 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 Uses a bit of number theory. Examples: 7-5 = -(5-7); (7-5)+(5-2) = (7-2). CS1022
Set partition (5) • Our previous example is y 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:(z2)Z,12,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'? R is the set of reals. Z is the set of integers. CS1022
Partial order (1) • 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) Rand (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 "is taller than" a partial order? CS1022
Partial order (2) • 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 Rz and z Ry, then x is an immediate predecessor of y • We represent this as xy CS1022
Partial order (3) • Immediate predecessors graphically represented as a Hasse diagram: • Vertices are elements of posetA • If xy, 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 CS1022
Partial order (4) • Example: relation “is a divisor of” on set A = 1, 2, 3, 6, 12, 18 • Defines a partial order • Table of predecessors & immediate predecessors 12 18 6 2 3 1 Why the branch? CS1022
Total order • 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 CS1022
Database management systems (1) • 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 CS1022
Database management systems (2) • 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) CS1022
Database management systems (3) • 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) CS1022
Tables (1) • Data in DB stored as tables • Table T1: Personal details (student record system) • ID. Numbers should be unique • Names can be repeated (e.g., Smith) • Problems when listing studentsjust by name CS1022
Tables (2) • 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 CS1022
Tables (3) • Table T2: Course results • Table is Cartesian product of • Names Marks Marks Marks Marks • Rows are elements • (Jones, B, C, B, D) • (Grant, C, B, A, C) CS1022
Database operations • DBMS provides operations on tables to • Extract information • Modify tables • Combine tables • Some of these operations are • Project (as in "to project") • Join • Select CS1022
Project operation • Selects columns of a table to form a new table • T3 = project(T1,{Name, Address}) yields CS1022
Join operation • Takes two tables and joins their information • Puts together tuples which agree on attributes • join(T3, T2) yields CS1022
Select operation • Extracts rows of a table which satisfy criteria • select(T1, (Sex = Male and Marital Status = Married)) • Contrast with • T5 = {(I,N,S,D,M,A) : • (I,N,S,D,M,A) T1 andS = Male andM = Married} CS1022
Summary You should now know: • What equivalence relations are • Definition of set partition • Partial and total order of a relation • Concepts of database management systems CS1022
Further reading • R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd. 2002. (Chapter 4) CS1022