1 Relations Rosen 5 th ed., ch. 7. 2 Relations Relationships between elements of sets occur very often.Relationships between elements of sets occur very.

Slides:



Advertisements
Similar presentations
Representing Relations Using Matrices
Advertisements

5/16/20151 You Never Escape Your… Relations. 5/16/20152Relations If we want to describe a relationship between elements of two sets A and B, we can use.
CS2210(22C:19) Discrete Structures Relations Spring 2015 Sukumar Ghosh.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Relations.
1 Section 7.1 Relations and their properties. 2 Binary relation A binary relation is a set of ordered pairs that expresses a relationship between elements.
1 Representing Relations Epp section ??? CS 202 Aaron Bloomfield.
Relations Chapter 9.
Applied Discrete Mathematics Week 10: Equivalence Relations
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 8: Relations Relations(8.1) n-any Relations &
Relations (1) Rosen 6 th ed., ch. 8. Binary Relations Let A, B be any two sets. A binary relation R from A to B, written (with signature) R:A↔B, is a.
Module #21 - Relations Representing Relations Rosen 6 th ed., Ch. 8.
Module #18: Relations, part I
Relations Rosen 5 th ed., ch. 7. Binary Relations Let A, B be any sets. A binary relation R from A to B, written (with signature) R:A×B, or R:A,B, is.
Chapter 4 Relations and Digraphs
April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a.
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.
Relations and Their Properties
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.
8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.
Sets Define sets in 2 ways  Enumeration  Set comprehension (predicate on membership), e.g., {n | n  N   k  k  N  n = 10  k  0  n  50} the set.
Discrete Structures1 You Never Escape Your… Relations.
1 Closures of Relations: Transitive Closure and Partitions Sections 8.4 and 8.5.
Discrete Mathematics and Its Applications Sixth Edition By Kenneth Rosen Chapter 8 Relations 歐亞書局.
Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Relations.
Discrete Mathematics Relation.
Relations and their Properties
CHAPTER 3 FUZZY RELATION and COMPOSITION. 3.1 Crisp relation Product set Definition (Product set) Let A and B be two non-empty sets, the product.
Dr. Eng. Farag Elnagahy Office Phone: King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.
Fall 2002CMSC Discrete Structures1 You Never Escape Your… Relations.
Mathematical Preliminaries
Relations. Important Definitions We covered all of these definitions on the board on Monday, November 7 th. Definition 1 Definition 2 Definition 3 Definition.
이산수학 (Discrete Mathematics) 7.3 관계의 표현 (Representing Relations) 2006 년 봄학기 문양세 강원대학교 컴퓨터과학과.
Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.
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.
Problem Statement How do we represent relationship between two related elements ?
Chapter 9. Chapter Summary Relations and Their Properties n-ary Relations and Their Applications (not currently included in overheads) Representing Relations.
Unit II Discrete Structures Relations and Functions SE (Comp.Engg.)
Relations Section 9.1, 9.3—9.5 of Rosen Spring 2012
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Relations.
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.
8.4 Closures of Relations Definition: The closure of a relation R with respect to property P is the relation obtained by adding the minimum number of.
RelationsCSCE 235, Spring Introduction A relation between elements of two sets is a subset of their Cartesian products (set of all ordered pairs.
2012 년 봄학기 강원대학교 컴퓨터과학전공 문양세 이산수학 (Discrete Mathematics) n- 항 관계 (n-ary Relations)
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.
1 Closures of Relations Based on Aaron Bloomfield Modified by Longin Jan Latecki Rosen, Section 8.4.
Chapter8 Relations 8.1: Relations and their properties.
15.1 CompSci 102© Michael Frank Today’s topics RelationsRelations –Kinds of relations –n-ary relations –Representations of relations Reading: Sections.
Relations and Their Properties
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 
Relations Chapter 9.
Relations and Digraphs
RELATION KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS )
Dr. Ameria Eldosoky Discrete mathematics
Relations Rosen 5th ed., ch. 7.
CSE 504 Discrete Structures & Foundations of Computer Science
Discrete Mathematics and its Applications
§7.3 Representing Relations Longin Jan Latecki
CS2210 Discrete Structures Relations
Rosen 5th ed., ch. 7 ~32 slides (in progress), ~2 lectures
2/18/2019 §7.1 Binary Relations (2nd pass) and §7.2 n-ary Relations Longin Jan Latecki Slides adapted from Kees van Deemter who adopted them from Michael.
2/25/2019 §7.1 Binary Relations (2nd pass) and §7.2 n-ary Relations Longin Jan Latecki Slides adapted from Kees van Deemter who adopted them from Michael.
교환 학생 프로그램 내년 1월 중순부터 6월 초 현재 학부 2,3 학년?
REVISION Relation. REVISION Relation Introduction to Relations and Functions.
§7.3 Representing Relations Longin Jan Latecki
7/30/2019 §7.1 Binary Relations (2nd pass) and §7.2 n-ary Relations Longin Jan Latecki Slides adapted from Kees van Deemter who adopted them from Michael.
Relations 12/7/2019.
Presentation transcript:

