CS1022 Computer Programming & Principles

Slides:



Advertisements
Similar presentations
Representing Relations
Advertisements

Representing Relations Rosen 7.3. Using Matrices For finite sets we can use zero-one matrices. Elements of each set A and B must be listed in some particular.
CSE115/ENGR160 Discrete Mathematics 04/26/12 Ming-Hsuan Yang UC Merced 1.
8.3 Representing Relations Connection Matrices Let R be a relation from A = {a 1, a 2,..., a m } to B = {b 1, b 2,..., b n }. Definition: A n m  n connection.
Chapter 3 Relations. Section 3.1 Relations and Digraphs.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Section 7.4: Closures of Relations Let R be a relation on a set A. We have talked about 6 properties that a relation on a set may or may not possess: reflexive,
Representing Relations Using Matrices
Applied Discrete Mathematics Week 11: Graphs
CS2210(22C:19) Discrete Structures Relations Spring 2015 Sukumar Ghosh.
Computing Fundamentals 1 Lecture 7 Relations Lecturer: Patrick Browne Room K308 Based on Chapter 14. A Logical approach.
Discrete Mathematics Lecture#11.
Relations Chapter 9.
Chapter 9 1. Chapter Summary Relations and Their Properties n-ary Relations and Their Applications (not currently included in overheads) Representing.
Applied Discrete Mathematics Week 10: Equivalence Relations
Chapter 9. Chapter Summary Relations and Their Properties Representing Relations Equivalence Relations Partial Orderings.
Lecture 14 Relations CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
Chapter 9. Chapter Summary Relations and Their Properties n-ary Relations and Their Applications (not currently included in overheads) Representing Relations.
Unit Unit 04 Relations IT DisiciplineITD1111 Discrete Mathematics & Statistics STDTLP1 Unit 4 Relations.
Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation.
Chapter 9. Section 9.1 Binary Relations Definition: A binary relation R from a set A to a set B is a subset R ⊆ A × B. Example: Let A = { 0, 1,2 } and.
Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Relations, Functions.
Relations Relations between sets are like functions, but with a more general definition (given later). presentations: a set, a binary matrix, a graph…
1 Closures of Relations: Transitive Closure and Partitions Sections 8.4 and 8.5.
Fall 2015 COMP 2300 Discrete Structures for Computation Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Relations.
Discrete Mathematics Relation.
Relations and their Properties
1 RELATIONS Learning outcomes Students are able to: a. determine the properties of relations – reflexive, symmetric, transitive, and antisymmetric b. determine.
CS 103 Discrete Structures Lecture 19 Relations. Chapter 9.
Relation. Combining Relations Because relations from A to B are subsets of A x B, two relations from A to B can be combined in any way two sets can be.
Chapter 9. Chapter Summary Relations and Their Properties n-ary Relations and Their Applications (not currently included in overheads) Representing Relations.
Chapter Relations and Their Properties
1 RELATIONS Learning outcomes Students are able to: a. determine the properties of relations – reflexive, symmetric, transitive, and antisymmetric b. determine.
Chapter 8: Relations. 8.1 Relations and Their Properties Binary relations: Let A and B be any two sets. A binary relation R from A to B, written R : A.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 0 Introduction Some slides are in courtesy of Prof.
Relations and Functions ORDERED PAIRS AND CARTESIAN PRODUCT An ordered pair consists of two elements, say a and b, in which one of them, say a is designated.
Section 7.3: Representing Relations In this section, we will cover two ways to represent a relation over a finite set other than simply listing the relation.
Section 9.3. Section Summary Representing Relations using Matrices Representing Relations using Digraphs.
Lecture 5 Set Theory. Plan of lecture Why set theory? Sets and their properties Membership and definition of sets “Famous” sets Types of variables and.
Lecture 7: Relations Dr Andrew Purkiss-Trew Cancer Research UK Mathematics for Computing.
Module Code MA1032N: Logic Lecture for Week Autumn.
Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation is a relation on.
Relations Chapter 9 Copyright © McGraw-Hill Education. All rights reserved. No reproduction or distribution without the prior written consent of McGraw-Hill.
Representing Relations Using Digraphs
Discrete Mathematical
Relations and Their Properties
Chapter 5 Relations and Operations
Lecture 6 Set Theory.
Relations.
Relations Binary relations represent relationships between the elements of two sets. A binary relation R from set A to set B is defined by: R  A 
Learn about relations and their basic properties
CS1022 Computer Programming & Principles
Relations Chapter 9.
Lecture # 14 Types of Relations
Applied Discrete Mathematics Week 10: Relations
Representing Relations
RELATION KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS )
CS 173: Discrete Mathematical Structures
CS1022 Computer Programming & Principles
CSNB 143 Discrete Mathematical Structures
Mathematical Structures for Computer Science Chapter 6
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 9 Relations Slides are adopted from.
Applied Discrete Mathematics Week 10: Equivalence Relations
CS2210 Discrete Structures Relations
Introduction to Finite Automata
Applied Discrete Mathematics Week 6: Relations/Digraphs
Combining relations via relational composition
REVISION Relation. REVISION Relation Introduction to Relations and Functions.
Agenda Lecture Content: Relations (Relasi)
Presentation transcript:

CS1022 Computer Programming & Principles Lecture 1 Relations

Plan of lecture Motivation Binary relations Graphs to represent relations Arrays to represent relations Properties of relations Closure of relations CS1022

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

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

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

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

(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

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

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

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

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

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

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

Arrays to represent relations (4) We could swap rows and columns around We could use 0 and 1 instead of T and F CS1022

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

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

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 xy ((x R y)  (y R x)) Antisymmetric when xy (((x, y  A) and (x R y) and (y R x))  (x  y)) Transitive when xyz ((x, y, z  A) and ((x R y) and (y R z)))  (x R z)) CS1022

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 xy (((x, y)  R)  ((y, x)  R)) Antisymmetric when xy (((x, y  A) and ((x, y)  R) and (x  y))  ((y, x)  R)) Transitive when xyz (((x, y, z  A) and ((x, y)  R) and ((y, z)  R))  ((x, z)  R)) CS1022

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

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

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

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

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? xyz (((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

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

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

Summary You should now know: Why we need relations How to represent relations Properties of relations Closure of relations CS1022

Further reading R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd. 2002. (Chapter 3) Wikipedia’s entry Wikibooks entry CS1022