8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex...

62
8.3 Representing Relations Directed Graphs Vertex Arc (directed edge) Initial vertex Terminal vertex

Transcript of 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex...

Page 1: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

8.3 Representing Relations

• Directed Graphs– Vertex– Arc (directed edge)– Initial vertex– Terminal vertex

Page 2: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Example

• Draw the “divides” relation on the set {2,3,4,5,6,7,8,9} as a directed graph

Page 3: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

The zero-one Matrix Representation MR

• MR is just a zero-one version of the “chart” representation of R.

2 3 4 5 6 7 8 92 1 0 1 0 1 0 1 03 0 1 0 0 1 0 0 14 0 0 1 0 0 0 1 05 0 0 0 1 0 0 0 06 0 0 0 0 1 0 0 07 0 0 0 0 0 1 0 08 0 0 0 0 0 0 1 09 0 0 0 0 0 0 0 1

Page 4: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Reflexivity

Directed graph picture Zero-one matrix picture

Page 5: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Symmetry

Directed graph picture Zero-one matrix picture

Page 6: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Antisymmetry

Directed graph picture Zero-one matrix picture

Page 7: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Transitivity

Directed graph picture Zero-one matrix picture

Page 8: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Theorem

1221

2121

2121

and ,

,

RRRR

RRRR

RRRR

MMM

MMM

MMM

For relations R1 and R2 on set A,

Page 9: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Example: Let and be binary relations. Find and . Use them to find Verify by calculating without matrices.

Page 10: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Corollary

][nRR

MM n

For a relation R on set A,

for any positive integer n.

Page 11: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Example: Let and . Calculate and to determine if the relations and are transitive.

Page 12: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

8.4 Closures of Relations

• Reflexive closure

• Symmetric closure

Page 13: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Example: Let R be the relation on the set containing the pairs

What is the reflexive closure of R? What is the symmetric closure of R?

{ (1,1 ) , (1,2 ) , (2,4 ) , (3,1 ) , (4,2 ) }

Page 14: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Paths in Directed Graphs

• A path in a directed graph is a sequence of vertices for which any two consecutive vertices ai and ai+1 in the sequence are joined by an arc from ai to ai+1.

• Theorem: Let R be a relation on set A, and n a positive integer. Then there is a path of length n from a to b in R if and only if (a,b) is in Rn.

Page 15: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Example:

Page 16: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

The “Connectivity Relation” R*

• Let R be a relation on set A. We define

1

*

i

iRR

Page 17: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Example: Let be the relation on the set of all people in the world that contains if has met . What is , where is a positive integer greater than one? What is ?

Page 18: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

The Transitive Closure

• For a relation R on a set A, we define the transitive closure of R to be the smallest transitive relation containing R.

• Theorem:

Page 19: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Finding transitive closure the “hard” way: 𝑅={ (1,1 ) , (1,2 ) , (2,4 ) , (3,1 ) , (4,2 ) }

Page 20: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Computing R*

• If A is a set with n elements, and R is a relation on A, then any time there is a path of length one or more from a to b in R then there is a path of length n or less.

• So actually

and

• Interestingly, this is not the best way of computing R*

n

i

iRR1

*

][

1*

iR

n

iRMM

Page 21: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

𝑅={ (1,1 ) , (1,2 ) , (2,4 ) , (3,1 ) , (4,2 ) }Computing transitive closure the better way:

𝑀𝑅=[1 1 00 0 010

01

00

0100] 𝑀𝑅❑

[2]=[ 1 1 00 1 010

10

00

1001]

𝑀𝑅❑[3 ]=[1 1 0

0 0 010

11

00

1110] 𝑀𝑅❑

[4 ]=[1 1 00 1 010

10

00

1011]

⋁ ⋁

¿𝑀𝑅∗=[1 1 00 1 010

11

00

1111]

Page 22: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Warshall’s Algorithm

procedure Warshall(MR: n by n zero-one matrix)

W := MR

for k:=1 to n for i:=1 to n for j:=1 to n wij := wij (wik wkj)

{ W now contains MR* }

Page 23: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Illustration of Warshall’s Algorithm

𝑀𝑅=[1 1 00 0 010

01

00

0100] [1 1 0

0 0 010

01

00

0100][1 1 0

0 0 010

01

00

1100] [1 1 0

0 0 010

01

00

1100][1 1 0

0 1 010

01

00

1100]

[1 1 00 1 010

01

00

1100][1 1 0

0 1 010

11

00

1100] [1 1 0

0 1 010

11

00

1100][1 1 0

0 1 010

11

00

1110] [1 1 0

0 1 010

11

00

1110][1 1 0

0 1 010

11

00

1111][1 1 0

0 1 010

11

00

1111]

Page 24: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

8.5 Equivalence Relations

• Definition: A relation R on set A is an equivalence relation if …

Page 25: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Examples

• aRb if and only if a and b have the same first name (on the set of students in this class)

• aRb if and only if a ≡ b (mod 5) (on the set of integers)

Page 26: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Equivalence Classes

• If R is a relation on set A, and a is an element of A, then…

Page 27: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Examples (continued)

• [Michael]

• [4]5

Page 28: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Theorem

For an equivalence relation R on set A and elements a and b of A, the following are all logically equivalent:

a) a R b