1 Relations Rosen 5 th ed., ch. 7

2 Relations Relationships between elements of sets occur very often.Relationships between elements of sets occur very often. –(Employee, Salary) –(Students, Courses, GPA) We use ordered pairs (or n-tuples) of elements from the sets to represent relationships.We use ordered pairs (or n-tuples) of elements from the sets to represent relationships.

3 Binary Relations A binary relation R from set A to set B, written R:A↔B, is a subset of A×B.A binary relation R from set A to set B, written R:A↔B, is a subset of A×B. –E.g., let < : N↔N :≡ {(n,m) | n < m} The notation aRb means (a,b)  R.The notation aRb means (a,b)  R. –E.g., a < b means (a,b)  < If aRb we may sayIf aRb we may say “a is related to b (by relation R)” “a is related to b (by relation R)”

4 Example A: {students at UNR}, B: {courses offered at UNR} R: “relation of students enrolled in courses” R: “relation of students enrolled in courses” (Jason, CS365), (Mary, CS201) are in R If Mary does not take CS365, then (Mary, CS365) is not in R! If CS480 is not being offered, then (Jason, CS480), (Mary, CS480) are not in R!

5 Complementary Relations Let R:A↔B be any binary relation.Let R:A↔B be any binary relation. Then, R:A↔B, the complement of R, is the binary relation defined by R :≡ {(a,b) | (a,b)  R} = (A×B) − RThen, R:A↔B, the complement of R, is the binary relation defined by R :≡ {(a,b) | (a,b)  R} = (A×B) − R Note this is just R if the universe of discourse is U = A×B; thus the name complement. Note the complement of R is R.Note the complement of R is R.  <} = {(a,b) | ¬a<b} = ≥ Example: < = {(a,b) | (a,b)  <} = {(a,b) | ¬a<b} = ≥

6 Inverse Relations Any binary relation R:A↔B has an inverse relation R −1 :B↔A, defined by R −1 :≡ {(b,a) | (a,b)  R}.Any binary relation R:A↔B has an inverse relation R −1 :B↔A, defined by R −1 :≡ {(b,a) | (a,b)  R}. E.g., a} = >.

7 Functions as Relations A function f:A  B is a relation from A to BA function f:A  B is a relation from A to B A relation from A to B is not always a function f:A  B (e.g., relations could be one-to-many)A relation from A to B is not always a function f:A  B (e.g., relations could be one-to-many) Relations are generalizations of functions!Relations are generalizations of functions!

8 Relations on a Set A (binary) relation from a set A to itself is called a relation on the set A.A (binary) relation from a set A to itself is called a relation on the set A. A: {1,2,3,4} R={(a,b) | a divides b}

