section 4.1: properties of binary...

46
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)}

Upload: dokhuong

Post on 17-May-2018

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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)}

Page 2: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 3: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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.

Page 4: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 5: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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!

Page 6: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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!

Page 7: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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!

Page 8: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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!

Page 9: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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!

Page 10: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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!

Page 11: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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 okay:

x )

Page 12: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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)}

Page 13: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 14: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 15: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 16: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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 = ?

Page 17: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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}

Page 18: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 19: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 20: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 21: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 22: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 23: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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 path from x to y, then add an edge directly from x to y.

R:

a b c

t(R):

a b c

Page 24: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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)?

Page 25: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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)?

Page 26: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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)?

Page 27: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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)? ≠

Page 28: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 29: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 30: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 31: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 32: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 33: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 34: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 35: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 36: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 37: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 38: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 39: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 40: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 41: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

Page 42: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

3030

10

5

40

10

20

Page 43: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

3030

10

5

40

10

20

M =

0 10 10 ∞ 20 10 ∞ 0 ∞ 30 ∞ ∞ ∞ ∞ 0 30 ∞ ∞ ∞ ∞ ∞ 0 ∞ ∞ ∞ ∞ ∞ 40 0 ∞ ∞ ∞ ∞ ∞ 5 0

Page 44: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

3030

10

5

40

10

20

M =

0 10 10 ∞ 20 10 ∞ 0 ∞ 30 ∞ ∞ ∞ ∞ 0 30 ∞ ∞ ∞ ∞ ∞ 0 ∞ ∞ ∞ ∞ ∞ 40 0 ∞ ∞ ∞ ∞ ∞ 5 0

Page 45: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

3030

10

5

40

10

20

40

45

15

M =

0 10 10 40 15 10 ∞ 0 ∞ 30 ∞ ∞ ∞ ∞ 0 30 ∞ ∞ ∞ ∞ ∞ 0 ∞ ∞ ∞ ∞ ∞ 40 0 ∞ ∞ ∞ ∞ 45 5 0

Page 46: Section 4.1: Properties of Binary Relationsweb.cecs.pdx.edu/~harry/discrete/slides/Section4.1.pdfCS340-Discrete Structures Section 4.1 Page 1 Section 4.1: Properties of Binary Relations

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

3030

10

5

40

10

20

40

45

15

M = P =

0 0 0 2 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 0

0 10 10 40 15 10 ∞ 0 ∞ 30 ∞ ∞ ∞ ∞ 0 30 ∞ ∞ ∞ ∞ ∞ 0 ∞ ∞ ∞ ∞ ∞ 40 0 ∞ ∞ ∞ ∞ 45 5 0