Download presentation
Presentation is loading. Please wait.
1
CS1022 Computer Programming & Principles
Lecture 1 Relations
2
Plan of lecture Motivation Binary relations
Graphs to represent relations Arrays to represent relations Properties of relations Closure of relations CS1022
3
Why relations? Pieces of information are inherently related
People, addresses, age, what they have bought, etc. When modelling information, we need to capture such relations: Bob is the husband of Jane Relation captured as an ordered pair (husband, wife), An element of the Cartesian product of Male Female sets Another scenario: students registered for courses Set S = {Bob, Tony, ...} of students Set C = {CS1022, CS1015, ...} of courses Cartesian product S C = {(s, c) : s S, c C} {(Bob,CS1022),(Bob,CS1015),(Bob, ...),(Tony,CS1022),(Tony,...)} Very large set with all possible ways students can be registered CS1022
4
What’s important in relations?
Binary relations model any relationship between elements of two sets Important: To know how to represent relations Properties we want to study in some relations Equivalence and partial orders Databases borrow extensively from sets & relations Whether you will work in IT or not, you will encounter information and how it is gathered, stored and managed You might have to help choosing information to gather CS1022
5
Binary relations A binary relation between sets A and B is a subset R of the Cartesian product A B, R A B When A B, we refer to R as a relation on A Example: family tree R = {(x, y) : x is a grandfather of y} S = {(x, y) : x is a sister of y} Fred & Mavis Alice Ken & Sue John & Mary Mike Penny Jane Fiona Alan CS1022
6
Binary relations A binary relation between sets A and B is a subset R of the Cartesian product A B, R A B When A B, we refer to R as a relation on A Example: family tree R = {(x, y) : x is a grandfather of y} R = {(Fred, Jane), (Fred, Fiona), (Fred, Alan), (John, Jane), (John, Fiona), (John, Alan)} S = {(x, y) : x is a sister of y} Fred & Mavis Alice Ken & Sue John & Mary Mike Penny Jane Fiona Alan CS1022
7
(Jane, Fiona), (Jane, Alan), (Fiona, Jane), (Fiona, Alan)}
Binary relations A binary relation between sets A and B is a subset R of the Cartesian product A B, R A B When A B, we refer to R as a relation on A Example: family tree R = {(x, y) : x is a grandfather of y} R = {(Fred, Jane), (Fred, Fiona), (Fred, Alan), (John, Jane), (John, Fiona), (John, Alan)} S = {(x, y) : x is a sister of y} S = {(Alice, Ken), (Sue, Mike), (Sue, Penny), (Penny, Sue), (Penny, Mike), (Jane, Fiona), (Jane, Alan), (Fiona, Jane), (Fiona, Alan)} Fred & Mavis Alice Ken & Sue John & Mary Mike Penny Jane Fiona Alan CS1022
8
Graphs to represent relations (1)
Let A and B be two finite sets Let R A B be a relation between A and B We build a directed graph (or digraph): For each (x, y) R, draw an arrow (or arc) from x to y Notice that arrow has a direction: from x to y Example: Let A = {1, 3, 5, 7}, B = {2, 4, 6} Relation V A B, V = {(x, y) : x y} Has the following graph 1 7 3 5 2 4 6 CS1022
9
Graphs to represent relations (2)
Example: Let A = {1, 2, 3, 4, 5, 6} Relation R A A, R = {(x, y) : x is a divisor of y} R = {(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 2), (2, 4), (2, 6), (3, 3), (3, 6), (4, 4), (5, 5), (6, 6)} Graph: divisor m of an integer n is an integer m that can be multiplied by some other integer o to produce n. 3 is a divisor of 6 since 3*2 = 6. 1 2 6 3 5 4 CS1022
10
Graphs to represent relations (3)
Graphs: very popular way to model information Examples: Cities and roads connecting them (some one-way) “Friends” in social networks Computing networks in general N.B.: many algorithms (with guarantees) for graphs Find out if two nodes are (not) connected Find out if there are “cliques” (fully connected groups) Find out “source” (a node from which all arrows leave) and “sinks” (nodes from which no arrows leave) CS1022
11
Arrays to represent relations (1)
Arrays (a kind of table) to represent graphs First, we Label elements of sets A and B List their elements in a particular order: A = {a1i, a2j, , aqn} B = {b1k, b2l, , brm} Notice: We are not ordering sets – these are element positions Whatever order, there will be an element 1, 2, etc. Recall the discussion about subscripts to distinguish elements in a set and superscripts to order those elements. CS1022
12
Arrays to represent relations (2)
Let A = {a1i, a2j, , aqn} B = {b1k, b2l, , brm} Relation R A B: array M with q rows, r columns Array M is called a q by r matrix Rows labelled by elements of A (in chosen order) Columns labelled by elements of B (in chosen order) Entry in row i and column j is M(i, j) where: M(i, j) = T (true) if, and only if, (ai, bj) R M(i, j) = F (false) if, and only if, (ai, bj) R CS1022
13
Arrays to represent relations (3)
Example: A = {a1, e3, c5, g7}, B = {b2, f4, d6} Relation V A B, V = {(x, y) : x y in the alphabet} 2 4 6 1 T 3 F 5 7 1 7 3 5 2 4 6 CS1022
14
Arrays to represent relations (4)
We could swap rows and columns around We could use 0 and 1 instead of T and F CS1022
15
Infix notation for relations
If R is a relation between two sets, we also write x R y whenever (x, y) R x R y reads “x is R-related to y” This is the “infix” notation to improve reading: x is_a_sister_of y x is_a_divisor_of y CS1022
16
Notation for relations: summary
Relation R between two finite sets can be described: In words (using a suitable predicate) As a set of ordered pairs As a digraph (directed graph) As a matrix Important: you are expected to Describe relations in any representation “Translate” between any two representations Question: how would you represent relations using your “pet” programming language? CS1022
17
Properties of relations (1)
We focus on relations on a single set A A relation R on a set A (using infix notation) is: Reflexive when x ((x A) (x R x)) Symmetric when xy ((x R y) (y R x)) Antisymmetric when xy (((x, y A) and (x R y) and (y R x)) (x y)) Transitive when xyz ((x, y, z A) and ((x R y) and (y R z))) (x R z)) CS1022
18
Properties of relations (2)
A relation R on a set A is (using pairs) is Reflexive when x ((x A) ((x, x) R)) Symmetric when xy (((x, y) R) ((y, x) R)) Antisymmetric when xy (((x, y A) and ((x, y) R) and (x y)) ((y, x) R)) Transitive when xyz (((x, y, z A) and ((x, y) R) and ((y, z) R)) ((x, z) R)) CS1022
19
Properties of relations (3)
A relation R on a set A is (now using a graph) is Reflexive if there is an arc from each vertex to itself Symmetric if whenever there is an arc from x to y there is also an arc from y to x Antisymmetric if whenever there is an arc from x to y and x y, there is no arc from y to x Transitive if whenever there is an arc from x to y and an arc from y to z, then there is also an arc from x to z CS1022
20
Properties of relations (4)
Suppose relation x has the same age as y over People It is reflexive as any x has the same age as itself It is symmetric since x has the same age as y means the same as y has the same age as x It is not antisymmetric because we can have many different people with the same age It is transitive since if x has the same age as y and y has the same age as z, then x has the same age as z "is taller than" has what properties? CS1022
21
Properties of relations (5)
Antisymmetric? has the same finger print as has the same DNA as has the same ID as is parent of is less than CS1022
22
Properties of relations (6)
You should be able to Check if a relation has any of the properties Proof: By a counter-example showing property does not hold Assume premises (general) and reach conclusion, to show the property does hold We might need to assume properties hold: E.g., algorithm only works if relation is transitive Algorithm needs pre-processing to check this is the case CS1022
23
Closure of relations (1)
We can extend a relation R with more pairs with respect to a property or relations P Example: what is the closure of the following ancestor with respect to transitivity? xyz (((x, y, z A) and ((x, y) R) and ((y, z) R)) ((x, z) R)) Ancestor = {(bill, jill), (jill, kim), (phil,mary), (kim,phil), (mary,jane)} Ancestor* = {(bill, jill), (jill, kim), (bill,kim), (phil,mary), (kim,phil), (jill,phil), (kim,mary), (bill, phil), (bill,mary), (mary,phil), (phil,jane), (bill,jane)} Do we have all of them?? Do we have anything extra?? CS1022
24
Closure of relations (1)
We can extend a relation R with more pairs until a certain property P holds If extended relation is smallest possible one, then It is the closure of R with respect to property P It is represented as R* “Smallest possible”: only essential new pairs added Formally, R* is the closure of R w.r.t. property P if: R* has property P R R* R* is a subset of any other relation that includes R and has property P CS1022
25
Computing closure of relations
Algorithms exist to compute closures, e.g. transitive closure. Algorithms will be quite specific to the property (e.g. transitive) You need to search through all the ‘paths’ to find missing relations for transitive closure CS1022
26
Summary You should now know: Why we need relations
How to represent relations Properties of relations Closure of relations CS1022
27
Further reading R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd (Chapter 3) Wikipedia’s entry Wikibooks entry CS1022
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.