9 Example How many relations are there on a set A with n elements?

10 Reflexivity A relation R on A is reflexive if  a  A, aRa.A relation R on A is reflexive if  a  A, aRa. –E.g., the relation ≥ :≡ {(a,b) | a≥b} is reflexive. –Is the “divide” relation on the set of positive integers reflexive?

11 Symmetry & Antisymmetry A binary relation R on A is symmetric iff R = R −1, that is, if (a,b)  R ↔ (b,a)  R.A binary relation R on A is symmetric iff R = R −1, that is, if (a,b)  R ↔ (b,a)  R. –E.g., = (equality) is symmetric. < is not. –“is married to” is symmetric, “likes” is not. A binary relation R is antisymmetric if (a,b)  R → (b,a)  R.A binary relation R is antisymmetric if (a,b)  R → (b,a)  R. –< is antisymmetric, “likes” is not.

12 Transitivity A relation R is transitive iff (for all a,b,c) (a,b)  R  (b,c)  R → (a,c)  R.A relation R is transitive iff (for all a,b,c) (a,b)  R  (b,c)  R → (a,c)  R. Examples:Examples: “is an ancestor of” is transitive.“is an ancestor of” is transitive. Is the “divides” relation on the set of positive integers transitive?Is the “divides” relation on the set of positive integers transitive?

13 Combining Relations Two relations can be combined in a similar way to combining two sets.Two relations can be combined in a similar way to combining two sets.

14 Composite Relations Let R:A↔B, and S:B↔C. Then the composite S  R of R and S is defined as:Let R:A↔B, and S:B↔C. Then the composite S  R of R and S is defined as: S  R = {(a,c) | aRb  bSc} Function composition f  g is an example.Function composition f  g is an example. The n th power R n of a relation R on a set A can be defined recursively by:The n th power R n of a relation R on a set A can be defined recursively by: R 1 :≡ R ;R n+1 :≡ R n  R for all n≥0. R 1 :≡ R ;R n+1 :≡ R n  R for all n≥0.

15 Example R is a relation from {1,2,3} to {1,2,3,4} R ={(1,1),(1,4),(2,3),(3,1),(3,4)} R ={(1,1),(1,4),(2,3),(3,1),(3,4)} S is a relation from {1,2,3,4} to {0,1,2} S = {(1,0),(2,0),(3,1),(3,2),(4,1)} S = {(1,0),(2,0),(3,1),(3,2),(4,1)} R  S = {(1,0),(1,1),(2,1),(2,2),(3,0),(3,1)} R  S = {(1,0),(1,1),(2,1),(2,2),(3,0),(3,1)}

16 §7.2: n-ary Relations An n-ary relation R on sets A 1,…,A n, written R:A 1,…,A n, is a subset R  A 1 × … × A n.An n-ary relation R on sets A 1,…,A n, written R:A 1,…,A n, is a subset R  A 1 × … × A n. The degree of R is n.The degree of R is n. Example: R consists of 5-tuples (A,N,S,D,T)Example: R consists of 5-tuples (A,N,S,D,T) A: airplane flights, N: flight number, A: airplane flights, N: flight number, S: starting point, D: destination, T: departure time S: starting point, D: destination, T: departure time

17 Databases The time required to manipulate information in a database depends in how this information is stored.The time required to manipulate information in a database depends in how this information is stored. Operations: add/delete, update, search, combine etc.Operations: add/delete, update, search, combine etc. Various methods for representing databases have been developed.Various methods for representing databases have been developed. We will discuss the “relational model”.We will discuss the “relational model”.

18 Relational Databases A database consists of records, which are n-tuples, made up of fields.A database consists of records, which are n-tuples, made up of fields. A relational database represents records as an n-ary relation R.A relational database represents records as an n-ary relation R. (STUDENT_NAME, ID, MAJOR, GPA) (STUDENT_NAME, ID, MAJOR, GPA) Relations are also called “tables” (e.g., displayed as tables often)Relations are also called “tables” (e.g., displayed as tables often)

