Section 4.1: Properties of Binary Relations A binary relation R over - - PowerPoint PPT Presentation

section 4 1 properties of binary relations
SMART_READER_LITE
LIVE PREVIEW

Section 4.1: Properties of Binary Relations A binary relation R over - - PowerPoint PPT Presentation

Section 4.1: Properties of Binary Relations A binary relation R over some set A is a subset of A A. If (x,y) R we sometimes write x R y. Example: Let R be the binary relaion less (<) over N . {(0,1), (0,2), (1,2),


slide-1
SLIDE 1

Section 4.1 CS340-Discrete Structures Page 1

Section 4.1: Properties of Binary Relations

A “binary relation” R over some set A is a subset of A×A. If (x,y) ∈ R we sometimes write x R y. Example: Let R be the binary relaion “less” (“<”) over N. {(0,1), (0,2), … (1,2), (1,3), … } (4,7) ∈ R Normally, we write: 4 < 7 Additional Examples: Here are some binary relations over A={0,1,2} Ø (nothing is related to anything) A×A (everything is related to everything) eq = {(0,0), (1,1),(2,2)} less = {(0,1),(0,2),(1,2)}

slide-2
SLIDE 2

Section 4.1 CS340-Discrete Structures Page 2

Representing Relations with Digraphs (directed graphs)

Let R = {(a,b), (b,a), (b,c)} over A={a,b,c} We can represent R with this graph: R: a b c

slide-3
SLIDE 3

Section 4.1 CS340-Discrete Structures Page 3

Properties of Binary Relations:

R is reflexive x R x for all x∈A Every element is related to itself. R is symmetric x R y implies y R x, for all x,y∈A The relation is reversable. R is transitive x R y and y R z implies x R z, for all x,y,z∈A Example: i<7 and 7<j implies i<j. R is irreflexive (x,x) ∉ R, for all x∈A Elements aren’t related to themselves. R is antisymmetric x R y and y R x implies that x=y, for all x,y,z∈A Example: i≤7 and 7≤i implies i=7.

slide-4
SLIDE 4

Section 4.1 CS340-Discrete Structures Page 4

Properties of Binary Relations:

R is reflexive x R x for all x∈A Every element is related to itself. R is symmetric x R y implies y R x, for all x,y∈A The relation is reversable. R is transitive x R y and y R z implies x R z, for all x,y,z∈A Example: i<7 and 7<j implies i<j. R is irreflexive (x,x) ∉ R, for all x∈A Elements aren’t related to themselves. R is antisymmetric x R y and y R x implies that x=y, for all x,y,z∈A Example: i≤7 and 7≤i implies i=7. a b c d

Reflexive

slide-5
SLIDE 5

Section 4.1 CS340-Discrete Structures Page 5

Properties of Binary Relations:

R is reflexive x R x for all x∈A Every element is related to itself. R is symmetric x R y implies y R x, for all x,y∈A The relation is reversable. R is transitive x R y and y R z implies x R z, for all x,y,z∈A Example: i<7 and 7<j implies i<j. R is irreflexive (x,x) ∉ R, for all x∈A Elements aren’t related to themselves. R is antisymmetric x R y and y R x implies that x=y, for all x,y,z∈A Example: i≤7 and 7≤i implies i=7. a b c d

Symmetric:

All edges are 2-way: Might as well use undirected edges!

slide-6
SLIDE 6

Section 4.1 CS340-Discrete Structures Page 6

Properties of Binary Relations:

R is reflexive x R x for all x∈A Every element is related to itself. R is symmetric x R y implies y R x, for all x,y∈A The relation is reversable. R is transitive x R y and y R z implies x R z, for all x,y,z∈A Example: i<7 and 7<j implies i<j. R is irreflexive (x,x) ∉ R, for all x∈A Elements aren’t related to themselves. R is antisymmetric x R y and y R x implies that x=y, for all x,y,z∈A Example: i≤7 and 7≤i implies i=7. a b c d

Symmetric:

All edges are 2-way: Might as well use undirected edges!

slide-7
SLIDE 7

Section 4.1 CS340-Discrete Structures Page 7

Properties of Binary Relations:

R is reflexive x R x for all x∈A Every element is related to itself. R is symmetric x R y implies y R x, for all x,y∈A The relation is reversable. R is transitive x R y and y R z implies x R z, for all x,y,z∈A Example: i<7 and 7<j implies i<j. R is irreflexive (x,x) ∉ R, for all x∈A Elements aren’t related to themselves. R is antisymmetric x R y and y R x implies that x=y, for all x,y,z∈A Example: i≤7 and 7≤i implies i=7. a b c d

Symmetric:

All edges are 2-way: Might as well use undirected edges!

slide-8
SLIDE 8

Section 4.1 CS340-Discrete Structures Page 8

Properties of Binary Relations:

R is reflexive x R x for all x∈A Every element is related to itself. R is symmetric x R y implies y R x, for all x,y∈A The relation is reversable. R is transitive x R y and y R z implies x R z, for all x,y,z∈A Example: i<7 and 7<j implies i<j. R is irreflexive (x,x) ∉ R, for all x∈A Elements aren’t related to themselves. R is antisymmetric x R y and y R x implies that x=y, for all x,y,z∈A Example: i≤7 and 7≤i implies i=7. a b c d

Transitive:

If you can get from x to y, then there is an edge directly from x to y!

slide-9
SLIDE 9

Section 4.1 CS340-Discrete Structures Page 9

Properties of Binary Relations:

R is reflexive x R x for all x∈A Every element is related to itself. R is symmetric x R y implies y R x, for all x,y∈A The relation is reversable. R is transitive x R y and y R z implies x R z, for all x,y,z∈A Example: i<7 and 7<j implies i<j. R is irreflexive (x,x) ∉ R, for all x∈A Elements aren’t related to themselves. R is antisymmetric x R y and y R x implies that x=y, for all x,y,z∈A Example: i≤7 and 7≤i implies i=7. a b c d

Transitive:

If you can get from x to y, then there is an edge directly from x to y!

slide-10
SLIDE 10

Section 4.1 CS340-Discrete Structures Page 10

Properties of Binary Relations:

R is reflexive x R x for all x∈A Every element is related to itself. R is symmetric x R y implies y R x, for all x,y∈A The relation is reversable. R is transitive x R y and y R z implies x R z, for all x,y,z∈A Example: i<7 and 7<j implies i<j. R is irreflexive (x,x) ∉ R, for all x∈A Elements aren’t related to themselves. R is antisymmetric x R y and y R x implies that x=y, for all x,y,z∈A Example: i≤7 and 7≤i implies i=7. a b c d

Irreflexive:

You won’t see any edges like these!

slide-11
SLIDE 11

Section 4.1 CS340-Discrete Structures Page 11

Properties of Binary Relations:

R is reflexive x R x for all x∈A Every element is related to itself. R is symmetric x R y implies y R x, for all x,y∈A The relation is reversable. R is transitive x R y and y R z implies x R z, for all x,y,z∈A Example: i<7 and 7<j implies i<j. R is irreflexive (x,x) ∉ R, for all x∈A Elements aren’t related to themselves. R is antisymmetric x R y and y R x implies that x=y, for all x,y,z∈A Example: i≤7 and 7≤i implies i=7. a b c d

Antisymmetric:

You won’t see any edges like these! (although xRx is

  • kay:

x )

slide-12
SLIDE 12

Section 4.1 CS340-Discrete Structures Page 12

Properties of Binary Relations:

R is reflexive x R x for all x∈A R is symmetric x R y implies y R x, for all x,y∈A R is transitive x R y and y R z implies x R z, for all x,y,z∈A R is irreflexive (x,x) ∉ R, for all x∈A R is antisymmetric x R y and y R x implies that x=y, for all x,y,z∈A Examples: Here are some binary relations over A={0,1}. Which of the properties hold? Answers: Ø A×A eq = {(0,0), (1,1)} less = {(0,1)}

slide-13
SLIDE 13

Section 4.1 CS340-Discrete Structures Page 13

Properties of Binary Relations:

R is reflexive x R x for all x∈A R is symmetric x R y implies y R x, for all x,y∈A R is transitive x R y and y R z implies x R z, for all x,y,z∈A R is irreflexive (x,x) ∉ R, for all x∈A R is antisymmetric x R y and y R x implies that x=y, for all x,y,z∈A Examples: Here are some binary relations over A={0,1}. Which of the properties hold? Answers: Ø symmetric,transitive,irreflexive,antisymmetric A×A reflexive, symmetric, transitive eq = {(0,0), (1,1)} reflexive, symmetric, transitive, antisymmetric less = {(0,1)} transitive, irreflexive, antisymmetric

slide-14
SLIDE 14

Section 4.1 CS340-Discrete Structures Page 14

Composition of Relations

If R and S are binary relations, then the composition of R and S is R ᐤ S = {(x,z) | x R y and y S z for some y } A B C D A B C D A B C D

R S

A B C D

R S

slide-15
SLIDE 15

Section 4.1 CS340-Discrete Structures Page 15

Composition of Relations

If R and S are binary relations, then the composition of R and S is R ᐤ S = {(x,z) | x R y and y S z for some y } A B C D A B C D A B C D

R S

A B C D

R S

slide-16
SLIDE 16

Section 4.1 CS340-Discrete Structures Page 16

Composition of Relations

If R and S are binary relations, then the composition of R and S is R ᐤ S = {(x,z) | x R y and y S z for some y } Examples: eq ᐤ less = ? R ᐤ Ø = ? isMotherOf ᐤ isFatherOf = ? isSonOf ᐤ isSiblingOf = ?

slide-17
SLIDE 17

Section 4.1 CS340-Discrete Structures Page 17

Composition of Relations

If R and S are binary relations, then the composition of R and S is R ᐤ S = {(x,z) | x R y and y S z for some y } Examples: eq ᐤ less = less { (x,z) | x=y and y<x, for some y} R ᐤ Ø = Ø isMotherOf ᐤ isFatherOf = isPaternalGrandmotherOf { (x,z) | x isMotherOf y and y isFatherOf x, for some y} isSonOf ᐤ isSiblingOf = isNephewOf { (x,z) | x isSonOf y and y isSiblingOf x, for some y}

slide-18
SLIDE 18

Section 4.1 CS340-Discrete Structures Page 18

Representing Relations with Digraphs (directed graphs)

Let R = {(a,b), (b,a),(b,c)} over A={a,b,c} Let R2 = R ᐤ R = ? We can represent R graphically: R: a b c

slide-19
SLIDE 19

Section 4.1 CS340-Discrete Structures Page 19

Representing Relations with Digraphs (directed graphs)

Let R = {(a,b), (b,a),(b,c)} over A={a,b,c} Let R2 = R ᐤ R Let R3 = R ᐤ R ᐤ R = ? We can represent R graphically: R: a b c R2: a b c

slide-20
SLIDE 20

Section 4.1 CS340-Discrete Structures Page 20

Representing Relations with Digraphs (directed graphs)

Let R = {(a,b), (b,a),(b,c)} over A={a,b,c} Let R2 = R ᐤ R Let R3 = R ᐤ R ᐤ R = R2 ᐤ R. We can represent R graphically: In this example, R3 happens to be the same relation as R. R3 = R Note: By definition, R0 = Eq, where x Eq y iff x=y. R: a b c R2: a b c R3: a b c

slide-21
SLIDE 21

Section 4.1 CS340-Discrete Structures Page 21

Reflexive Closure

Given a relation R, we want to add to it just enough “edges” to make the resulting relation satisfy the reflexive property. Reflexive Closure of R is r(R) = R ∪ Eq, where Eq is the equality relation. Example: r(R) = R ∪ Eq = {(a,b),(b,a),(b,c),(a,a),(b,b),(c,c)} R: a b c r(R): a b c

slide-22
SLIDE 22

Section 4.1 CS340-Discrete Structures Page 22

Symmetric Closure

Given a relation R, we want to add to it just enough “edges” to make the resulting relation satisfy the symmetric property. Symmetric Closure of R is s(R) = R ∪ Rc, where Rc is the converse

  • relation. Rc = {(b,a) | a R b}

Example: s(R) = R ∪ Rc = {(a,b),(b,a),(b,c),(c,b)} R: a b c s(R): a b c

slide-23
SLIDE 23

Section 4.1 CS340-Discrete Structures Page 23

Transitive Closure

Given a relation R, we want to add to it just enough “edges” to make the resulting relation satisfy the transitivity property. Transitive Closure of R is t(R) = R ∪ R2 ∪ R3 ∪ … Note: If the number of nodes is finite… If |A| = n then t(R) = R ∪ R2 ∪ R3 ∪ … ∪ Rn Example: t(R) = R ∪ R2 ∪ R3 = {(a,b),(b,a),(b,c),(a,a),(b,b),(a,c)} If there is a If there is a path path from x to y, then add an from x to y, then add an edge edge directly from x to y. directly from x to y. R: a b c t(R): a b c

slide-24
SLIDE 24

Section 4.1 CS340-Discrete Structures Page 24

In-Class Quiz:

Let R = {(x,x+1) | x∈Z } What is t(R)? What is rt(R)? What is st(R)?

slide-25
SLIDE 25

Section 4.1 CS340-Discrete Structures Page 25

In-Class Quiz:

Let R = {(x,x+1) | x∈Z } What is t(R)? < What is rt(R)? What is st(R)?

slide-26
SLIDE 26

Section 4.1 CS340-Discrete Structures Page 26

In-Class Quiz:

Let R = {(x,x+1) | x∈Z } What is t(R)? < What is rt(R)? ≤ What is st(R)?

slide-27
SLIDE 27

Section 4.1 CS340-Discrete Structures Page 27

In-Class Quiz:

Let R = {(x,x+1) | x∈Z } What is t(R)? < What is rt(R)? ≤ What is st(R)? ≠

slide-28
SLIDE 28

Section 4.1 CS340-Discrete Structures Page 28

Adjacency Matrix Idea: Use a matrix to represent a directed graph (or a relation). Let R = {(a,b),(b,c),(c,d)} Number the elements in the set: 1,2,3,… a b c d

slide-29
SLIDE 29

Section 4.1 CS340-Discrete Structures Page 29

Adjacency Matrix Idea: Use a matrix to represent a directed graph (or a relation). Let R = {(a,b),(b,c),(c,d)} Number the elements in the set: 1,2,3,… a b c d 1 2 3 4

slide-30
SLIDE 30

Section 4.1 CS340-Discrete Structures Page 30

Adjacency Matrix Idea: Use a matrix to represent a directed graph (or a relation). Let R = {(a,b),(b,c),(c,d)} Number the elements in the set: 1,2,3,… Now we can write R = {(1,2),(2,3),(3,4)} The matrix M will have a “1” in position Mij if i R j, and “0” otherwise. M = 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 2 3 4

slide-31
SLIDE 31

Section 4.1 CS340-Discrete Structures Page 31

Computing the Transitive Closure: Warshall’s Algorithm

We can use this matrix to compute the t(R), the transitive closure of R. Idea: Every time we find this pattern: …add this edge: i k j Warshall’s Algorithm for k := 1 to n for i := 1 to n for j := 1 to n if Mik = Mkj = 1 then Mij := 1 endIf endFor endFor endFor

slide-32
SLIDE 32

Section 4.1 CS340-Discrete Structures Page 32

Computing the Transitive Closure: Warshall’s Algorithm

We can use this matrix to compute the t(R), the transitive closure of R. Idea: Every time we find this pattern: …add this edge: Consider all ways to bypass node 1. Then forget about node 1. Consider all ways to bypass node 2. and so on… i k j 1 2 Warshall’s Algorithm for k := 1 to n for i := 1 to n for j := 1 to n if Mik = Mkj = 1 then Mij := 1 endIf endFor endFor endFor

slide-33
SLIDE 33

Section 4.1 CS340-Discrete Structures Page 33

Computing the Transitive Closure: Warshall’s Algorithm

We can use this matrix to compute the t(R), the transitive closure of R. Idea: Every time we find this pattern: …add this edge: Consider all ways to bypass node 1. Then forget about node 1. Consider all ways to bypass node 2. and so on… i k j 1 2 Warshall’s Algorithm for k := 1 to n for i := 1 to n for j := 1 to n if Mik = Mkj = 1 then Mij := 1 endIf endFor endFor endFor

slide-34
SLIDE 34

Section 4.1 CS340-Discrete Structures Page 34

Computing the Transitive Closure: Warshall’s Algorithm

We can use this matrix to compute the t(R), the transitive closure of R. Idea: Every time we find this pattern: …add this edge: Consider all ways to bypass node 1. Then forget about node 1. Consider all ways to bypass node 2. and so on… i k j 1 2 Warshall’s Algorithm for k := 1 to n for i := 1 to n for j := 1 to n if Mik = Mkj = 1 then Mij := 1 endIf endFor endFor endFor

slide-35
SLIDE 35

Section 4.1 CS340-Discrete Structures Page 35

Computing the Transitive Closure: Warshall’s Algorithm

We can use this matrix to compute the t(R), the transitive closure of R. Idea: Every time we find this pattern: …add this edge: Consider all ways to bypass node 1. Then forget about node 1. Consider all ways to bypass node 2. and so on… i k j 1 2 Warshall’s Algorithm for k := 1 to n for i := 1 to n for j := 1 to n if Mik = Mkj = 1 then Mij := 1 endIf endFor endFor endFor

slide-36
SLIDE 36

Section 4.1 CS340-Discrete Structures Page 36

Computing the Transitive Closure: Warshall’s Algorithm

We can use this matrix to compute the t(R), the transitive closure of R. Idea: Every time we find this pattern: …add this edge: Consider all ways to bypass node 1. Then forget about node 1. Consider all ways to bypass node 2. and so on… i k j 1 2 Warshall’s Algorithm for k := 1 to n for i := 1 to n for j := 1 to n if Mik = Mkj = 1 then Mij := 1 endIf endFor endFor endFor

slide-37
SLIDE 37

Section 4.1 CS340-Discrete Structures Page 37

Example:

k=1 1 2 3 4 M = 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 Warshall’s Algorithm for k := 1 to n for i := 1 to n for j := 1 to n if Mik = Mkj = 1 then Mij := 1 endIf endFor endFor endFor

slide-38
SLIDE 38

Section 4.1 CS340-Discrete Structures Page 38

Example:

k=2 1 2 3 4 M = 0 1 1 0 0 0 1 0 0 0 0 1 0 0 0 0 Warshall’s Algorithm for k := 1 to n for i := 1 to n for j := 1 to n if Mik = Mkj = 1 then Mij := 1 endIf endFor endFor endFor

slide-39
SLIDE 39

Section 4.1 CS340-Discrete Structures Page 39

Example:

k=3 1 2 3 4 M = 0 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 Warshall’s Algorithm for k := 1 to n for i := 1 to n for j := 1 to n if Mik = Mkj = 1 then Mij := 1 endIf endFor endFor endFor

slide-40
SLIDE 40

Section 4.1 CS340-Discrete Structures Page 40

Example:

k=3 1 2 3 4 M = 0 1 1 1 0 0 1 1 0 0 0 1 0 0 0 0 Warshall’s Algorithm for k := 1 to n for i := 1 to n for j := 1 to n if Mik = Mkj = 1 then Mij := 1 endIf endFor endFor endFor

slide-41
SLIDE 41

Section 4.1 CS340-Discrete Structures Page 41

Example:

k=4 …Done; no more changes. 1 2 3 4 M = 0 1 1 1 0 0 1 1 0 0 0 1 0 0 0 0 Warshall’s Algorithm for k := 1 to n for i := 1 to n for j := 1 to n if Mik = Mkj = 1 then Mij := 1 endIf endFor endFor endFor

slide-42
SLIDE 42

Section 4.1 CS340-Discrete Structures Page 42

Path Problems: Floyd’s Algorithm

Consider a directed graph with weights on the edges.

Problems:

Find the cheapest path from x to y. Find the shortest path from x to y. (Just make all weights = 1!) 1 2 3 4 5 6 10
 30
 30
 10
 5
 40
 10
 20


slide-43
SLIDE 43

Section 4.1 CS340-Discrete Structures Page 43

Path Problems: Floyd’s Algorithm

Consider a directed graph with weights on the edges. Idea: Represent the graph with a matrix. Store the weights. For non-existent edges, use a weight of ∞. Then modify Warshall’s Algorithm. 1 2 3 4 5 6 10
 30
 30
 10
 5
 40
 10
 20
 M = 10 10 ∞ 20 10 ∞ ∞ 30 ∞ ∞ ∞ ∞ 30 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 40 ∞ ∞ ∞ ∞ ∞ 5

slide-44
SLIDE 44

Section 4.1 CS340-Discrete Structures Page 44

Path Problems: Floyd’s Algorithm

Consider a directed graph with weights on the edges. Floyd’s Algorithm: for k := 1 to n for i := 1 to n for j := 1 to n if Mik+Mkj < Mij Mij := Mik+Mkj endIf endFor endFor endFor 1 2 3 4 5 6 10
 30
 30
 10
 5
 40
 10
 20
 M = 10 10 ∞ 20 10 ∞ ∞ 30 ∞ ∞ ∞ ∞ 30 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 40 ∞ ∞ ∞ ∞ ∞ 5

slide-45
SLIDE 45

Section 4.1 CS340-Discrete Structures Page 45

Path Problems: Floyd’s Algorithm

Consider a directed graph with weights on the edges. Floyd’s Algorithm: for k := 1 to n for i := 1 to n for j := 1 to n if Mik+Mkj < Mij Mij := Mik+Mkj endIf endFor endFor endFor 1 2 3 4 5 6 10
 30
 30
 10
 5
 40
 10
 20
 40
 45
 15
 M = 10 10 40 15 10 ∞ ∞ 30 ∞ ∞ ∞ ∞ 30 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 40 ∞ ∞ ∞ ∞ 45 5

slide-46
SLIDE 46

Section 4.1 CS340-Discrete Structures Page 46

Path Problems: Floyd’s Algorithm

How can we remember the best path? P=next node in best path! Floyd’s Algorithm: for k := 1 to n for i := 1 to n for j := 1 to n if Mik+Mkj < Mij Mij := Mik+Mkj Pij := k endIf endFor endFor endFor 1 2 3 4 5 6 10
 30
 30
 10
 5
 40
 10
 20
 40
 45
 15
 M = P = 2 6 5 10 10 40 15 10 ∞ ∞ 30 ∞ ∞ ∞ ∞ 30 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 40 ∞ ∞ ∞ ∞ 45 5