b) [a]R = [b]R

c) [a]R [b]R

Page 29: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Partitions

• For a set S, a partition of S is a collection = {A1, A2, …, Am} of nonempty subsets of S which satisfies the following properties:

– Every element of A is in one of the sets Ai.– For all i, j {1, 2, …, m},

if i j then Ai Aj = • Terminology: We say that the collection

partitions S.

Page 30: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Theorem

• Let R be an equivalence relation on set S. Then the equivalence classes of R partition S. Conversely, for any partition of S there is an equivalence relation R whose equivalence classes are the sets in .

Page 31: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

…, , ,-8, , ,-5, , ,-2, , ,1, , ,4, , ,7, , ,10, ……, ,-9, , ,-6, , ,-3, , ,0, , ,3, , ,6, , ,9, , ……,-10, , ,-7, , ,-4, , ,-1, , ,2, , ,5, , ,8, , , ……,-10, , ,-7, , ,-4, , ,-1, , ,2, , ,5, , ,8, , , ……, ,-9, , ,-6, , ,-3, , ,0, , ,3, , ,6, , ,9, , ……, , ,-8, , ,-5, , ,-2, , ,1, , ,4, , ,7, , ,10, …

Visual

[2]={… ,-10, -7, -4, -1, 2, 5, 8, …}

[0]={… ,-9, -6, -3, 0, 3, 6, 9, …}

[1]={… ,-8, -5, -2, 1, 4, 7, 10, …}

Page 32: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

…, , ,-8, , ,-5, , ,-2, , ,1, , ,4, , ,7, , ,10, …

…, ,-9, , ,-6, , ,-3, , ,0, , ,3, , ,6, , ,9, , …

Visual

…,-10, , ,-7, , ,-4, , ,-1, , ,2, , ,5, , ,8, , , …

[2]={… ,-10, -7, -4, -1, 2, 5, 8, …}

[0]={… ,-9, -6, -3, 0, 3, 6, 9, …}

[1]={… ,-8, -5, -2, 1, 4, 7, 10, …}

Page 33: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Example: Turning a partition into an equivalence relation

𝐴1= {1,5 } ,𝐴2={2,3,6 } ,𝐴3={ 4 }𝑜𝑛 h𝑡 𝑒𝑠𝑒𝑡 𝑆={1,2,3,4,5,6 }

Page 34: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Zero-One Matrix Representation of an Equivalence Relation Examples

Page 35: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Digraph Representation of an Equivalence Relation Examples

Page 36: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Equivalence as “sameness”

• Almost every equivalence relation definition comes down to identifying some notion of “sameness”– Same remainder when divided by n– Same name– Same set of a partition

Page 37: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Number of Partitions of a Set with n Elements

• n = 1

• n = 2

• n = 3

• n = 4

Page 38: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Recurrence Relation for the Number of Partitions of a Set with n Elements

Page 39: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

8.6 Partial Orderings

Let A be a set, and R a relation on A. We say that R is a partial ordering if and only if…