19 Relational Databases A domain A i of an n-ary relation is called primary key when no two n-tuples have the same value on this domain (e.g., ID)A domain A i of an n-ary relation is called primary key when no two n-tuples have the same value on this domain (e.g., ID) A composite key is a subset of domains {A i, A j, …} such that an n-tuple (…,a i,…,a j,…) is determined uniquely for each composite value (a i, a j,…)  A i ×A j ×…A composite key is a subset of domains {A i, A j, …} such that an n-tuple (…,a i,…,a j,…) is determined uniquely for each composite value (a i, a j,…)  A i ×A j ×…

20 Selection Operator Let R be an n-ary relation and C a condition that elements in R may satisfy.Let R be an n-ary relation and C a condition that elements in R may satisfy. The selection operator s C maps any (n-ary) relation R on A to the n-ary relation of all n- tuples from R that satisfy C.The selection operator s C maps any (n-ary) relation R on A to the n-ary relation of all n- tuples from R that satisfy C.

21 Example Suppose we have a domain A = StudentName × Standing × SocSecNosSuppose we have a domain A = StudentName × Standing × SocSecNos Suppose we define a certain condition on A, UpperLevel(name,standing,ssn) :≡ [(standing = junior)  (standing = senior)]Suppose we define a certain condition on A, UpperLevel(name,standing,ssn) :≡ [(standing = junior)  (standing = senior)] Then, s UpperLevel is the selection operator that takes any relation R on A (database of students) and produces a relation consisting of just the upper- level classes (juniors and seniors).Then, s UpperLevel is the selection operator that takes any relation R on A (database of students) and produces a relation consisting of just the upper- level classes (juniors and seniors).

22 Projection Operators The projection operator maps theThe projection operator maps the n-tuple n-tuple to the m-tuple to the m-tuple In other words, it deletes n-m of the components of an n-tuple.In other words, it deletes n-m of the components of an n-tuple.

23 Example Note that fewer rows may result when a projection is applied !

24 More Examples Suppose we have a ternary (3-ary) domain Cars=Model×Year×Color. (note n=3).Suppose we have a ternary (3-ary) domain Cars=Model×Year×Color. (note n=3). Consider the index sequence {i k }= 1,3. (m=2)Consider the index sequence {i k }= 1,3. (m=2) Then the projection P simply maps each tuple (a 1,a 2,a 3 ) = (model,year,color) to its image:Then the projection P simply maps each tuple (a 1,a 2,a 3 ) = (model,year,color) to its image: This operator can be usefully applied to a whole relation R  Cars (database of cars) to obtain a list of model/color combinations available.This operator can be usefully applied to a whole relation R  Cars (database of cars) to obtain a list of model/color combinations available. {ik}{ik}

25 Join Operator Puts two relations together to form a sort of combined relation.Puts two relations together to form a sort of combined relation. If the tuple (A,B) appears in R 1, and the tuple (B,C) appears in R 2, then the tuple (A,B,C) appears in the join J(R 1,R 2 ).If the tuple (A,B) appears in R 1, and the tuple (B,C) appears in R 2, then the tuple (A,B,C) appears in the join J(R 1,R 2 ). –A, B, C can also be sequences of elements rather than single elements.

26 Example

27 Join Example Suppose R 1 is a teaching assignment table, relating Professors to Courses.Suppose R 1 is a teaching assignment table, relating Professors to Courses. Suppose R 2 is a room assignment table relating Courses to Rooms,Times.Suppose R 2 is a room assignment table relating Courses to Rooms,Times. Then J(R 1,R 2 ) is like your class schedule, listing (professor,course,room,time).Then J(R 1,R 2 ) is like your class schedule, listing (professor,course,room,time).

