Download presentation
Presentation is loading. Please wait.
1
CS1022 Computer Programming & Principles
Lecture 2 Relations
2
Plan of lecture Equivalence relations Set partition Partial order
Total order Database management systems 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) 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 CS1022
5
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 A Blocks do not overlap Their intersection is empty A1 A2 A3 A4 A5 CS1022
6
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: Show that equivalent classes are non-empty subsets of A Show that if x R y then Ex Ey Show that A Ex1 Ex2 Exn Show that for any two Ex and Ey, Ex Ey Ey is the set of things that are in the equivalence class with respect to y. See later. CS1022
7
Set partition (4) 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 Uses a bit of number theory. Examples: 7-5 = -(5-7); (7-5)+(5-2) = (7-2). CS1022
8
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 : (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'? R is the set of reals. Z is the set of integers. CS1022
9
Partial order (1) "is taller than" a partial order?
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 "is taller than" a partial order? CS1022
10
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 x 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 CS1022
11
Partial order (3) 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 CS1022
12
Partial order (4) Example: relation “is a divisor of” on set
Defines a partial order Table of predecessors & immediate predecessors element predecessors immediate 1 none 2 3 6 1, 2, 3 2, 3 12 1, 2, 3, 6 18 6 12 18 3 2 1 Why the branch? CS1022
13
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
14
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
15
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 ( Trend: databases in the “cloud” (Amazon) CS1022
16
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
17
Tables (1) Data in DB stored as tables
Table T1: Personal details (student record system) ID No. Name Sex DOB Marital Status Address Jones Female 1.2.83 Single 2 The Motte, Newton Singh Male 4.5.84 Married 4A New Road, Seaforth Smith 17 The Crescent, Seaforth 21 Pudding Lane, Witham Ching Grant 9.7.83 18 Iffley Road, Reading McKay 133 Uff Road, Reading French 11 Finn Road, Newtown ID. Numbers should be unique Names can be repeated (e.g., Smith) Problems when listing students just by name CS1022
18
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
19
Names Marks Marks Marks Marks
Tables (3) Table T2: Course results Name Intro to Maths Programming Discrete Maths Computer Systems Cummings A B C Jones D Grant Singh French E McKay Cookson Table is Cartesian product of Names Marks Marks Marks Marks Rows are elements (Jones, B, C, B, D) (Grant, C, B, A, C) CS1022
20
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
21
Project operation Selects columns of a table to form a new table
T3 = project(T1,{Name, Address}) yields Name Address Jones 2 The Motte, Newton Singh 4A New Road, Seaforth Smith 17 The Crescent, Seaforth 21 Pudding Lane, Witham Ching Grant 18 Iffley Road, Reading McKay 133 Uff Road, Reading French 11 Finn Road, Newtown CS1022
22
Join operation Takes two tables and joins their information
Puts together tuples which agree on attributes join(T3, T2) yields Name Address Intro to Maths Programming Discrete Maths Computer Systems Jones 2 The Motte, Newton B C D Grant 18 Iffley Road, Reading A Singh 4A New Road, Seaforth French 11 Finn Road, Newtown E McKay 133 Uff Road, Reading CS1022
23
Select operation Extracts rows of a table which satisfy criteria
select(T1, (Sex = Male and Marital Status = Married)) ID No. Name Sex DOB Marital Status Address Singh Male 4.5.84 Married 4A New Road, Seaforth Grant 9.7.83 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} CS1022
24
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
25
Further reading R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd (Chapter 4) CS1022
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.