In this case we say that the pair (A, R) is a partially ordered set (poset).

Page 40: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Examples:

1. The real numbers R under the relation2. The real numbers R under the relation3. The positive integers under the “divides”

relation4. Any set of sets under the (subset)

relation5. The cartesian product ZZ under the

“(,)” relation R. (i.e. (x,y) R (z,w) if and only if x z and y w.)

Page 41: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Convention

• The symbol is the default symbol used to represent a partial ordering.

• Example: “Let A be a set, and let be a partial ordering on A.”

Page 42: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Comparable and Incomparable Elements

• Two elements a and b of a partially ordered set are said to be incomparable if and only if the statements a b and b a are both false. Otherwise the elements are comparable.

• Examples: – Subsets– Cartesian products– Divides relation

Page 43: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Examples:

Page 44: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Total Orderings

• Let A be a set, and let be a partial ordering on A. We say that is a total ordering provided…

In this case we say that the pair (A, ) is a totally ordered set. (linearly ordered set, chain)

• Examples: 1) Real numbers under 2) Any set of strings under the “dictionary”, or lexicographic, ordering

Page 45: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

A partial order on “induced” by partial orders on and on

• Lexicographic ordering

• Example:

Page 46: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Well-Ordered Sets

• A set S is well-ordered by the partial ordering if and only if every nonempty subset of S has a least element (minimum element).

• Examples:

• Non-Examples: , ,

Page 47: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Hasse Diagrams

• Begin with the digraph representation of the partial ordering

• Omit the reflexive loops• Omit all edges which would be implied by

transitivity• Orient all vertices and arcs so that the

direction of each arc is up.• Remove the direction arrow from each arc

Page 48: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Hasse Diagram Example I

• Pairs in {1,2,3}{1,2,3} under lexicographic order

Page 49: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Hasse Diagram Example I

• Pairs in {1,2,3}{1,2,3} under lexicographic order

Page 50: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Hasse Diagram Example I

• Pairs in {1,2,3}{1,2,3} under lexicographic order

Page 51: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

• Pairs in {1,2,3}{1,2,3} under lexicographic order

Hasse Diagram Example I

Page 52: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

• Pairs in {1,2,3}{1,2,3} under lexicographic order

Hasse Diagram Example I

Page 53: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Hasse Diagram Example II

• Integers 1-12 under “divides”

Page 54: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Hasse Diagram Example III

• Integers 1-12 under “divides”

Page 55: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Maximal and Minimal ElementsGiven a poset and an element , we say

• is maximal in the poset if

• is minimal in the poset if

• is the greatest element of the poset if

• is the least element of the poset if

Page 56: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Theorems

• Every finite poset has a minimal element

• Every finite poset has a maximal element

Page 57: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Example:

Maximal Elements?

Minimal Elements?

Greatest Element?

Least Element?

Page 58: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Upper Bounds and Lower Bounds

Given a subset of a poset we say

• is an upper bound of if

• is a lower bound of if

• is the least upper bound of if

• is the greatest lower bound of if

Page 59: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Example:Find the upper and lower bounds of the subsets {c,f}, {h,i}, {c,d,e}.

Find the greatest lower bound and the least upper bound of {b,d} and {a,b,g}.

Page 60: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Topological Sorts

• Let (A,) be a partial ordering, and let be a total ordering on the same set A. We say that is a compatible total ordering for provided that for all a, b in A, whenever a b then also a b.

• A topological sort is an algorithm which, given a partially ordered set, generates a compatible total ordering. In other words, it generates the elements of A one by one in a linear order compatible with the partial ordering. (PERT charts)

Page 61: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Topological Sort Algorithm

Procedure TopologicalSort(S: finite poset)

Let q be a queue of elements of S, initially empty

While S is not empty, do begin

Choose and enqueue a minimal element s of

S onto the queue q.

Remove s from S

End

{ The queue q now contains all the elements of S, arranged in a compatible total ordering of S. }

Page 62: 8.3 Representing Relations Directed Graphs –Vertex –Arc (directed edge) –Initial vertex –Terminal vertex.

Topological Sort Trace