28 SQL Example SELECT Departure_Time SELECT Departure_Time FROM Flights FROM Flights WHERE destination=“Detroit” WHERE destination=“Detroit” Find projection P5 of the selection of 5-tuples that satisfy the constraint “destination=Detroit”

29 §7.3: Representing Relations Some ways to represent n-ary relations:Some ways to represent n-ary relations: –With an explicit list or table of its tuples. –With a function from the domain to {T,F}. Or with an algorithm for computing this function.Or with an algorithm for computing this function. Some special ways to represent binary relations:Some special ways to represent binary relations: –With a zero-one matrix. –With a directed graph.

30 Using Zero-One Matrices To represent a relation R by a matrix M R = [m ij ], let m ij = 1 if (a i,b j )  R, else 0.To represent a relation R by a matrix M R = [m ij ], let m ij = 1 if (a i,b j )  R, else 0. E.g., Joe likes Susan and Mary, Fred likes Mary, and Mark likes Sally.E.g., Joe likes Susan and Mary, Fred likes Mary, and Mark likes Sally. The 0-1 matrix representation of that “Likes” relation:The 0-1 matrix representation of that “Likes” relation:

31 Zero-One Reflexive, Symmetric Terms: Reflexive, non-Reflexive, symmetric, and antisymmetric.Terms: Reflexive, non-Reflexive, symmetric, and antisymmetric. –These relation characteristics are very easy to recognize by inspection of the zero-one matrix. Reflexive: all 1’s on diagonal Non-reflexive: some 0’s on diagonal Symmetric: all identical across diagonal Antisymmetric: all 1’s are across from 0’s any- thing anything

32 Using Directed Graphs A directed graph or digraph G=(V G,E G ) is a set V G of vertices (nodes) with a set E G  V G ×V G of edges (arcs,links). Visually represented using dots for nodes, and arrows for edges. Notice that a relation R:A↔B can be represented as a graph G R =(V G =A  B, E G =R).A directed graph or digraph G=(V G,E G ) is a set V G of vertices (nodes) with a set E G  V G ×V G of edges (arcs,links). Visually represented using dots for nodes, and arrows for edges. Notice that a relation R:A↔B can be represented as a graph G R =(V G =A  B, E G =R). MRMR GRGR Joe Fred Mark Susan Mary Sally Node set V G (black dots) Edge set E G (blue arrows)

33 Digraph Reflexive, Symmetric It is extremely easy to recognize the reflexive/irreflexive/ symmetric/antisymmetric properties by graph inspection.       Reflexive: Every node has a self-loop Irreflexive: No node links to itself Symmetric: Every link is bidirectional   Antisymmetric: No link is bidirectional    Asymmetric, non-antisymmetricNon-reflexive, non-irreflexive

34 §7.4: Closures of Relations For any property X, the “X closure” of a set A is defined as the “smallest” superset of A that has the given property.For any property X, the “X closure” of a set A is defined as the “smallest” superset of A that has the given property. The reflexive closure of a relation R on A is obtained by adding (a,a) to R for each a  A. I.e., it is R  I AThe reflexive closure of a relation R on A is obtained by adding (a,a) to R for each a  A. I.e., it is R  I A The symmetric closure of R is obtained by adding (b,a) to R for each (a,b) in R. I.e., it is R  R −1The symmetric closure of R is obtained by adding (b,a) to R for each (a,b) in R. I.e., it is R  R −1 The transitive closure or connectivity relation of R is obtained by repeatedly adding (a,c) to R for each (a,b),(b,c) in R.The transitive closure or connectivity relation of R is obtained by repeatedly adding (a,c) to R for each (a,b),(b,c) in R. –I.e., it is

35 Paths in Digraphs/Binary Relations A path of length n from node a to b in the directed graph G (or the binary relation R) is a sequence (a,x 1 ), (x 1,x 2 ), …, (x n−1,b) of n ordered pairs in E G (or R).A path of length n from node a to b in the directed graph G (or the binary relation R) is a sequence (a,x 1 ), (x 1,x 2 ), …, (x n−1,b) of n ordered pairs in E G (or R). –An empty sequence of edges is considered a path of length 0 from a to a. –If any path from a to b exists, then we say that a is connected to b. (“You can get there from here.”) A path of length n≥1 from a to a is called a circuit or a cycle.A path of length n≥1 from a to a is called a circuit or a cycle. Note that there exists a path of length n from a to b in R if and only if (a,b)  R n.Note that there exists a path of length n from a to b in R if and only if (a,b)  R n.

36 Simple Transitive Closure Alg. A procedure to compute R * with 0-1 matrices. procedure transClosure(M R :rank-n 0-1 mat.) A := B := M R ; for i := 2 to n begin A := A for i := 2 to n begin A := A ⊙ M R ; B := B  A {join} end return B{Alg. takes Θ(n 4 ) time} {note A represents R i }

37 Roy-Warshall Algorithm Uses only Θ(n 3 ) operations!Uses only Θ(n 3 ) operations! Procedure Warshall(M R : rank-n 0-1 matrix) W := M R for k := 1 to n for i := 1 to n for j := 1 to n w ij := w ij  (w ik  w kj ) return W {this represents R * } w ij = 1 means there is a path from i to j going only through nodes ≤k

38 §7.5: Equivalence Relations An equivalence relation (e.r.) on a set A is simply any binary relation on A that is reflexive, symmetric, and transitive.An equivalence relation (e.r.) on a set A is simply any binary relation on A that is reflexive, symmetric, and transitive. –E.g., = itself is an equivalence relation. –For any function f:A→B, the relation “have the same f value”, or = f :≡ {(a 1,a 2 ) | f(a 1 )=f(a 2 )} is an equivalence relation, e.g., let m=“mother of” then = m = “have the same mother” is an e.r.

39 Equivalence Relation Examples “Strings a and b are the same length.”“Strings a and b are the same length.” “Integers a and b have the same absolute value.”“Integers a and b have the same absolute value.” “Real numbers a and b have the same fractional part (i.e., a − b  Z).”“Real numbers a and b have the same fractional part (i.e., a − b  Z).”

40 Equivalence Classes Let R be any equiv. rel. on a set A.Let R be any equiv. rel. on a set A. The equivalence class of a, [a] R :≡ { b | aRb } (optional subscript R)The equivalence class of a, [a] R :≡ { b | aRb } (optional subscript R) –It is the set of all elements of A that are “equivalent” to a according to the eq.rel. R. –Each such b (including a itself) is called a representative of [a] R. Since f(a)=[a] R is a function of a, any equivalence relation R be defined using aRb :≡ “a and b have the same f value”, given that f.Since f(a)=[a] R is a function of a, any equivalence relation R be defined using aRb :≡ “a and b have the same f value”, given that f.

41 Equivalence Class Examples “Strings a and b are the same length.”“Strings a and b are the same length.” –[a] = the set of all strings of the same length as a. “Integers a and b have the same absolute value.”“Integers a and b have the same absolute value.” –[a] = the set {a, −a} “Real numbers a and b have the same fractional part (i.e., a − b  Z).”“Real numbers a and b have the same fractional part (i.e., a − b  Z).” –[a] = the set {…, a−2, a−1, a, a+1, a+2, …}

42 Partitions A partition of a set A is the set of all the equivalence classes {A 1, A 2, … } for some e.r. on A.A partition of a set A is the set of all the equivalence classes {A 1, A 2, … } for some e.r. on A. The A i ’s are all disjoint and their union = A.The A i ’s are all disjoint and their union = A. They “partition” the set into pieces. Within each piece, all members of the set are equivalent to each other.They “partition” the set into pieces. Within each piece, all members of the set are equivalent to each other.